webman/console 命令行插件

webman/console 基於 symfony/console

插件需要webman>=1.2.2 webman-framework>=1.2.1

安裝

composer require webman/console

支援的命令

使用方法
php webman 命令 或者 php webman 命令
例如 php webman version 或者 php webman version

支援的命令

version

打印webman版本號

route:list

打印當前路由配置

make:controller

創建一個控制器文件
例如 php webman make:controller admin 將創建一個 app/controller/AdminController.php
例如 php webman make:controller api/user 將創建一個 app/api/controller/UserController.php

make:model

創建一個model文件
例如 php webman make:model admin 將創建一個 app/model/Admin.php
例如 php webman make:model api/user 將創建一個 app/api/model/User.php

make:middleware

創建一個中間件文件
例如 php webman make:middleware Auth 將創建一個 app/middleware/Auth.php

make:command

創建自定義命令文件
例如 php webman make:command db:config 將創建一個 app\command\DbConfigCommand.php

plugin:create

創建一個基礎插件
例如 php webman plugin:create --name=foo/admin 將創建config/plugin/foo/adminvendor/foo/admin 兩個目錄
參見創建基礎插件

plugin:export

導出基礎插件
例如 php webman plugin:export --name=foo/admin
參見創建基礎插件

plugin:export

導出應用插件
例如 php webman plugin:export shop
參見應用插件

phar:pack

將webman項目打包成phar文件
參見phar打包

此特性需要webman>=1.2.4 webman-framework>=1.2.4 webman\console>=1.0.5

自定義命令

用戶可以定義自己的命令,例如以下是打印數據庫配置的命令

  • 執行 php webman make:command config:mysql
  • 打開 app/command/ConfigMySQLCommand.php 修改成如下
<?php

namespace app\command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigMySQLCommand extends Command
{
    protected static $defaultName = 'config:mysql';
    protected static $defaultDescription = '顯示當前MySQL伺服器配置';

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('MySQL配置信息如下:');
        $config = config('database');
        $headers = ['name', 'default', 'driver', 'host', 'port', 'database', 'username', 'password', 'unix_socket', 'charset', 'collation', 'prefix', 'strict', 'engine', 'schema', 'sslmode'];
        $rows = [];
        foreach ($config['connections'] as $name => $db_config) {
            $row = [];
            foreach ($headers as $key) {
                switch ($key) {
                    case 'name':
                        $row[] = $name;
                        break;
                    case 'default':
                        $row[] = $config['default'] == $name ? 'true' : 'false';
                        break;
                    default:
                        $row[] = $db_config[$key] ?? '';
                }
            }
            if ($config['default'] == $name) {
                array_unshift($rows, $row);
            } else {
                $rows[] = $row;
            }
        }
        $table = new Table($output);
        $table->setHeaders($headers);
        $table->setRows($rows);
        $table->render();
        return self::SUCCESS;
    }
}

測試

命令行執行 php webman config:mysql

結果類似如下:

+-------+---------+--------+-----------+------+----------+----------+----------+-------------+---------+-----------------+--------+--------+--------+--------+---------+
| name  | default | driver | host      | port | database | username | password | unix_socket | charset | collation       | prefix | strict | engine | schema | sslmode |
+-------+---------+--------+-----------+------+----------+----------+----------+-------------+---------+-----------------+--------+--------+--------+--------+---------+
| mysql | true    | mysql  | 127.0.0.1 | 3306 | mysql    | root     | ******   |             | utf8    | utf8_unicode_ci |        | 1      |        |        |         |
+-------+---------+--------+-----------+------+----------+----------+----------+-------------+---------+-----------------+--------+--------+--------+--------+---------+

更多資料參考

http://www.symfonychina.com/doc/current/components/console.html