-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ConConovaloff/streams
add resource handlers
- Loading branch information
Showing
10 changed files
with
551 additions
and
5 deletions.
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
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,29 @@ | ||
<?php | ||
|
||
namespace axy\env; | ||
|
||
interface IStream | ||
{ | ||
/** | ||
* @param int $length | ||
* @return string|bool | ||
*/ | ||
public function read($length = null); | ||
|
||
/** | ||
* @return string|bool | ||
*/ | ||
public function readLine(); | ||
|
||
/** | ||
* @param string $data | ||
* @param int $length | ||
* @return int | ||
*/ | ||
public function write($data, $length = null); | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isEOF(); | ||
} |
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,66 @@ | ||
<?php | ||
|
||
namespace axy\env; | ||
|
||
class Stream implements IStream | ||
{ | ||
/** | ||
* @var resource | ||
*/ | ||
private $resource; | ||
|
||
/** | ||
* @param resource $resource | ||
*/ | ||
public function __construct($resource) | ||
{ | ||
$this->resource = $resource; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($length = null) | ||
{ | ||
if (!is_null($length)) { | ||
return fread($this->resource, $length); | ||
} | ||
|
||
$result = ''; | ||
while (!feof($this->resource)) { | ||
$result .= fread($this->resource, $this->dataSegment); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function readLine() | ||
{ | ||
return fgets($this->resource); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($data, $length = null) | ||
{ | ||
if (is_null($length)) { | ||
return fwrite($this->resource, $data); | ||
} | ||
|
||
return fwrite($this->resource, $data, $length); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isEOF() | ||
{ | ||
return feof($this->resource); | ||
} | ||
|
||
private $dataSegment = 10; | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace axy\env; | ||
|
||
use axy\errors\FieldNotExist; | ||
use axy\errors\NotValid; | ||
|
||
/** | ||
* @property-read IStream $stdin | ||
* @property-read IStream $stdout | ||
* @property-read IStream $stderr | ||
*/ | ||
class StreamContainer | ||
{ | ||
public function __construct(array $config = null) | ||
{ | ||
if (is_null($config)) { | ||
$config = []; | ||
} | ||
|
||
foreach ($config as $streamName => $streamObj) { | ||
if (!($streamObj instanceof IStream)) { | ||
throw new NotValid($streamObj, $streamName . " don't implement IStream"); | ||
} | ||
|
||
$this->streamList[$streamName] = $streamObj; | ||
} | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return mixed | ||
*/ | ||
public function __get($name) | ||
{ | ||
if (isset($this->streamList[$name])) { | ||
return $this->streamList[$name]; | ||
} | ||
|
||
if (isset($this->streamDefault[$name])) { | ||
$this->streamList[$name] = new Stream(fopen('php://' . $name, $this->streamDefault[$name])); | ||
return $this->streamList[$name]; | ||
} | ||
|
||
throw new FieldNotExist($name, $this, null, $this); | ||
} | ||
|
||
private $streamDefault = [ | ||
'stdin' => 'w', | ||
'stdout' => 'r', | ||
'stderr' => 'w' | ||
]; | ||
|
||
private $streamList = []; | ||
} |
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,99 @@ | ||
<?php | ||
|
||
namespace axy\env; | ||
|
||
/** | ||
* when we read - we read from start of data and remove what we read | ||
* when we write - we concatenate to end | ||
*/ | ||
class StreamTest implements IStream | ||
{ | ||
/** | ||
* @param string $data | ||
*/ | ||
public function __construct($data = '') | ||
{ | ||
$this->writeData($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($length = null) | ||
{ | ||
if (is_null($length)) { | ||
$result = $this->data; | ||
$this->writeData(null); | ||
|
||
return $result; | ||
} | ||
|
||
$result = substr($this->data, 0, $length); | ||
$this->writeData(substr($this->data, $length)); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function readLine() | ||
{ | ||
if ($this->isEOF()) { | ||
return false; | ||
} | ||
|
||
$lineList = preg_split("/(\\r\\n|\\r|\\n)/", $this->data, -1, PREG_SPLIT_DELIM_CAPTURE); | ||
$result = $lineList[0]; | ||
|
||
if (isset($lineList[1])) { | ||
$result .= $lineList[1]; | ||
} | ||
|
||
$lineLength = strlen($result); | ||
$this->writeData(substr($this->data, $lineLength)); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($data, $length = null) | ||
{ | ||
if (is_null($length)) { | ||
$write = substr($data, 0); | ||
} else { | ||
$write = substr($data, 0, $length); | ||
} | ||
|
||
$this->writeData($this->data . $write); | ||
|
||
return strlen($write); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isEOF() | ||
{ | ||
return $this->data === ''; | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
*/ | ||
private function writeData($data) | ||
{ | ||
if (!is_string($data)) { | ||
$data = ''; | ||
} | ||
|
||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $data; | ||
} |
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
Oops, something went wrong.