-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Berstein
committed
Jul 14, 2011
1 parent
f4b3e43
commit ee6003f
Showing
4 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |