elevenways/doen
Composer 安装命令:
composer require elevenways/doen
包简介
A bridge between PHP & Node.js
README 文档
README
elevenways/doen
Asynchronously call JavaScript code from within PHP Coded with ❤️ by Eleven Ways.
Introduction
Have you ever needed to use the functionality of a node.js package in your PHP project, but don't want to spend hours writing a wrapper? Well now you can.
Thanks to ReactPHP it was quite a simple script to get working.
Installation
Installation is easy via Composer:
$ composer require elevenways/doen
Or you can add it manually to your composer.json file.
Usage
Simple require() example
// You will need an event loop instance. // If this looks weird to you, you should lookup ReactPHP $loop = \React\EventLoop\Factory::create(); // Now we can create a Doen instance $doen = new \Elevenways\Doen\Doen($loop); // Lets get our first, simple reference // $libpath is now an \Elevenways\Doen\Reference instance $libpath = $doen->evaluateToRef('require("path")'); // Even though the code is happening asynchronously, we can already act upon it // This will return yet another \Elevenways\Doen\Reference instance $str = $libpath->join('a', 'b', 'c'); // Now we can get the value $str->getValue()->then(function ($value) { // This will print out a/b/c echo $value; }); // This, again, is part of ReactPHP. // It starts the event loop and will BLOCK the rest of the code! $loop->run();
API
Doen
new Doen(\React\EventLoop\LoopInterface $loop, array $options = [])
Create a new Doen instance, which always creates a new Node.js instance too.
By default it'll use the node binary, but this can be overridden with the node_path option.
require(string $name) ⇒ \Elevenways\Doen\Reference
Require a node.js module and return a reference to it.
$libpath = $doen->require('path');
evaluate(string $code) ⇒ \React\Promise\Promise
Execute an expression and return its value.
$promise = $doen->evaluate('1 + 1');
evaluateFunction(string $function, $args = []) ⇒ \React\Promise\Promise
Execute a function with the supplied arguments and return its value.
$promise = $doen->evaluate('function(a, b) {return a * b}', [3, 2]);
evaluateToRef(string $code, $args = null) ⇒ \Elevenways\Doen\Reference
Execute an expression or a function and return a reference to its value.
$libpath = $doen->evaluateToRef('require("path")');
close() ⇒ void
Close the node.js process
$doen->close();
Reference
getValue(callable $on_fulfilled = null, callable $on_rejected = null) ⇒ \React\Promise\Promise
Get the actual value of the reference
$ref = $doen->evaluateToRef('1 + 1'); $ref->getValue(function($result) { // Outputs: 2 echo $result; });
then(callable $on_fulfilled = null, callable $on_rejected = null) ⇒ \React\Promise\Promise
Execute the callables when this reference resolves. It will not resolve to its value, but to its type for primitives and to its class for objects.
$ref = $doen->evaluateToRef('1 + 1'); $ref->then(function($type) { // Outputs: "number" echo $type; }); $array = $doen->evaluateToRef('[]'); $ref->then(function($type) { // Outputs: "Array" echo $type; });
__monkeyPatch($method_name, Closure $fnc) ⇒ void
Add a method to this instance on-the-fly
$ref = $doen->evaluateToRef('1 + 1'); $ref->__monkeyPatch('weird', function() { return 'weird'; }); // Returns "weird" $ref->weird();
Contributing
Contributions are REALLY welcome. Please check the contributing guidelines for more details. Thanks!
Authors
- Jelle De Loecker - Follow me on Github (
@skerit) and on Twitter (🐦@skeriten)
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.
elevenways/doen 适用场景与选型建议
elevenways/doen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nodejs」 「Bridge」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 elevenways/doen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elevenways/doen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 elevenways/doen 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Soluble PHP Java bridge integration
Bridge between JMS Serializer Bundle and Superdesk Web Publisher.
DNode RPC protocol for PHP 5.3
Send events to a websocket real time engine though PHP
Builds a Bridge between Zend Expressive and Plugin Managers of Zend\MVC
Send events to a websocket real time engine though PHP
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-19