PhpExecJS lets you run JavaScript code from PHP.
Short example:
print_r($phpexecjs->evalJs("'red yellow blue'.split(' ')"));
Will print:
Array
(
[0] => red
[1] => yellow
[2] => blue
)
composer require nacmartin/phpexecjs
Sample program
<?php
require __DIR__ . '/../vendor/autoload.php';
use Nacmartin\PhpExecJs\PhpExecJs;
$phpexecjs = new PhpExecJs();
print_r($phpexecjs->evalJs("'red yellow blue'.split(' ')"));
Will print:
Array
(
[0] => red
[1] => yellow
[2] => blue
)
You can set up a context, like libraries and whatnot, that you want to use in your eval'd code. This is used for instance by the ReactBundle to render React server-side.
For instance, we can compile CoffeeScript using this feature:
$phpexecjs->createContextFromFile("http://coffeescript.org/extras/coffee-script.js");
print_r($phpexecjs->call("CoffeeScript.compile", ["square = (x) -> x * x", ['bare' => true]]));
That will print:
var square;
square = function(x) {
return x * x;
};
You can extend this example to do things like use this function as context:
$square = $phpexecjs->call("CoffeeScript.compile", ["square = (x) -> x * x", ['bare' => true]]);
$phpexecjs->createContext($square);
print_r($phpexecjs->evalJs('square(3)'));
That will print 9
.
This can be used for instance, to use CoffeeScript or compile templates in JavaScript templating languages.
When you run evalJs
, the code will be inserted into a small wrapper used to run JavaScript's eval()
against your code and check the status for error handling.
If you set up a context, the code will be inserted before the call to eval()
in JavaScript, and if you have the V8Js extension installed, it will precompile it.
By default, PhPExecjs will auto-detect the best runtime available. Currently, the routines supported are:
- V8Js (PHP extension)
- node.js
It is recommended to have V8Js installed, but you may want to have it installed in production and still be able to use PhpExecJs calling node as a subprocess during development, so you don't need to install the extension.
If you have a external runner (let's say, Spidermonkey), and you want to use it, pass it to the constructor:
$myRuntime = new ExternalRuntime('My runtime name', 'my_command');
$phpExecJs = new PhpExecJs($myRuntime);
We would like to support more runtimes (Duktape, for instance). If you want to contribute with a runtime, it is pretty simple. You just have to implement src/Runtimes/RuntimeInterface
. Check the directory src/Runtimes
for examples.
This library is inspired in ExecJs, A Ruby library.
The code used to manage processes and temporary files has been adapted from the Snappy library by KNP Labs.