2016-12-25 23 views

答えて

3

How to Call Other Commands上のドキュメントを参照してください:

は別のものからのコマンドを呼び出すには簡単です:あなたは、コマンド名を渡すことで実行したい

use Symfony\Component\Console\Input\ArrayInput; 
// ... 

protected function execute(InputInterface $input, OutputInterface $output) 
{ 
    $command = $this->getApplication()->find('demo:greet'); 

    $arguments = array(
     'command' => 'demo:greet', 
     'name' => 'Fabien', 
     '--yell' => true, 
    ); 

    $greetInput = new ArrayInput($arguments); 
    $returnCode = $command->run($greetInput, $output); 

    // ... 
} 

まず、あなたfind()コマンド。次に、コマンドに渡す引数とオプションを指定して、新しいArrayInputを作成する必要があります。

最終的にrun()メソッドを呼び出すと、実際にコマンドが実行され、返されたコードがコマンドから返されます(コマンドの​​メソッドの戻り値)。

2

コマンドを見つけて、それらを実行、アプリケーションのインスタンスを取得します:

protected function configure() 
{ 
    $this->setName('clean'); 
} 

protected function execute(InputInterface $input, OutputInterface $output) 
{ 
    $app = $this->getApplication(); 

    $cleanRedisKeysCmd = $app->find('clean-redis-keys'); 
    $cleanRedisKeysInput = new ArrayInput([]); 

    $cleanTempFilesCmd = $app->find('clean-temp-files'); 
    $cleanTempFilesInput = new ArrayInput([]); 

    // Note if "subcommand" returns an exit code, run() method will return it. 
    $cleanRedisKeysCmd->run($cleanRedisKeysInput, $output); 
    $cleanTempFilesCmd->run($cleanTempFilesInput, $output); 
} 

コードの重複を回避するには、サブコマンドを呼び出すための汎用的なメソッドを作成することができます。このようなもの:

private function executeSubCommand(string $name, array $parameters, OutputInterface $output) 
{ 
    return $this->getApplication() 
     ->find($name) 
     ->run(new ArrayInput($parameters), $output); 
} 
関連する問題