lionmm / binary-driver
alchemy/binary-driver的分支,用于支持symfony/process 6/7。构建二进制驱动程序的工具集。
Requires
- php: >=8.0.0
- evenement/evenement: ^3.0|^2.0
- psr/log: ^2.0|^3.0
- symfony/process: ^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^9 || ^6
Replaces
- alchemy/binary-driver: 5.2.0
README
Binary-Driver是一组PHP工具,用于构建二进制驱动程序。
为什么?
你可能想知道,为什么要在可以使用exec
或symfony/process的情况下还构建一个库?.
这里有一个简单的答案
-
如果你使用
exec
、passthru
、system
、proc_open
或任何PHP中的低级进程处理,你应该查看symfony/process组件,该组件将提供一个OO便携、可测试和安全的接口来处理这些操作。一开始看起来很简单,但如果你看一下这个组件的单元测试,你会看到在简单接口中处理进程很容易变成一场噩梦。 -
如果你已经使用symfony/process,并想构建二进制驱动程序,你将始终需要配置、记录、调试和生成进程的相同常用方法和对象。这个库是实现具有这些共同需求任何二进制驱动程序的基础。
AbstractBinary
AbstractBinary
提供了一个用于构建二进制驱动程序的抽象类。它实现了BinaryInterface
。
实现示例
use Alchemy\BinaryDriver\AbstractBinary; class LsDriver extends AbstractBinary { public function getName() { return 'ls driver'; } } $parser = new LsParser(); $driver = Driver::load('ls'); // will return the output of `ls -a -l` $parser->parse($driver->command(array('-a', '-l')));
二进制检测故障排除
如果你正在使用Nginx与PHP-fpm,由于$_ENV['path']
为空,可执行文件检测可能不会工作。为了避免有空的环境变量PATH
,请将以下行添加到您的fastcgi_params
配置文件中(将/your/current/path/
替换为printenv PATH
的输出)
fastcgi_param PATH /your/current/path
日志记录
你可以通过将日志记录器作为加载方法的第二个参数传递来使用Psr\Log\LoggerInterface
记录事件
$logger = new Monolog\Logger('driver'); $driver = Driver::load('ls', $logger);
监听器
你可以在进程中添加自定义监听器。监听器建立在Evenement之上,并必须实现Alchemy\BinaryDriver\ListenerInterface
。
use Symfony\Component\Process\Process; class DebugListener extends EventEmitter implements ListenerInterface { public function handle($type, $data) { foreach (explode(PHP_EOL, $data) as $line) { $this->emit($type === Process::ERR ? 'error' : 'out', array($line)); } } public function forwardedEvents() { // forward 'error' events to the BinaryInterface return array('error'); } } $listener = new DebugListener(); $driver = CustomImplementation::load('php'); // adds listener $driver->listen($listener); $driver->on('error', function ($line) { echo '[ERROR] ' . $line . PHP_EOL; }); // removes listener $driver->unlisten($listener);
捆绑的监听器
调试监听器是一个简单的监听器,用于捕获stderr
和stdout
输出;阅读实现进行自定义。
use Alchemy\BinaryDriver\Listeners\DebugListener; $driver = CustomImplementation::load('php'); $driver->listen(new DebugListener()); $driver->on('debug', function ($line) { echo $line; });
ProcessBuilderFactory
ProcessBuilderFactory通过生成Symfony [Process] (https://symfony.com.cn/doc/master/components/process.html)对象来简化进程的启动。
use Alchemy\BinaryDriver\ProcessBuilderFactory; $factory = new ProcessBuilderFactory('/usr/bin/php'); // return a Symfony\Component\Process\Process $process = $factory->create('-v'); // echoes '/usr/bin/php' '-v' echo $process->getCommandLine(); $process = $factory->create(array('-r', 'echo "Hello !";')); // echoes '/usr/bin/php' '-r' 'echo "Hello !";' echo $process->getCommandLine();
配置
一个简单的配置对象,提供ArrayAccess
和IteratorAggregate
接口。
use Alchemy\BinaryDriver\Configuration; $conf = new Configuration(array('timeout' => 0)); echo $conf->get('timeout'); if ($conf->has('param')) { $conf->remove('param'); } $conf->set('timeout', 20); $conf->all();
使用ArrayAccess
接口的相同示例
use Alchemy\BinaryDriver\Configuration; $conf = new Configuration(array('timeout' => 0)); echo $conf['timeout']; if (isset($conf['param'])) { unset($conf['param']); } $conf['timeout'] = 20;
许可证
本项目根据MIT许可证发布。