Invoking a symfony console command in another command and suppressing output

I have a simple console application using the symfony console component.

I have two commands (e.g. cmdOne and cmdTwo ) that you can easily call on your own.

 $ myApp.php cmdOne $ myApp.php cmdTwo 

Both commands have a significant amount of output, which I can easily disable by issuing the -q option.

Now I would like cmdOne call cmdTwo , but I would like cmdTwo be quiet. I am not trying to do something crazy, but I am struggling to get there, despite reading the documents.

Here is my sample code (this snippet will be contained in cmdOne->execute() ):

 $command = $this->getApplication()->find('cmdTwo'); $input = new ArrayInput(array( 'command' => 'cmdTwo', '-q' => true )); $returnCode = $command->run($input, $output); 

This works fine, as in the code command, but it displays on the console (generated cmdTwo ) that I would not want to show.

The -q option indicates is impossible because it is "reserved" (that is, dev was not created), or am I missing something obvious?

+5
source share
1 answer

Instead of passing the same instance of $output (the one that is output to your current console), create an instance of NullOutput

 $returnCode = $command->run($input, new \Symfony\Component\Console\Output\NullOutput); 

This is mainly a black hole - it takes an exit and quietly throws it away.

+6
source

Source: https://habr.com/ru/post/1210705/


All Articles