Skip to content

Commit

Permalink
Merge pull request #1 from ConConovaloff/streams
Browse files Browse the repository at this point in the history
add resource handlers
  • Loading branch information
vasa-c committed Jun 20, 2016
2 parents 2b87fad + c99c179 commit 5d577e8
Show file tree
Hide file tree
Showing 10 changed files with 551 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ public function __construct(array $config = null)
* @var array
*/
public $files;

/**
* @var StreamContainer
*/
public $streams;
}
20 changes: 15 additions & 5 deletions Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace axy\env;

use axy\env\helpers\Normalizer;
use axy\errors\InvalidConfig;
use axy\errors\FieldNotExist;
use axy\errors\Disabled;
use axy\errors\ContainerReadOnly;
use axy\errors\Disabled;
use axy\errors\FieldNotExist;
use axy\errors\InvalidConfig;

/**
* Wrapper for the environment
Expand All @@ -21,6 +21,7 @@
* @property-read array $post
* @property-read array $request
* @property-read array $cookie
* @property-read StreamContainer $streams
* @method void header(string $string, bool $replace = true, int $http_response_code = null)
* @method bool setcookie(string $name,string $val,int $e=0,string $p=null,string $d=null,bool $s=false,bool $h = false)
* @method array getallheaders()
Expand Down Expand Up @@ -56,8 +57,8 @@ public function __construct($config = null)
} else {
throw new InvalidConfig('Env', 'config must be an array or an instance of \axy\env\Config');
}
Normalizer::normalize($config);
}
Normalizer::normalize($config);
$this->config = $config;
$this->initTime();
}
Expand Down Expand Up @@ -199,5 +200,14 @@ private function initTime()
/**
* @var string[]
*/
private static $envArrays = ['server', 'env', 'get', 'post', 'request', 'cookie', 'files'];
private static $envArrays = [
'server',
'env',
'get',
'post',
'request',
'cookie',
'files',
'streams',
];
}
29 changes: 29 additions & 0 deletions IStream.php
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();
}
66 changes: 66 additions & 0 deletions Stream.php
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;
}
55 changes: 55 additions & 0 deletions StreamContainer.php
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 = [];
}
99 changes: 99 additions & 0 deletions StreamTest.php
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;
}
13 changes: 13 additions & 0 deletions helpers/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
namespace axy\env\helpers;

use axy\env\Config;
use axy\env\Stream;
use axy\env\StreamContainer;
use axy\errors\InvalidConfig;
use axy\errors\NotValid;

/**
* Normalizer of config
Expand All @@ -25,6 +28,7 @@ public static function normalize(Config $config)
self::normalizeFunctions($config);
self::normalizeTime($config);
self::normalizeArrays($config);
self::normalizeStreams($config);
}

/**
Expand Down Expand Up @@ -91,4 +95,13 @@ private static function normalizeArrays(Config $config)
}
}
}

private static function normalizeStreams(Config $config)
{
if ($config->streams instanceof StreamContainer) {
return;
}

$config->streams = new StreamContainer($config->streams);
}
}
Loading

0 comments on commit 5d577e8

Please sign in to comment.