pbmedia/binary-driver

此包已被弃用且不再维护。未建议替代包。

一组构建二进制驱动程序的工具

3.0.1 2019-09-12 13:57 UTC

This package is auto-updated.

Last update: 2020-09-12 16:16:16 UTC


README

Binary-Driver 是一组 PHP 工具,用于构建二进制驱动程序。

Build Status

为什么?

您可能会想,为什么我要构建一个库,而不是使用 execsymfony/process 呢?

这里有一个简单的回答

  • 如果您使用 execpassthrusystemproc_open 或 PHP 中的任何低级进程处理,您应该查看 symfony/process 组件,它将提供一个面向对象、可移植、可测试和安全的接口来处理这些问题。乍一看似乎很简单,但如果您查看这个组件的 单元测试,您会发现通过简单接口处理进程很容易变成一场噩梦。

  • 如果您已经使用 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 传递到加载方法的第二个参数来使用 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);

捆绑监听器

调试监听器是一个简单的监听器,用于捕获 stderrstdout 输出;阅读实现以进行自定义。

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.ac.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();

配置

一个简单的配置对象,提供 ArrayAccessIteratorAggregate 接口。

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许可证发布。