zoukankan      html  css  js  c++  java
  • Hyperf 命令行

    创建命令

    php bin/hyperf.php gen:command FooCommand

    可以看到在App/Command目录下生成文件 FooCommand.php 文件

    parent::__construct('demo:command')
    设置当前命令,在命令行运行改命令的指令为:
    php bin/hyperf.php gen:command demo:command

    configure方法永不对命令做基础的设置,如参数的设置,命令的描述等
    handle 方法 具体的业务处理逻辑
    <?php
    
    declare(strict_types=1);
    
    namespace AppCommand;
    
    use HyperfCommandCommand as HyperfCommand;
    use HyperfCommandAnnotationCommand;
    use PsrContainerContainerInterface;
    
    /**
     * @Command
     */
    class FooCommand extends HyperfCommand
    {
        /**
         * @var ContainerInterface
         */
        protected $container;
    
        public function __construct(ContainerInterface $container)
        {
            $this->container = $container;
    
            parent::__construct('demo:command');
        }
    
        public function configure()
        {
            parent::configure();
            $this->setDescription('Hyperf Demo Command');
        }
    
        public function handle()
        {
            $this->line('Hello Hyperf!', 'info');
        }
    }

    输入常用方法

    1、利用addArgument和getArgument 设置和获取参数

    public function __construct(ContainerInterface $container)
        {
            $this->container = $container;
    
            parent::__construct('foo:test');
        }
    
        public function configure()
        {
            parent::configure();
            $this->setDescription('Hyperf Demo Command');
            /**
             * 添加一个参数
             * @param string               $name        参数名
             * @param int|null             $mode        参数模式: InputArgument::REQUIRED 必填 or InputArgument::OPTIONAL 可选
             * @param string               $description 描述
             * @param string|string[]|null $default     默认值 (仅在可选模式下)
             */
            $this->addArgument('name',InputArgument::OPTIONAL,'名字','Hyperf');
            $this->addArgument('name2',InputArgument::OPTIONAL,'名字2','Hyperf2');
        }
    
        public function handle()
        {
            /**
             * 获取参数
             */
            $name = $this->input->getArgument("name");
            $name2 = $this->input->getArgument("name2");
            $this->line(sprintf('Hello %s %s!',$name,$name2), 'info');
        }

    执行命令

     php bin/hyperf.php foo:test World1 World2

    得到结果

    Hello World1 World2!

    2、利用addOption和getOption 设置和获取参数

    public function configure()
        {
            parent::configure();
            $this->setDescription('Hyperf Demo Command');
            /**
             * @param string                        $name        The option name
             * @param string|array|null             $shortcut    The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
             * @param int|null                      $mode        The option mode: One of the InputOption::VALUE_* constants
             * @param string                        $description A description text
             * @param string|string[]|int|bool|null $default     The default value (must be null for InputOption::VALUE_NONE)
             */
            $this->addOption('name','n1',InputOption::VALUE_OPTIONAL,'名字','Hyperf');
            //InputOption::VALUE_IS_ARRAY必须指定其模式是必选 InputOption::VALUE_REQUIRED 还是可选 InputOption::VALUE_OPTIONAL
            $this->addOption('name2','n2',InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,'名字2',["波波","bobo"]);
        }
    
        public function handle()
        {
            /**
             * 获取参数
             */
            $name = $this->input->getOption("name");
            $name2 = $this->input->getOption("name2");
            $this->line(sprintf('Hello %s %s!',$name,implode(",",$name2)), 'info');
        }

    执行命令

    php bin/hyperf.php foo:test --name World1 --name2 World2 --name2 World3
    # 或者
     php bin/hyperf.php foo:test --name=World1 --name2=World2 --name2=World3

    结果

    Hello World1 World2,World3!

    如果参数中有空格可以使用 ""

     php bin/hyperf.php foo:test --name "Worl  d1" --name2 World2 --name2 World3

    结果

    Hello Worl  d1 World2,World3!

    输出常用方法

    public function configure()
        {
            parent::configure();
            $this->setDescription('Hyperf Demo Command');
        }
    
        public function handle()
        {
            //换行输出
            $this->output->writeln("Hello World");
            //错误
            $this->output->writeln("<error>Hello</error> World");
            //输入
            $vaule = $this->ask("请输入信息:","Hyperf");
            $this->output->writeln($vaule);
            //选择
            $vaule = $this->output->choice("请选择颜色:",["黄色","绿色"]);
            $this->output->writeln($vaule);
            //确认
            $vaule = $this->output->confirm("确认要继续吗",false);
            $this->output->writeln((int)$vaule);
            //进度条
            $this->output->progressStart(100);
            for ($i=1;$i<=10;$i++){
                $this->output->progressAdvance(10);
                sleep(1);
            }
            $this->output->writeln("Finish");
        }

    执行命令

     php bin/hyperf.php foo:test

    得到结果

    其他

    //是否在协程下执行
        public $coroutine=true;
     
  • 相关阅读:
    在IIS中浏览网站时出现:无法打开登录所请求的数据库 "***",登录失败
    Java中的深拷贝和浅拷贝(转载)
    Java的Final和C#的Const,Readonly比较分析(转载)
    C#中的Sealed和J#中的Final比较(转载)
    Java全系列帮助文档下载
    The Willpower Instinct(自控力,意志力)
    瓦片地图的前世今生(转载)
    创建CUDA项目
    CUDA warning C4819的消除
    并行调用
  • 原文地址:https://www.cnblogs.com/bobobobobo/p/13139841.html
Copyright © 2011-2022 走看看