Run mjml cli using symfony process - php

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.

Related

Laravel Process can't find Node

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'

Symfony\Component\Process\Process unable to run audit2allow

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?

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

Symfony/Process cant run sh script from directory

I have some script: /home/user/project/deploy.sh
I trying to run with Symfony/Process component from php handler in another directory:
$process = new Process(['./deploy.sh']);
$process->setWorkingDirectory('/home/user/project');
$process->run(function ($type, $buffer) {
echo $buffer;
});
But I got an error:
The provided cwd "/home/user/project" does not exist.
This folder exists and symfony handler ran as correct user which have correct permissions to this folder. What is the correct way to run this script from another folder?
Just for debugging purposes test code below.
$process = new Process(['/home/user/project/bin/console', '--version']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
If it does work then try:
new Process(['/home/user/project/deploy.sh']);
new Process(['sh /home/user/project/deploy.sh']);
Process::fromShellCommandline('/home/user/project/deploy.sh');
You may need to use the full path to sh since the webserver uses a different user and a PATH variable.

installing codeigniter sparks manager on windows

SOLVED, read bottom of post:
I'm trying to install the Sparks package manager on windows by following the official instructions.
Issuing this command:
php -r "$(curl -fsSL http://getsparks.org/go-sparks)"
results in this errormessage:
Parse error: syntax error, unexpected ':' in Command line code on line 1
If I only execute the curl command within the above line, i.e this:
curl -fsSL http://getsparks.org/go-sparks
it echoes out the php script located on the URL. So I think the problem is piping the curl output to PHP somehow fails. I've tried a couple of variants, using diffrent quotes etc but I'm at a loss.
MY SOLUTION
As DaveRandom pointed out, the instruction didn't apply to windows.
But instead of doing it the manual(normal) way, what I did was taking the output from curl, appending php script tags and executing it as a file with the php -f option.
Here is the output:
<?php
$zip = "http://getsparks.org/static/install/spark-manager-0.0.7.zip";
$loader = "http://getsparks.org/static/install/MY_Loader.php.txt";
if(!file_exists("application/core"))
{
echo "Can't find application/core. Currently this script only works with the default instance of Reactor. You may need to try a manual installation..\n";
exit;
}
echo "Pulling down spark manager from $zip ...\n";
copy($zip, "sparks.zip");
echo "Pulling down Loader class core extension from $loader ...\n";
copy($loader, "application/core/MY_Loader.php");
echo "Extracting zip package ...\n";
if(class_exists('ZipArchive'))
{
$zip = new ZipArchive;
if ($zip->open('sparks.zip') === TRUE)
{
$zip->extractTo('./tools');
$zip->close();
} else {
echo "Extraction failed .. exiting.\n";
exit;
}
} elseif(!!#`unzip`) {
`unzip sparks.zip -d ./tools`;
} else
{
echo "It seems you have no PHP zip library or `unzip` in your path. Use the manual installation.\n";
exit;
}
echo "Cleaning up ...\n";
#unlink('sparks.zip');
echo "Spark Manager has been installed successfully!\n";
echo "Try: `php tools/spark help`\n";
The instructions you linked do explicitly state;
In order to use this quick start option, you should be using OSX or some flavor of linux.
You need to follow the Normal Installation instructions for use on Windows.
You should execute the command php -r "$(curl -fsSL http://getsparks.org/go-sparks)" in your root application folder. Maybe you're executing this command at wrong folder.

Categories