Boris Le Méec
Boris Le Méec

Reputation: 2671

Symfony4 - Process - Command Could not open input file bin/console

I'm building a symfony4 webapp. I have a command that I can run like a charm directly in cli :

php bin/console app:analysis-file 4

But when I try to exec if directly from a Controller via :

$process = new Process('php bin/console app:analysis-file '. 
$bankStatement->getId());
$process->run();

Then $process->getOutput() return "Could not open input file bin/console".

This is the Command Class :

class AnalysisFileCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('app:analysis-file')
            ->addArgument('file_id', InputArgument::REQUIRED);
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $entityManager = $this->getContainer()->get('doctrine')->getEntityManager();
        $bankStatement = $entityManager->getRepository(BankStatement::class)->find($input->getArgument("file_id"));
        $bankStatement->setStatus(BankStatement::ANALYZING);
        $entityManager->persist($bankStatement);
        $entityManager->flush();
    }
}

Upvotes: 1

Views: 5425

Answers (1)

Jovan Perovic
Jovan Perovic

Reputation: 20193

I would guess that the current working directory does not match your project root. Because of that, relative path bin/console does not exist.

You have 2 ways to solve this:

  1. Set the current working directory:

    $kernel = ...; // Get instance of your Kernel
    $process = new Process('php bin/console app:analysis-file ');
    $process->setWorkingDirectory($kernel->getProjectDir());
    $bankStatement->getId());
    $process->run();
    
  2. Invoke the command via Symfony Command call, which is described in official docs article

Have in mind that #2 has a slight overhead (as described in the article)

Hope this helps...

Upvotes: 6

Related Questions