2

I want to execute the command fos:elastica:populate from my controller.

I tried that code but it doesn't work, i get error = 1 the var_dump show ""

$command = 'fos:elastica:populate';
$app = new Application($this->get('kernel'));
$app->setAutoExit(false);
$input = new StringInput($command);
$output = new ConsoleOutput;
$error = $app->run($input, $output);
var_dump($error);
var_dump(stream_get_contents($output->getStream());

Any ideas ?

I try a different code.....

    $command = $this->get('FosElasticaPopulateService');
    $input = new StringInput('');

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

    if(!$retval)
    {
        echo "Command executed successfully!\n";
    }
    else
    {
        echo "Command was not successful.\n";
    }
    var_dump(stream_get_contents($output->getStream()));

It say: 'The "no-interaction" option does not exist.' at Input ->getOption ('no-interaction') in the PopulateCommand.

if i change my code with:
$input = new StringInput('--no-interaction');

It say: 'The "--no-interaction" option does not exist.' at
'ArgvInput ->addLongOption ('no-interaction', null) '

rene
  • 41,474
  • 78
  • 114
  • 152
Sancho
  • 1,288
  • 2
  • 25
  • 50

3 Answers3

3

Step by step, how to run cache clear command: app/console cac:cle --env=(current_env) from controller.

At first, make sure to register command as service (service.yml):

xxx.cache.clear:
    class: Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand
    calls:
        - [setContainer, ["@service_container"] ]

Conde in your controller:

$command = $this->container->get('xxx.cache.clear');
$input = new ArgvInput(array('--env=' . $this->container->getParameter('kernel.environment')));
$output = new ConsoleOutput();
$command->run($input, $output);

That's all. | tested on symfony 2.4

pomaxa
  • 1,740
  • 16
  • 26
  • With Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand ot work (I just have one error with a session active but never mind. With FOS\ElasticaBundle\Command\PopulateCommand, it still say The "no-interaction" option does not exist. Is there a way to give the option to FOS ? – Sancho Mar 20 '14 at 15:48
  • @Sancho, all the options you'd like to pass, you need to add into $input var. so it will be like ```$input = new ArgvInput(array('--env=' . $this->container->getParameter('kernel.environment'), '-n')); – pomaxa Mar 20 '14 at 16:24
2

First of all: check if that command is already registered as a service. If not register it yourself

FosElasticaPopulateService:
    class: Path\To\Bundle\Class
    calls:
        - [setContainer, ["@service_container"] ]

then into your controller

$output = new ConsoleOutput;
$command = $this->get('FosElasticaPopulateService');
$command->run(null, $ouput); //null here is for input parameters; if you need them, insert

if it is already registered, just use it as showed above


Ok, I got it: my solution is an alternative one (maybe more suitable for your own command if bundle's command aren't registered as services natively). Reading some documentation, your method should work aswell. You've got an error that blocks you:

$output = new ConsoleOutput; should be $output = new ConsoleOutput();

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • Now i have: $output = new ConsoleOutput; $input = new ArrayInput(array("fos:elastica:populate")); $command = $this->get('FosElasticaPopulateService'); $command->run($input, $output);. It doesn't work. I have the symfony error: "The "0" argument does not exist" – Sancho Mar 20 '14 at 14:16
  • Same error with: $output = new ConsoleOutput(); $command = $this->get('FosElasticaPopulateService'); $input = new ArrayInput(array("fos:elastica:populate")); $command->run($input, $output); . There is no "easy" way to execute a simple Command ? Something like SymfonyExecute('fos:elastica:populate'); ? – Sancho Mar 20 '14 at 14:24
  • @Sancho: what are you doing with `get('FosElasticaPopulateService');`? I told you, them are two alternative solutions. Your code has a typo man, don't mix and mess all the things up please :) – DonCallisto Mar 20 '14 at 14:45
  • i don't understand something... I use $output = new ConsoleOutput; $command = $this->get('FosElasticaPopulateService'); $command->run(null, $output); Is that correct ? It say 'run() must be an instance of InputInterface. – Sancho Mar 20 '14 at 14:54
  • @Sancho: Sorry if I'm rude but ... Can you read what I wrote? You got a typo! You have to modify `$output = new ConsoleOutput` in `$output = new ConsoleOutput()` Please check under the "line" in my answer – DonCallisto Mar 20 '14 at 14:56
  • I try my first code with '$output = new ConsoleOutput();'. I got return = 1 (command failed) – Sancho Mar 20 '14 at 15:04
  • @Sancho: so, maybe, you got an error in command or command running. But trust me, this is the right way ;) – DonCallisto Mar 20 '14 at 15:54
0

If you need the code in the command to be executed in the controller, put the code in its own class and call that class in the controller and the command.

Pavel Petrov
  • 847
  • 10
  • 19
  • I want to execute a command give by a bundle. I can't move the code from fos:elastica to another class. – Sancho Mar 20 '14 at 14:32
  • I see. The command you are trying to execute has some required options. https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/Command/PopulateCommand.php May be you should set them through the input. – Pavel Petrov Mar 20 '14 at 16:05
  • The option "batch-size" is required and has no default value. – Pavel Petrov Mar 20 '14 at 16:09