1.symfony 组件库学习之 symfony/console

因为最近想写个自己的小东西,准备写个console程序生成curd常用的代码所以找到了symfony/console组件,以下是对该组件的一点简单的学习记录

一.下载和简单使用

1. 下载

composer require symfony/console @stable

2. 创建TestCmd类

我的类是建立在 App\Console\TestCmd 下的,代码如下

<?php
namespace App\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TestCmd extends Command
{
    public function __construct($name = null)
    {
        parent::__construct($name);
    }
    protected function configure()
    {
        $this
            // 命令的名称 
            ->setName('test')
            // 简短描述
            ->setDescription('Create curd')
            // 运行命令时使用 "--help" 选项时的完整命令描述
            ->setHelp('Create curd')
            //->addArgument('optional_argument', InputArgument::REQUIRED, 'this is a optional argument');
            ->setDefinition([
                new InputArgument('name', InputArgument::REQUIRED, 'controller,service,model 名称'),
                new InputOption('controller', 'c', InputOption::VALUE_NONE, '生成controller'),
                new InputOption('service', 's', InputOption::VALUE_NONE, '生成Service'),
                new InputOption('model', 'm', InputOption::VALUE_NONE, '生成model'),
                new InputOption('no-to-cc', 'no-tcc', InputOption::VALUE_NONE, '不转换name为大驼峰'),
            ]); 
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        //打印
        $output->writeln([
            'gen model',
        ]);
        $output->writeln('name: ' . $this->toCamelCase($input->getArgument('name'), true));
        $output->writeln('controller: ' . $input->getOption('controller'));
        $output->writeln('service: ' . $input->getOption('service'));
        $output->writeln('model: ' . $input->getOption('model'));
        $output->writeln('no-to-cc: ' . $input->getOption('no-to-cc'));
        return 1;
    }
    //下划线命名到驼峰命名
    function toCamelCase($str, $toOne = false)
    {
        $array = explode('_', $str);
        $result = $toOne ? ucfirst($array[0]) : $array[0];
        $len = count($array);
        if ($len > 1) {
            for ($i = 1; $i < $len; $i++) {
                $result .= ucfirst($array[$i]);
            }
        }
        return $result;
    }
}

配置一个参数的两种方式

  1. setDefinition 以数组方式配置参数
new InputArgument 配置参数   参数顺序为 : 参数name   模式mode  描述   默认值
new InputOption 配置选项     参数顺序为 : 参数name   是否有短参数 如 --service 可配置 -s 模式mode  描述
  1. addArgument 配置单个参数
常用模式
mode:
           InputArgument::OPTIONAL 可选参数   php console.php   -s  Test
           InputArgument::REQUIRED 必传参数  php console.php   --xx Test
           InputArgument::VALUE_NONE 可选不用传值 不能有默认值 -x
           InputArgument::VALUE_REQUIRED 可选 使用了必须传值  如: --xx 值
           InputArgument::VALUE_OPTIONAL 可选 可传值可不传

2. 在项目根目录建立console.php文件

    <?php 
    require __DIR__.'/vendor/autoload.php';
    use App\Console\TestCmd;
    use Symfony\Component\Console\Application;

    $application = new Application();
    $application->add(new TestCmd());
    $application->run();

the end,以后用熟悉在修改

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!