juststeveking/os-process
Composer 安装命令:
composer require juststeveking/os-process
包简介
A PHP Package to work with OS processes in an OOP way.
README 文档
README
This package is a wrapper around the Symfony Process component, and build up an API that is object-oriented and user-friendly.
Installation
composer require juststeveking/os-process
Usage
To start using this package, you first need to create an Abstraction for the process you want to use. I will walk you through this here:
We create an abstraction for Terraform:
use JustSteveKing\OS\Contracts\ProcessContract; class Terraform implements ProcessContract { // }
The ProcessContract means that you have to implement a build method that will return a built Symfony Process that we can then run.
use JustSteveKing\OS\Contracts\CommandContract; use JustSteveKing\OS\Contracts\ProcessContract; use Symfony\Component\Process\Process; class Terraform implements ProcessContract { private CommandContract $command; public function build() : Process { return new Process( command: $this->command->toArgs(), ); } }
Let's create a Command now:
use JustSteveKing\OS\Contracts\CommandContract; class TerraformCommand implements CommandContract { public function __construct( public readonly array $args = [], public readonly null|string $executable = null, ) {} public function toArgs() : array { $executable = (new ExecutableFinder())->find( name: $this->executable ?? 'terraform', ); if (null === $executable) { throw new InvalidArgumentException( message: "Cannot find executable for [$this->executable].", ); } return array_merge( [$executable], $this->args, ); } }
Now we just need to build and assign this within our abstraction:
use JustSteveKing\OS\Contracts\CommandContract; use JustSteveKing\OS\Contracts\ProcessContract; use Symfony\Component\Process\Process; class Terraform implements ProcessContract { private CommandContract $command; public function apply(): Process { $this->command = new TerraformCommand( executable: 'terraform', ); return $this->build(); } public function build() : Process { return new Process( command: $this->command->toArgs(), ); } }
Now we are able to work with this process in our code:
$terraform = new Terraform(); $terraform->apply()->run(); // Will run `terraform apply` in an OS process.
You can also obtain information about the process
$terraform = new Terraform(); $process = $terraform->apply(); // The process variable contains an instance of Symfony\Component\Process $process->run(); // run `terraform apply` in an OS process. // Obtaining information about the process $output = $process->getOutput(); // Output of the command $error = $process->getErrorOutput(); // Error output $commandLine = $process->getCommandLine(); // Obtaining the complete command
Getting real-time Process Output
$terraform = new Terraform(); $process = $terraform->apply(); // The process variable contains an instance of Symfony\Component\Process $process->start(); // start `terraform apply` in an OS process. (start not run) $process->wait(function ($type, $buffer) { if (\Symfony\Component\Process\Process::ERR === $type) { echo 'ERR > '.$buffer; } else { echo 'OUT > '.$buffer; } });
juststeveking/os-process 适用场景与选型建议
juststeveking/os-process 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 159 次下载、GitHub Stars 达 76, 最近一次更新时间为 2022 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 juststeveking/os-process 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 juststeveking/os-process 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 159
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 76
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-30