Skip to content

Commit

Permalink
read - may don`t specify length
Browse files Browse the repository at this point in the history
  • Loading branch information
ConConovaloff committed Jun 20, 2016
1 parent 9bc3398 commit c99c179
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 21 deletions.
20 changes: 19 additions & 1 deletion IStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@

interface IStream
{
public function read($length);
/**
* @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();
}
30 changes: 28 additions & 2 deletions Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,42 @@ class Stream implements IStream
*/
private $resource;

/**
* @param resource $resource
*/
public function __construct($resource)
{
$this->resource = $resource;
}

public function read($length)
/**
* {@inheritdoc}
*/
public function read($length = null)
{
return fread($this->resource, $length);
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)) {
Expand All @@ -33,8 +54,13 @@ public function write($data, $length = null)
return fwrite($this->resource, $data, $length);
}

/**
* {@inheritdoc}
*/
public function isEOF()
{
return feof($this->resource);
}

private $dataSegment = 10;
}
6 changes: 5 additions & 1 deletion StreamContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ public function __construct(array $config = null)
}

foreach ($config as $streamName => $streamObj) {
if (!$streamObj instanceof IStream) {
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])) {
Expand Down
60 changes: 44 additions & 16 deletions StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@

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
{
/**
* @var string
*/
private $data;

/**
* @param string $data
*/
public function __construct($data = '')
{
$this->data = $data;
$this->writeData($data);
}

/**
* То что читаем из строки - удаляем
*
* @param int $length
*
* @return mixed
* {@inheritdoc}
*/
public function read($length)
public function read($length = null)
{
if (is_null($length)) {
$result = $this->data;
$this->writeData(null);

return $result;
}

$result = substr($this->data, 0, $length);
$this->data = substr($this->data, $length);
$this->writeData(substr($this->data, $length));

return $result;
}

/**
* {@inheritdoc}
*/
public function readLine()
{
if ($this->isEOF()) {
Expand All @@ -46,11 +51,14 @@ public function readLine()
}

$lineLength = strlen($result);
$this->data = substr($this->data, $lineLength);
$this->writeData(substr($this->data, $lineLength));

return $result;
}

/**
* {@inheritdoc}
*/
public function write($data, $length = null)
{
if (is_null($length)) {
Expand All @@ -59,13 +67,33 @@ public function write($data, $length = null)
$write = substr($data, 0, $length);
}

$this->data .= $write;
$this->writeData($this->data . $write);

return strlen($write);
}

/**
* {@inheritdoc}
*/
public function isEOF()
{
return $this->data === '' || $this->data === false;
return $this->data === '';
}

/**
* @param mixed $data
*/
private function writeData($data)
{
if (!is_string($data)) {
$data = '';
}

$this->data = $data;
}

/**
* @var string
*/
private $data;
}
17 changes: 16 additions & 1 deletion tests/EnvFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use axy\env\Env;
use axy\env\Stream;
use axy\env\StreamContainer;

/**
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
Expand Down Expand Up @@ -245,6 +244,22 @@ public function testStreamRead()
$this->assertSame($string, $readResult);
}

public function testStreamReadWithoutLength()
{
$string = "one\ntwo\nthree";
$resource = $this->getMemoryResourceWithString($string);
$stream = new Stream($resource);

$env = new Env([
'streams' => [
'stdin' => $stream
]
]);

$this->assertSame($string, $env->streams->stdin->read());
}


public function testReadLine()
{
$string = "one line\ntwo line";
Expand Down
14 changes: 14 additions & 0 deletions tests/StreamTestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ public function testRead()
$this->assertSame($string, $result);
}

public function testReadWithoutLength()
{
$string = "one\ntwo\nthree";
$streamTest = new StreamTest($string);

$env = new Env([
'streams' => [
'stdin' => $streamTest
]
]);

$this->assertSame($string, $env->streams->stdin->read());
}

public function testReadLine()
{
$string = "One line\nTwo line";
Expand Down

0 comments on commit c99c179

Please sign in to comment.