How can I run symfony 2 run command from controller

前端 未结 9 1445
长发绾君心
长发绾君心 2020-11-28 20:03

I\'m wondering how can I run Symfony 2 command from browser query or from controller.

Its because I don\'t have any possibility on hosting to run it

9条回答
  •  隐瞒了意图╮
    2020-11-28 20:18

    If you run a command that need the env option like assetic:dump

    $stdout->writeln(sprintf('Dumping all %s assets.', $input->getOption('env')));
    

    You have to create a Symfony\Component\Console\Application and set the definition like that:

    use Symfony\Component\Console\Application;
    use Symfony\Component\Console\Input\ArgvInput;
    use Symfony\Component\Console\Input\InputDefinition;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Output\NullOuput;
    
    // Create and run the command of assetic
    $app = new Application();
    $app->setDefinition(new InputDefinition([
        new InputOption('env', '', InputOption::VALUE_OPTIONAL, '', 'prod')
    ]));
    $app->add(new DumpCommand());
    
    /** @var DumpCommand $command */
    $command = $app->find('assetic:dump');
    $command->setContainer($this->container);
    $input = new ArgvInput([
        'command' => 'assetic:dump',
        'write_to' => $this->assetsDir
    ]);
    $output = new NullOutput();
    $command->run($input, $output);
    

    You can't set the option env to the command because it isn't in its definition.

提交回复
热议问题