1

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();
    }
}

1 Answer 1

6

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Aaaaaaaand.... it worked perfectly. Shame on me to forget the working directory. Anyway for those looking how to get instance of kernel : $this->container->get('kernel'); Thank you !
Solution #2 doesn't run the command asynchronously though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.