欢迎光临!请记住我的域名:http://wish123.cnblogs.com
代码改变世界

thinkphp6给命令行添加develop和product环境变量

2022-07-01 12:01  wish123  阅读(252)  评论(0编辑  收藏  举报

thinkphp6给命令行添加develop和product环境变量的方法如下:

一、think 文件中代码改为:

$cmdLine = implode(' ', $argv);
if(strpos($cmdLine, '--env') && (strpos($cmdLine, 'dev') || strpos($cmdLine, 'develop'))) {
    define('ENV', 'develop');
    (new App())->setEnvName(ENV)->console->run();
} else {
    define('ENV', 'product');
    (new App())->console->run();
}

 

二、app下增加app/Console.php文件,Console.php文件内容如下:

<?php
namespace app;
use think\console\input\Argument as InputArgument;
use think\console\input\Definition as InputDefinition;
use think\console\input\Option as InputOption;
class Console extends \think\Console
{
    protected function getDefaultInputDefinition(): InputDefinition
    {
        return new InputDefinition([
            new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
            new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
            new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this console version'),
            new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
            new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
            new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
            new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
            new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
            new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
            new InputOption('--env', '-env', InputOption::VALUE_OPTIONAL, 'Environment variable settings'),
        ]);
    }
}

三、app\provider.php文件中增加'console'=> Console::class, 如下:

return [
    'think\Request'          => Request::class,
    'think\exception\Handle' => ExceptionHandle::class,
    'console'                => Console::class,
];

四、使用

php think your-ommand --env=dev
php think queue:work --queue=yourQueue --env dev