Skip to content

Commit

Permalink
Facade support. #8
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jun 12, 2016
1 parent 9d7eeef commit 08895b6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ composer require "overtrue/laravel-pinyin:~3.0"
Add the following line to the section `providers` of `config/app.php`:

```php
...
Overtrue\LaravelPinyin\ServiceProvider::class,
...
'providers' => [
//...
Overtrue\LaravelPinyin\ServiceProvider::class,
],
```

as optional, you can use facade:

```php

'aliases' => [
//...
'Pinyin' => Overtrue\LaravelPinyin\Facades\Pinyin::class,
],
```

## For Lumen
Expand Down Expand Up @@ -53,14 +64,27 @@ There are more convenient functions:
| `pinyin_sentence` | `app('pinyin')->sentence()` |

```php
echo pinyin('带着希望去旅行,比到达终点更美好');
var_dump(pinyin('带着希望去旅行,比到达终点更美好'));
// ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]

echo pinyin_abbr('带着希望去旅行');
var_dump(pinyin_abbr('带着希望去旅行'));
// dzxwqlx
...
```

Using facade:

```php
use Pinyin; // Facade class, NOT Overtrue\Pinyin\Pinyin

var_dump(Pinyin::convert('带着希望去旅行'));
// ["dai", "zhe", "xi", "wang", "qu", "lv", "xing"]

echo Pinyin::sentence('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo

```

About `overtrue/pinyin` specific configuration and use, refer to: [overtrue/pinyin](https://github.com/overtrue/pinyin)

## License
Expand Down
14 changes: 14 additions & 0 deletions src/Facades/Pinyin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Overtrue\LaravelPinyin\Facades;

use Illuminate\Support\Facades\Facade;
use Overtrue\Pinyin\Pinyin as Accessor;

class Pinyin extends Facade
{
public static function getFacadeAccessor()
{
return Accessor::class;
}
}
19 changes: 18 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

class ServiceProvider extends LaravelServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Boot the provider.
*
Expand All @@ -24,9 +31,19 @@ public function boot()
*/
public function register()
{
$this->app->singleton('pinyin', function($app)
$this->app->singleton([Pinyin::class => 'pinyin'], function($app)
{
return new Pinyin();
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [Pinyin::class, 'pinyin'];
}
}
12 changes: 6 additions & 6 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*
* @return string
*/
function pinyin($string, $option = PINYIN_NONE)
function pinyin($string, $option = Pinyin::NONE)
{
return app('pinyin')->convert($string, $option);
return app(Pinyin::class)->convert($string, $option);
}
} else {
Log::warning('There exist multiple function "pinyin".');
Expand All @@ -31,7 +31,7 @@ function pinyin($string, $option = PINYIN_NONE)
*/
function pinyin_abbr($string, $delimiter = '')
{
return app('pinyin')->abbr($string, $delimiter);
return app(Pinyin::class)->abbr($string, $delimiter);
}
} else {
Log::warning('There exist multiple function "pinyin_abbr".');
Expand All @@ -50,7 +50,7 @@ function pinyin_abbr($string, $delimiter = '')
*/
function pinyin_permlink($string, $delimiter = '-')
{
return app('pinyin')->permalink($string, $delimiter);
return app(Pinyin::class)->permalink($string, $delimiter);
}
} else {
Log::warning('There exist multiple function "pinyin_permlink".');
Expand All @@ -67,7 +67,7 @@ function pinyin_permlink($string, $delimiter = '-')
*/
function pinyin_permalink($string, $delimiter = '-')
{
return app('pinyin')->permalink($string, $delimiter);
return app(Pinyin::class)->permalink($string, $delimiter);
}
} else {
Log::warning('There exist multiple function "pinyin_permalink".');
Expand All @@ -84,7 +84,7 @@ function pinyin_permalink($string, $delimiter = '-')
*/
function pinyin_sentence($string, $tone = false)
{
return app('pinyin')->sentence($string, $tone);
return app(Pinyin::class)->sentence($string, $tone);
}
} else {
Log::warning('There exist multiple function "pinyin_sentence".');
Expand Down

0 comments on commit 08895b6

Please sign in to comment.