天天看點

tp5自定義指令

建立自定義指令行:

1.首先是注冊cammand:

application/cammand:

<?php
return [
    'app\common\command\Chat',
];      

2.建立類,繼承Cammand:

<?php


namespace app\common\command;


use think\console\Command;

class Chat extends Command
{
    protected function configure()
    {
        $this->setName('chat')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln("TestCommand:");
    }

}      

下面說一下參數的含義

首先configure 和execute這兩個是必須存在的.

configure是配置資訊:

其中setName 辨別的php  think 後面緊跟的指令名

自定義指令行還支援傳遞參數和可選選項

php  think chat start  --restart      

其中start為參數,必須要輸入的,在configure中配置為:

->addArgument('number1')

執行的是時候調用execute裡面的

$input->getArgument('number1')  擷取使用者輸入的參數,

setDescription: 這個是執行php think list 展示的簡述,類似的還有

->setHelp("這個是使用--help時展示的資訊");      

getArgument()方法是有預設值的,不止一個參數,下面是tp的架構代碼

/**
     * 添加參數
     * @param string $name        名稱
     * @param int    $mode        類型,
     * @param string $description 描述
     * @param mixed  $default     預設值
     * @return Command
     */
    public function addArgument($name, $mode = null, $description = '', $default = null)
    {
        $this->definition->addArgument(new Argument($name, $mode, $description, $default));

        return $this;
    }      

其中$mode:

參數類型: self::REQUIRED 或者 self::OPTIONAL

required常量值為1,

optional 常量值2

他們辨別的是是否必須

示例:

->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload", 'start')      

其中action 作為execute擷取參數的鍵, Argument::OPTIONAL是$mode的值,表示非必填項,第三個是提示描述,最後一個是預設值,預設傳遞的start

下面是可以選選項的使用

在configure方法裡:

->addOption('restart')

在執行execute裡通過

if($input->hasOption('restart')){

  //執行裡面的裡面的邏輯

}

同理下面是tp架構的代碼:

/**
     * 添加選項
     * @param string $name        選項名稱
     * @param string $shortcut    别名
     * @param int    $mode        類型
     * @param string $description 描述
     * @param mixed  $default     預設值
     * @return Command
     */
    public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
    {
        $this->definition->addOption(new Option($name, $shortcut, $mode, $description, $default));

        return $this;
    }      

示例:

->addOption('daemon', 'd', Option::VALUE_NONE, '該程序已背景運作')      

daemon 是execute擷取options時的鍵,d,是别名

同理$mode辨別的是是否必須,

第三個參數是描述

第四個是預設值,

隻有第一個是必須的參數,其他均為可選,跟addArguments一緻.

在指令行輸出資料:

下面是一些可以使用顔色的輸出

// 紅色背景上的白字
$output->writeln('<error>white text on a red background</error>');

// 綠字
$output->writeln('<info>green text</info>');
 
// 黃字
$output->writeln('<comment>yellow text</comment>');

// 黃色色背景上的黑字
$output->writeln('<warning>black text on a yellow background</warning>');
 
// 青色背景上的黑字
$output->writeln('<question>black text on a cyan background</question>');
 
// 紅背景上的白字
$output->writeln('<error>white text on a red background</error>');

// 支援混合輸出
$output->writeln('<info>green text</info><question>black text on a cyan background</question>......');      

通過控制器去觸發指令行操作

// 調用指令行的指令
$output = Console::call('app:demo', ['--num', '10', 'kitty']);      

tp指令行還可以做一些簡單的判斷

//execute方法内
$question = $output->confirm($input, '是否繼續操作?', false);
 if (!$question) {
            return;
 }      

轉載于:https://www.cnblogs.com/callmelx/p/11529152.html

php