webman命令行

v1.3.6 版本
2024-04-10 版本更新时间
57553 安装
23 star

说明

webman命令行工具。symfony/console

安装

composer require webman/console

使用

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

支持的命令

version

打印webman版本号

route:list

打印当前路由配置

make:controller

创建一个控制器文件
例如 ./webman make:controller admin 将创建一个 app/controller/Admin.php
例如 ./webman make:controller api/user 将创建一个 app/api/controller/User.php

make:model

创建一个model文件
例如 ./webman make:model admin 将创建一个 app/model/Admin.php
例如 ./webman make:model api/user 将创建一个 app/api/model/User.php

make:middleware

创建一个中间件文件
例如 ./webman make:middleware Auth 将创建一个 app/middleware/Auth.php

make:command

创建自定义命令文件
例如 ./webman make:command db:config 将创建一个 app\command\DbConfigCommand.php

plugin:create

创建一个插件项目
例如 ./webman plugin:create foo/admin 将创建config/plugin/foo/adminvendor/foo/admin 两个目录
参见创建插件

plugin:export

导出插件项目
例如 ./webman plugin:export foo/admin
参见创建插件

自定义命令

用户可以定义自己的命令,例如以下是打印数据库配置的命令

  • 执行 ./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