I have the following simple command:
$process = new Process("php /Users/Name/Sites/App/app/../bin/console cache:clear --env=prod");
$process->run();
when I try to run this it gives me:
string(153) " Parse error: parse error in /Users/Name/Sites/App/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php on line 278 "
What is wrong?
Looks like you are using Symfony 3.0 that has "finally"
https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/DependencyInjection/Container.php#L282
PHP 5.5 and later has support for "finally" in try/catch blocks.
http://php.net/manual/en/language.exceptions.php
And looks like your php version is less then 5.5, so upgrade your php version > 5.5 and it will work
$process = new Process("php /Users/Name/Sites/App/app/../bin/console cache:clear --env=prod");
$process->run();
You should try
$process = new Process("php /Users/Name/Sites/App/app/console cache:clear --env=prod");
$process->run();
Because console is in app/ directory and not in bin/
Related
I am developing a Laravel (8.83.24) app and testing with Laravel's built in server "php artisan serve". I am using Process and am trying to debug a memory allocation error which has lead me to here:
$process = new Process(['node', '-v']);
$process->run();
print_r($process);
That leads me to a line in the output:
[command] => cmd /V:ON /E:ON /D /C (node -v) 1>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.out" 2>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.err"
and sf_proc_01.err contains:
'node' is not recognized as an internal or external command,
operable program or batch file.
Clearly node can't be found. I am on Windows 10. Node is set in my in System and User PATH. Running "node -v" from cmd.exe works and returns the version number. So Laravel's server doesn't seem to be able to find node. However, I can't check the path as running this:
$process = new Process(['echo', '%PATH%']);
$process->run();
print_r($process);
Just leads to an output of
"%PATH%"
How can I make Node findable by Laravel / Process?
Many thanks for your help.
I eventually solved this by setting the path for node in my Laravel .env file:
NODE_PATH='C:\PROGRA~1\nodejs\node.exe'
NPM_PATH='D:\Work\Project\node_modules'
I'm trying to execute audit2allow using Symfony\Component\Process\Process.
When I run exec("audit2allow -a -M a2a"); in PHP, it works just fine, a2a.pp and a2a.te is produced.
$process = new Process(['audit2allow', '-a', '-M', 'a2a']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
However, the above code produces the output below
Symfony\Component\Process\Exception\ProcessFailedException : The command "'audit2allow' '-a' '-M' 'a2a'" failed.
Exit Code: 1(General error)
Working directory: /var/www/html/example
Output:
================
compilation failed:
a2a.te:6:ERROR 'syntax error' at token '' on line 6:
/usr/bin/checkmodule: error(s) encountered while parsing configuration
/usr/bin/checkmodule: loading policy configuration from a2a.te
which is the typical output when there is a empty /var/log/audit/audit.log.
What do I need to change to make it work properly? Symfony claims it is not a bug.
https://github.com/symfony/symfony/issues/35862
Update
Using Symfony\Component\Process\Process (code above) actually produces the file a2a.te, but it has only 1 line.
module a2a 1.0;
Whereas using exec() produces the file a2a.te with many lines:
module a2a 1.0;
require {
type kernel_t;
type vmblock_t;
type container_t;
...
Why does running the same command on Symfony\Component\Process\Process and exec() gives different outcomes?
I've installed mjml cli using the following command (as described in the mjml documentation):
npm install mjml --save
now if i did node_modules/.bin/mjml in the command line it will run successfully.
the problem is when i use the symfony process component in php i got the following error (even if it's the right path):
The command "/Users/qoraiche/Documents/my-app/node_modules/.bin/mjml" failed. Exit Code: 127(Command not found) Working directory: /Users/qoraicheOS/Documents/my-app/public Output: ================ Error Output: ================ env: node: No such file or directory
Symfony process code:
$process = new Process(base_path('node_modules/.bin/mjml'));
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
By the way i have installed mjml globally as well and try it with no luck.
Try providing the full path to node.js when calling the MJML binary from a Symfony Process().
$process = new Process('/your/path/to/node ' . base_path('node_modules/.bin/mjml'));
I use this method in my own apps. Of course, you will need to provide the input and output files to actually transpile anything.
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?
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.
I am able to run it through the browser but not from command line
ie php test.php
$raw = Cassandra::cluster()
->withContactPoints('localhost')
//->withCredentials($this->username, $this->password)
->build();
var_dump($raw);
die;
from browser:
object(Cassandra\DefaultCluster)#2 (0) { }
command line:
PHP Fatal error: Class 'Cassandra' not found in /var/www/html/test.php on line 2
Is it possible to get same from command line as well?
The cli uses a different php.ini file (e.g. /etc/php/7.0/cli/php.ini & /etc/php/7.0/fpm/php.ini). You are probably not including the extension in the cli one.