• Stars
    star
    139
  • Rank 262,037 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run JavaScript code from PHP

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 Latest Stable Version Latest Unstable Version License

Installation

composer require nacmartin/phpexecjs

Sample program

Usage

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

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.

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.

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 installed, it will precompile it.

Runtimes supported

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

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.

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:

    $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, A Ruby library.

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

More Repositories

1

manitas

Move your fingers in the air to interact with a computer ☝️☝️
TypeScript
173
star
2

gop2p

simple p2p project to learn go
Go
79
star
3

elm-webgl-lessons

A series of lessons in elm-webgl
Elm
35
star
4

nodetcltk

Using TclTk from node.js is fun
JavaScript
17
star
5

skinny

Web application to create and manage lists. Derivated from http://symfony-check.org/ Made with symfony 1.4
PHP
14
star
6

silexslides

Presentación de Silex para Jornadas Symfony 2011 (Hecha en Silex)
PHP
11
star
7

gozame

small app written in Silex to share bookmarks quickly with people in the same room without emails or whatever
PHP
10
star
8

riak-client

update of the official riak php client to use composer, namespaces, phpunit and other goodies
PHP
7
star
9

Cazalla

Static website generator written in PHP using Symfony2 components
PHP
6
star
10

edisque

Disque client for Erlang
Erlang
5
star
11

eredis_smart_sub

Layer on top of eredis (erlang redis client) to multiplex sub connections among several subscriptor processes in pubsub
Erlang
5
star
12

tuco

Webapp in node.js to create and manage collections of images
JavaScript
4
star
13

dotvim

My beloved vim configuration
Vim Script
3
star
14

nmDoctrineGuardStuffPlugin

Yet another plugin extending sfDoctrineGuardPlugin
PHP
3
star
15

try-riak

PHP
3
star
16

ReduxIntro

Sample code for the React Native Munich Meetup
JavaScript
3
star
17

flickrgetter

Simple php script to help posting Flickr Creative Commons pictures into wordpress
PHP
2
star
18

prismsvg

Prismjs tweaked to output svgs
JavaScript
1
star
19

hmark

Simple web app to share a bookmark in haskell. I use now github.com/nacmartin/gozame instead
Haskell
1
star
20

Historyable

Doctrine 2 extension
PHP
1
star
21

algernon

Webapp in Symfony2 to memorize vocabulary using flashcards
JavaScript
1
star
22

ipanema

Simple CMS to explore the current symfony-cmf posibilities
JavaScript
1
star
23

boetticher

Webapp to manage your time
PHP
1
star
24

HistoryableBundle

Bundle for Historyable extension
PHP
1
star