lifespikes/php-node
Composer 安装命令:
composer require lifespikes/php-node
包简介
Spawn and manage node instances from PHP
README 文档
README
Simple library for executing short-lived NodeJS processes from PHP.
Hi Packagist :) https://packagist.com/orgs/lifespikes/packages/3021975
Who is this for?
Every language and platform has their pros and cons, for us one of the biggest benefits of JavaScript is being able to whip up programs in the span of a few minutes.
This package is for developers who want a simple, object-oriented, library to integrate microservices written in Node in their PHP apps.
All this for a process manager?
PHP Node is a library we built to leverage power from multiple languages different members of our team specialize in and their awesome ecosystems. Naturally we want these integrations to be as seamless as possible with the rest of our code, but we also don't want to make anything more complicated than it needs to be.
Instead of implementing separate APIs for each service using let's say a SOAP or REST interface using Express, or deploying these services as docker containers, PHP Node helps us:
- Execute scripts using a simple one-line syntax
- Mitigate disconnects in error reporting
- Quickly deploy new microservices with minimal setup
- Test integrations by asserting
FinishedProcessproperties - Send and receive information without using arguments
- Easily determine error reasons, exit status, and other details
Examples
Using PHP Node is pretty straight forward.
The very first step is going to be making sure you have NodeJS installed on your
machine. The version you run is up to you. Then, make sure the path to Node is present
in your PATH, or set as a NODE_BINARY environment variable.
Now make sure your script is accessible by your web server or PHP process owner.
Lastly, make sure to expect input on your script by receiving it using stdin, instead
of argv. We opted for using the stream route to allow us to broadcast reliable and larger
chunks of data.
(This library is pretty awesome at this: https://www.npmjs.com/package/get-stdin)
Then, just run it. In this example we're using a microservice to take a screenshot of a website:
<?php namespace LifeSpikes\PHPNode\Engine; use \scripts_path; /* You can add arguments to the script if needed */ $script = Engine::spawn(scripts_path('screenshot.js')); /* Use "with" to send data before executing */ $result = $script->with([ 'url' => 'https://google.com' ]); printf( "Job ID: %s, Exit Code: %d", $result->output['job_id'], $result->status() ); /* If not sending any data, you can use ->run() */
For context, we'll write another small pseudo-script:
import screenshot from 'web-browser-lib'; import getStdin from 'get-stdin'; import {stdout} from 'process'; const stdout = (json) => stdout.write( JSON.stringify(json) ); (async () => { const input = JSON.parse(await getStdin()); const job = await screenshot(input.url); /* console.log would have the same result */ stdout({ job_id: job.id }); })();
lifespikes/php-node 适用场景与选型建议
lifespikes/php-node 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.71k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 lifespikes/php-node 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lifespikes/php-node 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 25.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-04