• Stars
    star
    187
  • Rank 206,464 (Top 5 %)
  • Language
    PHP
  • License
    Other
  • Created over 11 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Binary driver utilities

Binary Driver

Binary-Driver is a set of PHP tools to build binary drivers.

Build Status

Why ?

You may wonder Why building a library while I can use exec or symfony/process ?.

Here is a simple answer :

  • If you use exec, passthru, system, proc_open or any low level process handling in PHP, you should have a look to symfony/process component that will provide an OO portable, testable and secure interface to deal with this. It seems easy at first approach, but if you look at this component unit tests, you will see that handling process in a simple interface can easily become a nightmare.

  • If you already use symfony/process, and want to build binary drivers, you will always have the same common set of methods and objects to configure, log, debug, and generate processes. This library is a base to implement any binary driver with this common set of needs.

AbstractBinary

AbstractBinary provides an abstract class to build a binary driver. It implements BinaryInterface.

Implementation example :

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')));

Binary detection troubleshooting

If you are using Nginx with PHP-fpm, executable detection may not work because of an empty $_ENV['path']. To avoid having an empty PATH environment variable, add the following line to your fastcgi_params config file (replace /your/current/path/ with the output of printenv PATH) :

fastcgi_param    PATH    /your/current/path

Logging

You can log events with a Psr\Log\LoggerInterface by passing it in the load method as second argument :

$logger = new Monolog\Logger('driver');
$driver = Driver::load('ls', $logger);

Listeners

You can add custom listeners on processes. Listeners are built on top of Evenement and must implement 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);

Bundled listeners

The debug listener is a simple listener to catch stderr and stdout outputs ; read the implementation for customization.

use Alchemy\BinaryDriver\Listeners\DebugListener;

$driver = CustomImplementation::load('php');
$driver->listen(new DebugListener());

$driver->on('debug', function ($line) {
    echo $line;
});

ProcessBuilderFactory

ProcessBuilderFactory ease spawning processes by generating Symfony [Process] (http://symfony.com/doc/master/components/process.html) objects.

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

Configuration

A simple configuration object, providing an ArrayAccess and IteratorAggregate interface.

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

Same example using the ArrayAccess interface :

use Alchemy\BinaryDriver\Configuration;

$conf = new Configuration(array('timeout' => 0));

echo $conf['timeout'];

if (isset($conf['param'])) {
    unset($conf['param']);
}

$conf['timeout'] = 20;

License

This project is released under the MIT license.

More Repositories

1

Zippy

PHP zip/tar/bz2 archives (de)compression library with commandline or extensions
PHP
464
star
2

Phraseanet

Digital Asset Management PHP app
PHP
243
star
3

PHP-Unoconv

PHP
73
star
4

Media-Alchemyst

PHP
64
star
5

PHP-dataURI

A PHP dataURI library, RFC 2397 Compliant
PHP
59
star
6

GeonamesServer

A node Geonames server
CSS
42
star
7

RabbitMQ-Management-API-Client

A OO PHP client for the RabbitMQ Management Plugin API
PHP
40
star
8

PHP-XPDF

An object oriented wrapper for XPDF
PHP
37
star
9

PHPExiftool

A PHP Exiftool driver
PHP
31
star
10

Ghostscript-PHP

PHP
29
star
11

exiftool

Phraseanet dependency repository
Perl
27
star
12

PHPSwftools

PHP Swftools driver
PHP
20
star
13

PHP-MP4Box

PHP
15
star
14

Phraseanet-Docs

PHP
10
star
15

phrasea

Phrasea
PHP
10
star
16

gdata

Zend Gdata 1.11
PHP
8
star
17

phraseanet-docker

Phraseanet docker deployement
Shell
7
star
18

Phraseanet-PHP-SDK

PHP
7
star
19

GeonamesServer-PHP-Plugin

PHP
6
star
20

Phraseanet-Extension

PHP Extension for Phraseanet
C
6
star
21

PHPMailer_v5.1

Phraseanet dependency repository
PHP
6
star
22

google-plus-api-client

PHP
5
star
23

Phlickr

Phraseanet dependency repository
PHP
3
star
24

tcpdf

Phraseanet dependency repository
PHP
3
star
25

Phraseanet-Indexer

Phraseanet Indexer
C
3
star
26

task-manager

PHP
3
star
27

zippy-svc

PHP
3
star
28

lazy-command-bundle

Bundle for better dependency injection with Symfony commands
PHP
2
star
29

acl-bundle

PHP
2
star
30

resource-component

Resource component
PHP
2
star
31

Documentation-Boilerplate

A sphinx documentation boilerplate for Alchemy open source projects
Python
2
star
32

Showcase

PHP
1
star
33

Blender

PHP
1
star
34

Phraseanet-PHP-SDK-Silex-Provider

Phraseanet PHP SDK Silex Provider
PHP
1
star
35

Phraseanet-Wordpress-Plugin

Wordpress Plugin for Phraseanet
JavaScript
1
star
36

MoovRelocator

Phraseanet dependency repository
PHP
1
star
37

queue-component

Queue component
PHP
1
star
38

PHPShortener

Phraseanet dependency repository
PHP
1
star
39

pipeline-component

Pipeline component
PHP
1
star
40

phraseanet-bundle

Phraseanet Bundle
PHP
1
star
41

rest-bundle

PHP
1
star
42

SphinxSearch-PHP-SDK

Phraseanet dependency repository
PHP
1
star
43

grunt-js-render

Grunt plugin to embed JSRender templates in Javascript files
JavaScript
1
star
44

embed-bundle

TypeScript
1
star