2016-12-25 4 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() фактически выполняет команду и возвращает возвращенный код из команды (возвращаемое значение из метода команды execute()).

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); 
} 
Смежные вопросы