Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Created March 5, 2014 01:24
Show Gist options
  • Save jakubkulhan/9359412 to your computer and use it in GitHub Desktop.
Save jakubkulhan/9359412 to your computer and use it in GitHub Desktop.
js2php's usage example
console.log("hello, world!");
<?php
require_once __DIR__ . "/build/image.php";
require_once __DIR__ . "/build/JSInterpreter.php";
$interpreter = new JSInterpreter(file_get_contents("some.file.js"), "some.file.js");
$compiledCode = $interpreter->compile();
$mainFunction = eval($compiledCode);
file_put_contents(
"some.file.php",
"<?php\n" .
preg_replace(
"/\n([^\n]|\n\n)+$/",
"\nrequire_once __DIR__ . '/build/image.php';\n$mainFunction(JS::\$global);",
$compiledCode
)
);
<?php
require_once __DIR__ . "/build/image.php";
require_once __DIR__ . "/build/JSInterpreter.php";
$interpreter = new JSInterpreter(file_get_contents("some.file.js"), "some.file.js");
$interpreter->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment