From 1f89e35b9670e764cf89c3c4601e735700076426 Mon Sep 17 00:00:00 2001 From: liujian Date: Thu, 5 Aug 2021 11:27:11 +0800 Subject: [PATCH] feat:examples:add mix/cli --- examples/api-skeleton/README.md | 22 ++++++------------- examples/api-skeleton/bin/cli.php | 21 +++++++++++++----- examples/api-skeleton/composer.json | 1 + .../api-skeleton/src/Command/ClearCache.php | 9 +++++--- examples/grpc-skeleton/README.md | 18 +++++---------- examples/grpc-skeleton/bin/cli.php | 21 +++++++++++++----- examples/grpc-skeleton/composer.json | 1 + .../grpc-skeleton/src/Command/ClearCache.php | 9 +++++--- examples/web-skeleton/README.md | 20 +++++------------ examples/web-skeleton/bin/cli.php | 21 +++++++++++++----- examples/web-skeleton/composer.json | 1 + .../web-skeleton/src/Command/ClearCache.php | 9 +++++--- examples/websocket-skeleton/README.md | 20 +++++------------ examples/websocket-skeleton/bin/cli.php | 21 +++++++++++++----- examples/websocket-skeleton/composer.json | 1 + .../src/Command/ClearCache.php | 9 +++++--- 16 files changed, 116 insertions(+), 88 deletions(-) diff --git a/examples/api-skeleton/README.md b/examples/api-skeleton/README.md index 07844ca4..2be40011 100644 --- a/examples/api-skeleton/README.md +++ b/examples/api-skeleton/README.md @@ -12,7 +12,7 @@ composer create-project --prefer-dist mix/api-skeleton api ## 快速开始 -启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 +启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 (零依赖) ``` composer run-script --timeout=0 cliserver:start @@ -139,8 +139,8 @@ $vega->handle('/users/{id}', [new Users(), 'index'])->methods('GET'); 路由里使用了 `Users` 控制器,我们需要创建他 -- 如何配置路由:[mix-php/vega](https://github.com/mix-php/vega#readme) -- 如何调用数据库:[mix-php/database](https://github.com/mix-php/database#readme) +- 如何配置路由:[mix/vega](https://github.com/mix-php/vega#readme) +- 如何调用数据库:[mix/database](https://github.com/mix-php/database#readme) ```php load(); define("APP_DEBUG", env('APP_DEBUG')); App\Error::register(); -switch ($argv[1]) { - case 'clearcache'; - (new \App\Command\ClearCache())->exec(); - break; -} +Cli::setName('app')->setVersion('0.0.0-alpha'); +$cmds = [ + new Mix\Cli\Command([ + 'name' => 'clearcache', + 'short' => 'Clear cache', + 'options' => [ + new Mix\Cli\Option([ + 'names' => ['k', 'key'], + 'usage' => 'Key name' + ]), + ], + 'run' => new App\Command\ClearCache(), + ]) +]; +Cli::addCommand(...$cmds)->run(); diff --git a/examples/api-skeleton/composer.json b/examples/api-skeleton/composer.json index 0d6a50f2..7cb41f2b 100644 --- a/examples/api-skeleton/composer.json +++ b/examples/api-skeleton/composer.json @@ -22,6 +22,7 @@ "require": { "workerman/workerman": "^4.0", "mix/vega": "~3.0", + "mix/cli": "~3.0", "mix/database": "~3.0", "mix/redis": "~3.0", "vlucas/phpdotenv": "^5.3", diff --git a/examples/api-skeleton/src/Command/ClearCache.php b/examples/api-skeleton/src/Command/ClearCache.php index ef2bb4cd..025bb761 100644 --- a/examples/api-skeleton/src/Command/ClearCache.php +++ b/examples/api-skeleton/src/Command/ClearCache.php @@ -3,17 +3,20 @@ namespace App\Command; use App\Container\RDS; +use Mix\Cli\Flag; +use Mix\Cli\RunInterface; /** * Class ClearCache * @package App\Command */ -class ClearCache +class ClearCache implements RunInterface { - public function exec(): void + public function main(): void { - RDS::instance()->del('foo_cache'); + $key = Flag::match('k', 'key')->string(); + RDS::instance()->del($key); print 'ok'; } diff --git a/examples/grpc-skeleton/README.md b/examples/grpc-skeleton/README.md index c13ff904..af7aa1c0 100644 --- a/examples/grpc-skeleton/README.md +++ b/examples/grpc-skeleton/README.md @@ -135,44 +135,36 @@ composer run-script swoole:start ## 如何使用 gRPC 客户端 -- [mix-php/grpc#客户端调用一个 gRPC 服务](https://github.com/mix-php/grpc#%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%B0%83%E7%94%A8%E4%B8%80%E4%B8%AA-grpc-%E6%9C%8D%E5%8A%A1) +- [mix/grpc#客户端调用一个 gRPC 服务](https://github.com/mix-php/grpc#%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%B0%83%E7%94%A8%E4%B8%80%E4%B8%AA-grpc-%E6%9C%8D%E5%8A%A1) ## 使用容器中的对象 容器采用了一个简单的单例模式,你可以修改为更加适合自己的方式。 -- 数据库 +- 数据库:[mix/database](https://github.com/mix-php/database#readme) ``` DB::instance() ``` -文档:[mix-php/database](https://github.com/mix-php/database#readme) - -- Redis +- Redis:[mix/redis](https://github.com/mix-php/redis#readme) ``` RDS::instance() ``` -文档:[mix-php/redis](https://github.com/mix-php/redis#readme) - -- 日志 +- 日志:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html) ``` Logger::instance() ``` -文档:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html) - -- 配置 +- 配置:[hassankhan/config](https://github.com/hassankhan/config#getting-values) ``` Config::instance() ``` -文档:[hassankhan/config](https://github.com/hassankhan/config#getting-values) - ## License Apache License Version 2.0, http://www.apache.org/licenses/ diff --git a/examples/grpc-skeleton/bin/cli.php b/examples/grpc-skeleton/bin/cli.php index 8f1d39ed..b74f47a4 100644 --- a/examples/grpc-skeleton/bin/cli.php +++ b/examples/grpc-skeleton/bin/cli.php @@ -2,14 +2,25 @@ require __DIR__ . '/../vendor/autoload.php'; use Dotenv\Dotenv; +use Mix\Cli\Cli; Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load(); define("APP_DEBUG", env('APP_DEBUG')); App\Error::register(); -switch ($argv[1]) { - case 'clearcache'; - (new \App\Command\ClearCache())->exec(); - break; -} +Cli::setName('app')->setVersion('0.0.0-alpha'); +$cmds = [ + new Mix\Cli\Command([ + 'name' => 'clearcache', + 'short' => 'Clear cache', + 'options' => [ + new Mix\Cli\Option([ + 'names' => ['k', 'key'], + 'usage' => 'Key name' + ]), + ], + 'run' => new App\Command\ClearCache(), + ]) +]; +Cli::addCommand(...$cmds)->run(); diff --git a/examples/grpc-skeleton/composer.json b/examples/grpc-skeleton/composer.json index bb0c0580..40dd942b 100644 --- a/examples/grpc-skeleton/composer.json +++ b/examples/grpc-skeleton/composer.json @@ -23,6 +23,7 @@ "workerman/workerman": "^4.0", "mix/vega": "~3.0", "mix/grpc": "~3.0", + "mix/cli": "~3.0", "mix/database": "~3.0", "mix/redis": "~3.0", "vlucas/phpdotenv": "^5.3", diff --git a/examples/grpc-skeleton/src/Command/ClearCache.php b/examples/grpc-skeleton/src/Command/ClearCache.php index ef2bb4cd..025bb761 100644 --- a/examples/grpc-skeleton/src/Command/ClearCache.php +++ b/examples/grpc-skeleton/src/Command/ClearCache.php @@ -3,17 +3,20 @@ namespace App\Command; use App\Container\RDS; +use Mix\Cli\Flag; +use Mix\Cli\RunInterface; /** * Class ClearCache * @package App\Command */ -class ClearCache +class ClearCache implements RunInterface { - public function exec(): void + public function main(): void { - RDS::instance()->del('foo_cache'); + $key = Flag::match('k', 'key')->string(); + RDS::instance()->del($key); print 'ok'; } diff --git a/examples/web-skeleton/README.md b/examples/web-skeleton/README.md index fa5cb8de..86d106d9 100644 --- a/examples/web-skeleton/README.md +++ b/examples/web-skeleton/README.md @@ -12,7 +12,7 @@ composer create-project --prefer-dist mix/web-skeleton web ## 快速开始 -启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 +启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 (零依赖) ``` composer run-script --timeout=0 cliserver:start @@ -139,7 +139,7 @@ $vega->handle('/', [new Hello(), 'index'])->methods('GET'); 路由里使用了 `Hello` 控制器,我们需要创建他 -- 如何配置路由:[mix-php/vega](https://github.com/mix-php/vega#readme) +- 如何配置路由:[mix/vega](https://github.com/mix-php/vega#readme) ```php load(); define("APP_DEBUG", env('APP_DEBUG')); App\Error::register(); -switch ($argv[1]) { - case 'clearcache'; - (new \App\Command\ClearCache())->exec(); - break; -} +Cli::setName('app')->setVersion('0.0.0-alpha'); +$cmds = [ + new Mix\Cli\Command([ + 'name' => 'clearcache', + 'short' => 'Clear cache', + 'options' => [ + new Mix\Cli\Option([ + 'names' => ['k', 'key'], + 'usage' => 'Key name' + ]), + ], + 'run' => new App\Command\ClearCache(), + ]) +]; +Cli::addCommand(...$cmds)->run(); diff --git a/examples/web-skeleton/composer.json b/examples/web-skeleton/composer.json index bf25e437..b7444529 100644 --- a/examples/web-skeleton/composer.json +++ b/examples/web-skeleton/composer.json @@ -22,6 +22,7 @@ "require": { "workerman/workerman": "^4.0", "mix/vega": "~3.0", + "mix/cli": "~3.0", "mix/database": "~3.0", "mix/redis": "~3.0", "vlucas/phpdotenv": "^5.3", diff --git a/examples/web-skeleton/src/Command/ClearCache.php b/examples/web-skeleton/src/Command/ClearCache.php index ef2bb4cd..025bb761 100644 --- a/examples/web-skeleton/src/Command/ClearCache.php +++ b/examples/web-skeleton/src/Command/ClearCache.php @@ -3,17 +3,20 @@ namespace App\Command; use App\Container\RDS; +use Mix\Cli\Flag; +use Mix\Cli\RunInterface; /** * Class ClearCache * @package App\Command */ -class ClearCache +class ClearCache implements RunInterface { - public function exec(): void + public function main(): void { - RDS::instance()->del('foo_cache'); + $key = Flag::match('k', 'key')->string(); + RDS::instance()->del($key); print 'ok'; } diff --git a/examples/websocket-skeleton/README.md b/examples/websocket-skeleton/README.md index 17287dd7..61c70f9d 100644 --- a/examples/websocket-skeleton/README.md +++ b/examples/websocket-skeleton/README.md @@ -81,8 +81,8 @@ $vega->handle('/websocket', [new WebSocket(), 'index'])->methods('GET'); 路由里使用了 `WebSocket` 控制器,我们需要创建他 -- 如何配置路由:[mix-php/vega](https://github.com/mix-php/vega#readme) -- 如何使用 WebSocket 升级器:[mix-php/websocket](https://github.com/mix-php/websocket#readme) +- 如何配置路由:[mix/vega](https://github.com/mix-php/vega#readme) +- 如何使用 WebSocket 升级器:[mix/websocket](https://github.com/mix-php/websocket#readme) ```php load(); define("APP_DEBUG", env('APP_DEBUG')); App\Error::register(); -switch ($argv[1]) { - case 'clearcache'; - (new \App\Command\ClearCache())->exec(); - break; -} +Cli::setName('app')->setVersion('0.0.0-alpha'); +$cmds = [ + new Mix\Cli\Command([ + 'name' => 'clearcache', + 'short' => 'Clear cache', + 'options' => [ + new Mix\Cli\Option([ + 'names' => ['k', 'key'], + 'usage' => 'Key name' + ]), + ], + 'run' => new App\Command\ClearCache(), + ]) +]; +Cli::addCommand(...$cmds)->run(); diff --git a/examples/websocket-skeleton/composer.json b/examples/websocket-skeleton/composer.json index 0914bd0d..7aef178d 100644 --- a/examples/websocket-skeleton/composer.json +++ b/examples/websocket-skeleton/composer.json @@ -19,6 +19,7 @@ "require": { "mix/vega": "~3.0", "mix/websocket": "~3.0", + "mix/cli": "~3.0", "mix/database": "~3.0", "mix/redis": "~3.0", "vlucas/phpdotenv": "^5.3", diff --git a/examples/websocket-skeleton/src/Command/ClearCache.php b/examples/websocket-skeleton/src/Command/ClearCache.php index ef2bb4cd..025bb761 100644 --- a/examples/websocket-skeleton/src/Command/ClearCache.php +++ b/examples/websocket-skeleton/src/Command/ClearCache.php @@ -3,17 +3,20 @@ namespace App\Command; use App\Container\RDS; +use Mix\Cli\Flag; +use Mix\Cli\RunInterface; /** * Class ClearCache * @package App\Command */ -class ClearCache +class ClearCache implements RunInterface { - public function exec(): void + public function main(): void { - RDS::instance()->del('foo_cache'); + $key = Flag::match('k', 'key')->string(); + RDS::instance()->del($key); print 'ok'; }