2017 © Pedro Peláez
 

library process

Tiny proc_open wrapper for PHP

image

moccalotto/process

Tiny proc_open wrapper for PHP

  • Saturday, January 28, 2017
  • by moccalotto
  • Repository
  • 1 Watchers
  • 0 Stars
  • 92 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 3 % Grown

The README.md

Process

Build Status, (*1)

A PHP process execution helper., (*2)

It assists in I/O from processes. See examples below or run demo.php to see for yourself., (*3)

Installation

To add this package as a local, per-project dependency to your project, simply add a dependency on moccalotto/process to your project's composer.json file like so:, (*4)

{
    "require": {
        "moccalotto/process": "~0.7"
    }
}

Alternatively simply call composer require moccalotto/process, (*5)

Demo

#!/usr/bin/env php
<?php

use Moccalotto\Process\Process;

require 'vendor/autoload.php';



/*
| Execute ls
|------------
| Execute ls and capture the output.
| Then print all the captured output.
*/
$proc = Process::execute('ls -a');
echo $proc->readToEnd();

echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;

/*
| Execute cat and talk to it interactively
|------------------------------------------
| Execute cat, and input a few lines into stdin.
| Cat should echo those lines out to stdout.
| Then, we read those lines from stdout.
| Then we ask cat to terminate.
| Then we print cat's exitcode.
*/
$proc = Process::execute('cat');
$proc->write('========================' . PHP_EOL);
$proc->write('====   CAT EXAMPLE  ====' . PHP_EOL);
$proc->write('==== cat is working ====' . PHP_EOL);
$proc->write('========================' . PHP_EOL);

// read the lines again.
// Beware that this process is blocking.
// if you call readline on a process that
// has not yet printed a newline character,
// your program will hang!
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();

$proc->signalStop();
echo $proc->readErrorToEnd();
echo PHP_EOL;
printf('Exit code: %d', $proc->exitCode());

echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;

/*
| Execute a terminal and run commands in it
|-------------------------------------------
| Execute bash.
| Run a few commands inside it, and capture their output.
| Then wait for the commands to finish executing,
| Then run exit to make bash exit neatly.
| Then ask bash to terminate if it has not executed the 'exit' command yet.
| Then print all the captured output.
*/
$proc = Process::execute('/bin/bash');
$proc->write('echo "=========================="' . PHP_EOL);
$proc->write('echo "====   BASH EXAMPLE   ===="' . PHP_EOL);
$proc->write('echo "==== several commands ===="' . PHP_EOL);
$proc->write('echo "====    in a shell    ===="' . PHP_EOL);
$proc->write('echo "=========================="' . PHP_EOL);
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
$proc->write('ls' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('uname' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('export | head' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('echo bye...' . PHP_EOL);
$proc->write("exit");

// give all commands time to execute.
usleep(100000);

$proc->signalStop();
echo $proc->readToEnd();

The Versions

28/01 2017

dev-master

9999999-dev

Tiny proc_open wrapper for PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

php process proc_open

28/01 2017

0.7.0

0.7.0.0

Tiny proc_open wrapper for PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

php process proc_open

01/12 2015

0.6.2

0.6.2.0

Tiny proc_open wrapper for PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

php process proc_open

04/09 2015

v0.6

0.6.0.0

Tiny proc_open wrapper for PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

php process proc_open

04/09 2015

0.6.1

0.6.1.0

Tiny proc_open wrapper for PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

php process proc_open