Skip to content

Commit

Permalink
Added Sfw\Process class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berstein committed Jul 14, 2011
1 parent f4b3e43 commit ee6003f
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Sfw/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function emit()
$payload = null;
if (file_exists($layout)) {
// Replace placeholders
$placeholders = $this->_getPlaceholders()
$placeholders = $this->_getPlaceholders();
foreach ($placeholders as $name => $value) {
$payload = preg_replace('/{' . preg_quote($name, '/') . '}/', $value);
}
Expand Down
109 changes: 109 additions & 0 deletions lib/Sfw/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Sfw;

use Sfw\Process\Pipes;

class Process
{
protected $_command;
protected $_pipes;

public function __construct($command = null)
{
if (!is_null($command)) {
$this->setCommand($command);
}
}

public function setCommand($command)
{
$this->_command = $command;

// Clear pipes
$this->_pipes = array(
\Sfw\Process\Pipes::STDIN => null,
\Sfw\Process\Pipes::STDOUT => null,
\Sfw\Process\Pipes::STDERR => null,
);

return $this;
}

public function getCommand()
{
return $this->_command;
}

public function execute($stdin = null, $cwd = null, $env = null)
{
$this->write(Process\Pipes::STDIN, $stdin);

$spec = array(
Process\Pipes::STDIN => array('pipe', 'r'),
Process\Pipes::STDOUT => array('pipe', 'w'),
Process\Pipes::STDERR => array('pipe', 'w'),
);

$resource = proc_open(
$this->getCommand(),
$spec,
$pipes,
$cwd,
$env
);

if (!is_resource($resource)) {
throw new \Exception(
'No process created!'
);
}

$return = fwrite(
$pipes[Process\Pipes::STDIN],
$this->_pipes[Process\Pipes::STDIN]
);

fflush($pipes[Process\Pipes::STDIN]);
fclose($pipes[Process\Pipes::STDIN]);

// Read command's STDOUT/STDERR
$data = array(
Process\Pipes::STDOUT => null,
Process\Pipes::STDERR => null,
);

$processPipes = new Process\Pipes;
foreach ($data as $fd => &$content) {
$handle = $pipes[$fd];

while (!feof($handle)) {
$content .= fgetc($handle);
}

$processPipes->set($fd, $content);
}

$status = proc_close($resource);

return new Process\Result($status, $processPipes);
}

protected function write($pipe, $data)
{
if (array_key_exists($pipe, $this->_pipes)) {
$this->_pipes[$pipe] = $data;
}

return $this;
}

public function read($pipe, $default = null)
{
if (array_key_exists($pipe, $this->_pipes)) {
$default = $this->_pipes[$pipe];
}

return $default;
}
}
28 changes: 28 additions & 0 deletions lib/Sfw/Process/Pipes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Sfw\Process;

class Pipes
{
const STDIN = 0;
const STDOUT = 1;
const STDERR = 2;

protected $_pipes = array();

public function set($pipe, $data)
{
$this->_pipes[$pipe] = $data;

return $this;
}

public function get($pipe, $default = null)
{
if (array_key_exists($pipe, $this->_pipes)) {
$default = $this->_pipes[$pipe];
}

return $default;
}
}
25 changes: 25 additions & 0 deletions lib/Sfw/Process/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Sfw\Process;

class Result
{
protected $_status;
protected $_pipes;

public function __construct($status, Pipes $pipes)
{
$this->_status = $status;
$this->_pipes = $pipes;
}

public function getStatus()
{
return $this->_status;
}

public function get($pipe = Pipes::STDOUT)
{
return $this->_pipes->get($pipe);
}
}

0 comments on commit ee6003f

Please sign in to comment.