2

i faced a problem when upgrade from 2.1 to 2.2

in my action controller i am calling a console command and get the output from the command like this.

$input = new ArgvInput(array(
                                'object_id' => $object_id,
                                'client_id' => $client_id,
                                'email_address' => $email
                                )
                           );

    $output = new ConsoleOutput();

    $command = $this->get('mycommand');
    $returnCode = $command->run($input, $output);

    $response = stream_get_contents($output->getStream());

it worked in symfony 2.1, but after upgrading to 2.2 first i got the following exception "Not enough arguments.". To prevent this i have added a dummy parameter in front of others.

But after this the command executes, but i cant read the output, it always empty.

Is there any solution for this?

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
user1112057
  • 180
  • 3
  • 11

2 Answers2

4

The Symfony 2.4 branch added a BufferedOutput which does exactly what you want.

    $input = new ArgvInput(array());
    $output = new BufferedOutput();

    $command = $this->get("command");
    $command->run($input, $output);

    $content = $output->fetch();
Chris
  • 460
  • 10
  • 22
2

I found the following gist which replaces the ConsoleOutput with the following MemoryWriter class to solve the problem.

It also suggests using the Symfony\Bundle\FrameworkBundle\Console\Application class to avoid having to create the command as a service:

$application = new Application($this->getContainer()->get('kernel'));
$application->setAutoExit(false); 

// The input interface should contain the command name, and whatever arguments the command needs to run      
$input = new ArrayInput(array("doctrine:schema:update"));

// Run the command
$retval = $application->run($input, $output);

var_dump($output->getOutput());
vanrijm
  • 21
  • 2