Skip to content

Commit 1f89e35

Browse files
committed
feat:examples:add mix/cli
1 parent 0166826 commit 1f89e35

File tree

16 files changed

+116
-88
lines changed

16 files changed

+116
-88
lines changed

examples/api-skeleton/README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ composer create-project --prefer-dist mix/api-skeleton api
1212

1313
## 快速开始
1414

15-
启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务
15+
启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 (零依赖)
1616

1717
```
1818
composer run-script --timeout=0 cliserver:start
@@ -139,8 +139,8 @@ $vega->handle('/users/{id}', [new Users(), 'index'])->methods('GET');
139139

140140
路由里使用了 `Users` 控制器,我们需要创建他
141141

142-
- 如何配置路由:[mix-php/vega](https://github.com/mix-php/vega#readme)
143-
- 如何调用数据库:[mix-php/database](https://github.com/mix-php/database#readme)
142+
- 如何配置路由:[mix/vega](https://github.com/mix-php/vega#readme)
143+
- 如何调用数据库:[mix/database](https://github.com/mix-php/database#readme)
144144

145145
```php
146146
<?php
@@ -195,38 +195,30 @@ curl http://127.0.0.1:9501/users/1
195195

196196
容器采用了一个简单的单例模式,你可以修改为更加适合自己的方式。
197197

