webman/console 命令行插件

webman/console 基于 symfony/console

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

安装

  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/Admin.php 例如 php webman make:controller api/user 将创建一个 app/api/controller/User.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:create

导出应用插件 例如 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 修改成如下
  1. <?php
  2. namespace app\command;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Helper\Table;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. class ConfigMySQLCommand extends Command
  8. {
  9. protected static $defaultName = 'config:mysql';
  10. protected static $defaultDescription = '显示当前MySQL服务器配置';
  11. protected function execute(InputInterface $input, OutputInterface $output)
  12. {
  13. $output->writeln('MySQL配置信息如下:');
  14. $config = config('database');
  15. $headers = ['name', 'default', 'driver', 'host', 'port', 'database', 'username', 'password', 'unix_socket', 'charset', 'collation', 'prefix', 'strict', 'engine', 'schema', 'sslmode'];
  16. $rows = [];
  17. foreach ($config['connections'] as $name => $db_config) {
  18. $row = [];
  19. foreach ($headers as $key) {
  20. switch ($key) {
  21. case 'name':
  22. $row[] = $name;
  23. break;
  24. case 'default':
  25. $row[] = $config['default'] == $name ? 'true' : 'false';
  26. break;
  27. default:
  28. $row[] = $db_config[$key] ?? '';
  29. }
  30. }
  31. if ($config['default'] == $name) {
  32. array_unshift($rows, $row);
  33. } else {
  34. $rows[] = $row;
  35. }
  36. }
  37. $table = new Table($output);
  38. $table->setHeaders($headers);
  39. $table->setRows($rows);
  40. $table->render();
  41. return self::SUCCESS;
  42. }
  43. }

测试

命令行运行 php webman config:mysql

结果类似如下:

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

更多资料参考

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