Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nacmartin/phpexecjs
Run JavaScript code from PHP
https://github.com/nacmartin/phpexecjs
Last synced: 3 days ago
JSON representation
Run JavaScript code from PHP
- Host: GitHub
- URL: https://github.com/nacmartin/phpexecjs
- Owner: nacmartin
- License: mit
- Created: 2016-01-27T18:30:57.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-05-25T07:42:48.000Z (over 1 year ago)
- Last Synced: 2024-10-30T06:58:24.482Z (about 2 months ago)
- Language: PHP
- Homepage:
- Size: 55.7 KB
- Stars: 146
- Watchers: 5
- Forks: 26
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PhpExecJs
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
)[![Build Status](https://travis-ci.org/nacmartin/phpexecjs.svg?branch=master)](https://travis-ci.org/nacmartin/phpexecjs)
[![Latest Stable Version](https://poser.pugx.org/nacmartin/phpexecjs/v/stable)](https://packagist.org/packages/nacmartin/phpexecjs)
[![Latest Unstable Version](https://poser.pugx.org/nacmartin/phpexecjs/v/unstable)](https://packagist.org/packages/nacmartin/phpexecjs)
[![License](https://poser.pugx.org/nacmartin/phpexecjs/license)](https://packagist.org/packages/nacmartin/phpexecjs)# Installation
composer require nacmartin/phpexecjs
Sample program
# Usage
```php
evalJs("'red yellow blue'.split(' ')"));
```Will print:
Array
(
[0] => red
[1] => yellow
[2] => blue
)# Using contexts
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](https://github.com/limenius/ReactBundle/) to render React server-side.
For instance, we can compile CoffeeScript using this feature:
```php
$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:```php
$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.
# How it works
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](https://github.com/phpv8/v8js) installed, it will precompile it.
# Runtimes supported
By default, PhPExecjs will auto-detect the best runtime available. Currently, the routines supported are:
* [V8Js (PHP extension)](https://github.com/phpv8/v8js)
* node.jsIt 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.
## Adding a external runtime
If you have a external runner (let's say, Spidermonkey), and you want to use it, pass it to the constructor:
```php
$myRuntime = new ExternalRuntime('My runtime name', 'my_command');
$phpExecJs = new PhpExecJs($myRuntime);
```## Contributing with runtimes
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.
# Why can't I use some functions like `setTimeout`?
PhpExecJs provides a common denominator interface to JavaScript runtimes, so it can only run code that is agnostic about the interpreter. Thus, some features are disabled. Notably, timer functions are disabled because not all runtimes guarantee a full JavaScript event loop. If you want to use any of these please use directly node.js instead of this higher level library:
`global`, `module`, `exports`, `require`, `console`, `setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `setImmediate`, `clearImmediate`
# Credits
This library is inspired in [ExecJs](https://github.com/rails/execjs), A Ruby library.
The code used to manage processes and temporary files has been adapted from the [Snappy](https://github.com/KnpLabs/snappy) library by KNP Labs.