2017 © Pedro PelĂĄez
 

library phpexecjs

Run JavaScript code from PHP

image

nacmartin/phpexecjs

Run JavaScript code from PHP

  • Wednesday, November 22, 2017
  • by nacmartin
  • Repository
  • 4 Watchers
  • 54 Stars
  • 140,068 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 11 Forks
  • 2 Open issues
  • 15 Versions
  • 13 % Grown

The README.md

PhpExecJs

PhpExecJS lets you run JavaScript code from PHP., (*1)

Short example:, (*2)

print_r($phpexecjs->evalJs("'red yellow blue'.split(' ')"));

Will print:, (*3)

Array
(
    [0] => red
    [1] => yellow
    [2] => blue
)

Build Status Latest Stable Version Latest Unstable Version License, (*4)

Installation

composer require nacmartin/phpexecjs

Sample program, (*5)

Usage

<?php
    require __DIR__ . '/../vendor/autoload.php';

    use Nacmartin\PhpExecJs\PhpExecJs;

    $phpexecjs = new PhpExecJs();

    print_r($phpexecjs->evalJs("'red yellow blue'.split(' ')"));

Will print:, (*6)

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 to render React server-side., (*7)

For instance, we can compile CoffeeScript using this feature:, (*8)

    $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:, (*9)

    $square = $phpexecjs->call("CoffeeScript.compile", ["square = (x) -> x * x", ['bare' => true]]);
    $phpexecjs->createContext($square);
    print_r($phpexecjs->evalJs('square(3)'));

That will print 9., (*10)

This can be used for instance, to use CoffeeScript or compile templates in JavaScript templating languages., (*11)

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., (*12)

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., (*13)

Runtimes supported

By default, PhPExecjs will auto-detect the best runtime available. Currently, the routines supported are:, (*14)

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., (*15)

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:, (*16)

    $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., (*17)

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:, (*18)

global, module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate, (*19)

Credits

This library is inspired in ExecJs, A Ruby library., (*20)

The code used to manage processes and temporary files has been adapted from the Snappy library by KNP Labs., (*21)

The Versions

22/11 2017

dev-master

9999999-dev

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

22/11 2017

v2.0.0-beta.0

2.0.0.0-beta0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

22/11 2017

v2.0.0

2.0.0.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

22/11 2017

1.0.x-dev

1.0.9999999.9999999-dev

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

22/11 2017

v0.8.8

0.8.8.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

22/11 2017

v1.0.0

1.0.0.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

28/09 2016

v0.8.7

0.8.7.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

13/09 2016

v0.8.6

0.8.6.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

18/03 2016

v0.8.5

0.8.5.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

21/02 2016

v0.8.4

0.8.4.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

21/02 2016

v0.8.3

0.8.3.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

20/02 2016

dev-analysis-Xajox0

dev-analysis-Xajox0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

15/02 2016

v0.8.2

0.8.2.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

14/02 2016

v0.8.1

0.8.1.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho

13/02 2016

v0.8.0

0.8.0.0

Run JavaScript code from PHP

  Sources   Download

MIT

The Requires

 

by nacho