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 App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;

/**
 * @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;

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM