1

How run a command as command line from a symfony function?

E.g.

C:\SymfonyProject> php app/console my-command --option=my-option

I want to run this command from a function. This command export files from a database and place this files in app/Resource/translations folder from Symfony Project.

I.e.

public function exportFiles(){ // I want to run command here. }

Thanks! :)

  • Possible duplicate of [How can I run symfony 2 run command from controller](http://stackoverflow.com/questions/10497567/how-can-i-run-symfony-2-run-command-from-controller) – eXe Jul 25 '16 at 15:27

1 Answers1

2

You could use the Symfony Process component for that. The code would look something like this:

private function process(OutputInterface $output)
{
    $cmd = 'php app/console my-command --option=my-option';

    $process = new Process($cmd);
    $process->setTimeout(60);

    $process->run(
        function ($type, $buffer) use ($output) {
            $output->write((Process::ERR === $type) ? 'ERR:' . $buffer : $buffer);
        }
);
muffe
  • 2,285
  • 1
  • 19
  • 23
  • $output where is defined? It can be a simple file? –  Jul 25 '16 at 15:28
  • Ah, missed that while copy pasting. $output is a OutputInterface in this case (was running this code in another Symfony Command). But obviously you could just log to a file or whatever you want to do in that case. – muffe Jul 25 '16 at 15:40
  • "Could not open input file: app/console" This error appear in $output file Path problem? –  Jul 25 '16 at 15:50
  • Finish: $cmd = 'php ../app/my-command --option=my-option'; Path problem :D Thank you so much! –  Jul 25 '16 at 16:01