Skip to content

Commit

Permalink
feat:examples:add mix/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Aug 5, 2021
1 parent 0166826 commit 1f89e35
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 88 deletions.
22 changes: 7 additions & 15 deletions examples/api-skeleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
<?php
Expand Down Expand Up @@ -195,38 +195,30 @@ curl http://127.0.0.1:9501/users/1

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

- 数据库
- 数据库[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/
21 changes: 16 additions & 5 deletions examples/api-skeleton/bin/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
1 change: 1 addition & 0 deletions examples/api-skeleton/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions examples/api-skeleton/src/Command/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
18 changes: 5 additions & 13 deletions examples/grpc-skeleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
21 changes: 16 additions & 5 deletions examples/grpc-skeleton/bin/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
1 change: 1 addition & 0 deletions examples/grpc-skeleton/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions examples/grpc-skeleton/src/Command/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
20 changes: 6 additions & 14 deletions examples/web-skeleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
<?php
Expand Down Expand Up @@ -196,38 +196,30 @@ curl http://127.0.0.1:9501/

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

- 数据库
- 数据库[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/
21 changes: 16 additions & 5 deletions examples/web-skeleton/bin/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
1 change: 1 addition & 0 deletions examples/web-skeleton/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions examples/web-skeleton/src/Command/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
Loading

0 comments on commit 1f89e35

Please sign in to comment.