phplinuxsymfonyprocessphing

Symfony - use Command to run Phing task


When I try to run that phing command: bin/phing clear_cache_action from a console, everything works. Unfortunately, when I try to run the same command from the controller in the Symfony project I get an error.

That my code:

    public function clearAction()
    {
        $process = new Process('bin/phing clear_cache_action');
        $process->run();

        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }

        echo $process->getOutput();
    }

Symfony returns me that error:

The command "bin/phing clear_cache_action" failed.

Exit Code: 127(Command not found)

Working directory: /var/www/caolin/web

Output:
================


Error Output:
================
sh: 1: bin/phing: not found

Linux commands e.g. 'ls' works properly. How can I run phing command from code?


Solution

  • I guess you are trying to execute phing from a Controller. Thus Working directory: /var/www/caolin/web instead of /var/www/caolin causes resolving bin/phing to /var/www/caolin/web/bin/phing which does not exist. You should set your current working directory to %kernel.project_dir%:

    $process = new Process(
        ['bin/phing', 'clear_cache_action'], 
        $this->getParameter('kernel.project_dir')
    );
    $process->run();
    

    However, I would not recommend starting a process from a Controller unless you are really sure what you are doing.