菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
17
0

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

原创
05/13 14:22
阅读数 941

因为最近想写个自己的小东西,准备写个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 协议》,转载必须注明作者和本文链接

发表评论

0/200
17 点赞
0 评论
收藏
为你推荐 换一批