198-
- 数据库
198+
- 数据库[mix/database](https://github.com/mix-php/database#readme)
199199

200200
```
201201
DB::instance()
202202
```
203203

204-
文档:[mix-php/database](https://github.com/mix-php/database#readme)
205-
206-
- Redis
204+
- Redis:[mix/redis](https://github.com/mix-php/redis#readme)
207205

208206
```
209207
RDS::instance()
210208
```
211209

212-
文档:[mix-php/redis](https://github.com/mix-php/redis#readme)
213-
214-
- 日志
210+
- 日志:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)
215211

216212
```
217213
Logger::instance()
218214
```
219215

220-
文档:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)
221-
222-
- 配置
216+
- 配置:[hassankhan/config](https://github.com/hassankhan/config#getting-values)
223217

224218
```
225219
Config::instance()
226220
```
227221

228-
文档:[hassankhan/config](https://github.com/hassankhan/config#getting-values)
229-
230222
## License
231223

232224
Apache License Version 2.0, http://www.apache.org/licenses/

examples/api-skeleton/bin/cli.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
require __DIR__ . '/../vendor/autoload.php';
33

44
use Dotenv\Dotenv;
5+
use Mix\Cli\Cli;
56

67
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
78
define("APP_DEBUG", env('APP_DEBUG'));
89

910
App\Error::register();
1011

11-
switch ($argv[1]) {
12-
case 'clearcache';
13-
(new \App\Command\ClearCache())->exec();
14-
break;
15-
}
12+
Cli::setName('app')->setVersion('0.0.0-alpha');
13+
$cmds = [
14+
new Mix\Cli\Command([
15+
'name' => 'clearcache',
16+
'short' => 'Clear cache',
17+
'options' => [
18+
new Mix\Cli\Option([
19+
'names' => ['k', 'key'],
20+
'usage' => 'Key name'
21+
]),
22+
],
23+
'run' => new App\Command\ClearCache(),
24+
])
25+
];
26+
Cli::addCommand(...$cmds)->run();

examples/api-skeleton/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"require": {
2323
"workerman/workerman": "^4.0",
2424
"mix/vega": "~3.0",
25+
"mix/cli": "~3.0",
2526
"mix/database": "~3.0",
2627
"mix/redis": "~3.0",
2728
"vlucas/phpdotenv": "^5.3",

examples/api-skeleton/src/Command/ClearCache.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
namespace App\Command;
44

55
use App\Container\RDS;
6+
use Mix\Cli\Flag;
7+
use Mix\Cli\RunInterface;
68

79
/**
810
* Class ClearCache
911
* @package App\Command
1012
*/
11-
class ClearCache
13+
class ClearCache implements RunInterface
1214
{
1315

14-
public function exec(): void
16+
public function main(): void
1517
{
16-
RDS::instance()->del('foo_cache');
18+
$key = Flag::match('k', 'key')->string();
19+
RDS::instance()->del($key);
1720
print 'ok';
1821
}
1922

examples/grpc-skeleton/README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,44 +135,36 @@ composer run-script swoole:start
135135

136136
## 如何使用 gRPC 客户端
137137

138-
- [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)
138+
- [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)
139139

140140
## 使用容器中的对象
141141

142142
容器采用了一个简单的单例模式,你可以修改为更加适合自己的方式。
143143

144-
- 数据库
144+
- 数据库[mix/database](https://github.com/mix-php/database#readme)
145145

146146
```
147147
DB::instance()
148148
```
149149

150-
文档:[mix-php/database](https://github.com/mix-php/database#readme)
151-
152-
- Redis
150+
- Redis:[mix/redis](https://github.com/mix-php/redis#readme)
153151

154152
```
155153
RDS::instance()
156154
```
157155

158-
文档:[mix-php/redis](https://github.com/mix-php/redis#readme)
159-
160-
- 日志
156+
- 日志:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)
161157

162158
```
163159
Logger::instance()
164160
```
165161

166-
文档:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)
167-
168-
- 配置
162+
- 配置:[hassankhan/config](https://github.com/hassankhan/config#getting-values)
169163

170164
```
171165
Config::instance()
172166
```
173167

174-
文档:[hassankhan/config](https://github.com/hassankhan/config#getting-values)
175-
176168
## License
177169

178170
Apache License Version 2.0, http://www.apache.org/licenses/

examples/grpc-skeleton/bin/cli.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
require __DIR__ . '/../vendor/autoload.php';
33

44
use Dotenv\Dotenv;
5+
use Mix\Cli\Cli;
56

67
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
78
define("APP_DEBUG", env('APP_DEBUG'));
89

910
App\Error::register();
1011

11-
switch ($argv[1]) {
12-
case 'clearcache';
13-
(new \App\Command\ClearCache())->exec();
14-
break;
15-
}
12+
Cli::setName('app')->setVersion('0.0.0-alpha');
13+
$cmds = [
14+
new Mix\Cli\Command([
15+
'name' => 'clearcache',
16+
'short' => 'Clear cache',
17+
'options' => [
18+
new Mix\Cli\Option([
19+
'names' => ['k', 'key'],
20+
'usage' => 'Key name'
21+
]),
22+
],
23+
'run' => new App\Command\ClearCache(),
24+
])
25+
];
26+
Cli::addCommand(...$cmds)->run();

examples/grpc-skeleton/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"workerman/workerman": "^4.0",
2424
"mix/vega": "~3.0",
2525
"mix/grpc": "~3.0",
26+
"mix/cli": "~3.0",
2627
"mix/database": "~3.0",
2728
"mix/redis": "~3.0",
2829
"vlucas/phpdotenv": "^5.3",

examples/grpc-skeleton/src/Command/ClearCache.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
namespace App\Command;
44

55
use App\Container\RDS;
6+
use Mix\Cli\Flag;
7+
use Mix\Cli\RunInterface;
68

79
/**
810
* Class ClearCache
911
* @package App\Command
1012
*/
11-
class ClearCache
13+
class ClearCache implements RunInterface
1214
{
1315

14-
public function exec(): void
16+
public function main(): void
1517
{
16-
RDS::instance()->del('foo_cache');
18+
$key = Flag::match('k', 'key')->string();
19+
RDS::instance()->del($key);
1720
print 'ok';
1821
}
1922

examples/web-skeleton/README.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ composer create-project --prefer-dist mix/web-skeleton web
1212

1313
## 快速开始
1414

15-
启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务
15+
启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 (零依赖)
1616

1717
```
1818
composer run-script --timeout=0 cliserver:start
@@ -139,7 +139,7 @@ $vega->handle('/', [new Hello(), 'index'])->methods('GET');
139139

140140
路由里使用了 `Hello` 控制器,我们需要创建他
141141

142-
- 如何配置路由:[mix-php/vega](https://github.com/mix-php/vega#readme)
142+
- 如何配置路由:[mix/vega](https://github.com/mix-php/vega#readme)
143143

144144
```php
145145
<?php
@@ -196,38 +196,30 @@ curl http://127.0.0.1:9501/
196196

197197
容器采用了一个简单的单例模式,你可以修改为更加适合自己的方式。
198198

199-
- 数据库
199+
- 数据库[mix/database](https://github.com/mix-php/database#readme)
200200

201201
```
202202
DB::instance()
203203
```
204204

205-
文档:[mix-php/database](https://github.com/mix-php/database#readme)
206-
207-
- Redis
205+
- Redis:[mix/redis](https://github.com/mix-php/redis#readme)
208206

209207
```
210208
RDS::instance()
211209
```
212210

213-
文档:[mix-php/redis](https://github.com/mix-php/redis#readme)
214-
215-
- 日志
211+
- 日志:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)
216212

217213
```
218214
Logger::instance()
219215
```
220216

221-
文档:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)
222-
223-
- 配置
217+
- 配置:[hassankhan/config](https://github.com/hassankhan/config#getting-values)
224218

225219
```
226220
Config::instance()
227221
```
228222

229-
文档:[hassankhan/config](https://github.com/hassankhan/config#getting-values)
230-
231223
## License
232224

233225
Apache License Version 2.0, http://www.apache.org/licenses/

examples/web-skeleton/bin/cli.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
require __DIR__ . '/../vendor/autoload.php';
33

44
use Dotenv\Dotenv;
5+
use Mix\Cli\Cli;
56

67
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
78
define("APP_DEBUG", env('APP_DEBUG'));
89

910
App\Error::register();
1011

11-
switch ($argv[1]) {
12-
case 'clearcache';
13-
(new \App\Command\ClearCache())->exec();
14-
break;
15-
}
12+
Cli::setName('app')->setVersion('0.0.0-alpha');
13+
$cmds = [
14+
new Mix\Cli\Command([
15+
'name' => 'clearcache',
16+
'short' => 'Clear cache',
17+
'options' => [
18+
new Mix\Cli\Option([
19+
'names' => ['k', 'key'],
20+
'usage' => 'Key name'
21+
]),
22+
],
23+
'run' => new App\Command\ClearCache(),
24+
])
25+
];
26+
Cli::addCommand(...$cmds)->run();

0 commit comments

Comments
 (0)