0

在 CakPHP 3.6.0中添加了控制台命令以长期替换 Shell 和任务。

我目前正在设计一个 cronjob 命令以在不同的时间间隔执行其他命令。所以我想从这样的 Command 类中运行一个命令:

namespace App\Command;
// ...

class CronjobCommand extends Command
{
    public function execute(Arguments $args, ConsoleIo $io)
    {
        // Run other command
    }
}

对于外壳/任务,可以使用Cake\Console\ShellDispatcher

$shell = new ShellDispatcher();
$output = $shell->run(['cake', $task]);

但这不适用于命令。由于我在文档中没有找到任何信息,有什么想法可以解决这个问题吗?

4

1 回答 1

1

您可以简单地实例化命令然后运行它,如下所示:

try {
    $otherCommand = new \App\Command\OtherCommand();
    $result = $otherCommand->run(['--foo', 'bar'], $io);
} catch (\Cake\Console\Exception\StopException $e) {
    $result = $e->getCode();
}

CakePHP 3.8 将引入一个方便的方法来帮助解决这个问题。引用即将发布的文档:

您可能需要从您的命令中调用其他命令。你可以用它 executeCommand来做到这一点::

// You can pass an array of CLI options and arguments.
$this->executeCommand(OtherCommand::class, ['--verbose', 'deploy']);

// Can pass an instance of the command if it has constructor args
$command = new OtherCommand($otherArgs);
$this->executeCommand($command, ['--verbose', 'deploy']);

另请参阅https://github.com/cakephp/cakephp/pull/13163

于 2019-06-06T21:40:06.920 回答