nacmartin/phpexecjs
Composer 安装命令:
composer require nacmartin/phpexecjs
包简介
Run JavaScript code from PHP
README 文档
README
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
)
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:
- 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.
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.
nacmartin/phpexecjs 适用场景与选型建议
nacmartin/phpexecjs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.39M 次下载、GitHub Stars 达 150, 最近一次更新时间为 2016 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nacmartin/phpexecjs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nacmartin/phpexecjs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.39M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 151
- 点击次数: 17
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-14