From 08895b65b12ab21136004c76571719c6bf21df34 Mon Sep 17 00:00:00 2001 From: overtrue Date: Sun, 12 Jun 2016 10:39:28 +0800 Subject: [PATCH] Facade support. #8 --- README.md | 34 +++++++++++++++++++++++++++++----- src/Facades/Pinyin.php | 14 ++++++++++++++ src/ServiceProvider.php | 19 ++++++++++++++++++- src/helpers.php | 12 ++++++------ 4 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 src/Facades/Pinyin.php diff --git a/README.md b/README.md index a929e78..b45f84f 100755 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/Facades/Pinyin.php b/src/Facades/Pinyin.php new file mode 100644 index 0000000..64471a8 --- /dev/null +++ b/src/Facades/Pinyin.php @@ -0,0 +1,14 @@ +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']; + } } \ No newline at end of file diff --git a/src/helpers.php b/src/helpers.php index 7da038b..9d3d874 100755 --- a/src/helpers.php +++ b/src/helpers.php @@ -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".'); @@ -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".'); @@ -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".'); @@ -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".'); @@ -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".');