Skip to content

Commit

Permalink
Allow stream variable to be dynamically generated, if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Jan 29, 2014
1 parent 45e82e3 commit 6e785a4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
62 changes: 37 additions & 25 deletions framework/Stream/lib/Horde/Stream.php
Expand Up @@ -20,18 +20,11 @@
* @copyright 2012-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Stream
*
* @property-read resource $stream Stream resource (@deprecated).
*/
class Horde_Stream implements Serializable
{
/**
* Stream resource.
*
* @deprecated Will be removed in 2.0.0
*
* @var resource
*/
public $stream;

/**
* Parse character as UTF-8 data instead of single byte.
*
Expand All @@ -48,6 +41,15 @@ class Horde_Stream implements Serializable
*/
protected $_params;

/**
* Stream resource.
*
* @deprecated Will be removed in 2.0.0
*
* @var resource
*/
protected $_stream;

/**
* Constructor.
*
Expand All @@ -65,8 +67,18 @@ public function __construct(array $opts = array())
protected function _init()
{
// Sane default: read-write, 0-length stream.
if (!$this->stream) {
$this->stream = @fopen('php://temp', 'r+');
if (!$this->_stream) {
$this->_stream = @fopen('php://temp', 'r+');
}
}

/**
*/
public function __get($name)
{
switch ($name) {
case 'stream':
return $this->_stream;
}
}

Expand All @@ -75,7 +87,7 @@ protected function _init()
public function __clone()
{
$data = strval($this);
$this->stream = null;
$this->_stream = null;
$this->_init();
$this->add($data);
}
Expand Down Expand Up @@ -120,7 +132,7 @@ public function add($data, $reset = false)
}
$data->seek($dpos, false);
} else {
fwrite($this->stream, $data);
fwrite($this->_stream, $data);
}

if ($reset) {
Expand Down Expand Up @@ -361,8 +373,8 @@ public function substring($start = 0, $length = null, $char = false)
}
}

while (!feof($this->stream) && ($to_end || $length)) {
$read = fread($this->stream, $to_end ? 16384 : $length);
while (!feof($this->_stream) && ($to_end || $length)) {
$read = fread($this->_stream, $to_end ? 16384 : $length);
$out .= $read;
if (!$to_end) {
$length -= strlen($read);
Expand Down Expand Up @@ -411,7 +423,7 @@ public function getEOL()
*/
public function getChar()
{
$char = fgetc($this->stream);
$char = fgetc($this->_stream);
if (!$this->utf8_char) {
return $char;
}
Expand All @@ -432,7 +444,7 @@ public function getChar()
}

for ($i = 0; $i < $n; ++$i) {
if (($c = fgetc($this->stream)) === false) {
if (($c = fgetc($this->_stream)) === false) {
throw new Horde_Stream_Exception('ERROR');
}
$char .= $c;
Expand All @@ -450,7 +462,7 @@ public function getChar()
*/
public function pos()
{
return ftell($this->stream);
return ftell($this->_stream);
}

/**
Expand All @@ -462,7 +474,7 @@ public function pos()
*/
public function rewind()
{
return rewind($this->stream);
return rewind($this->_stream);
}

/**
Expand Down Expand Up @@ -506,7 +518,7 @@ public function seek($offset = 0, $curr = true, $char = false)
$offset = abs($offset);

while ($pos-- && $offset) {
fseek($this->stream, -1, SEEK_CUR);
fseek($this->_stream, -1, SEEK_CUR);
if ((ord($this->peek()) & 0xC0) != 0x80) {
--$offset;
}
Expand All @@ -516,7 +528,7 @@ public function seek($offset = 0, $curr = true, $char = false)
return true;
}

return (fseek($this->stream, $offset, $curr ? SEEK_CUR : SEEK_SET) === 0);
return (fseek($this->_stream, $offset, $curr ? SEEK_CUR : SEEK_SET) === 0);
}

/**
Expand All @@ -530,7 +542,7 @@ public function seek($offset = 0, $curr = true, $char = false)
*/
public function end($offset = 0)
{
return (fseek($this->stream, $offset, SEEK_END) === 0);
return (fseek($this->_stream, $offset, SEEK_END) === 0);
}

/**
Expand All @@ -542,7 +554,7 @@ public function end($offset = 0)
*/
public function eof()
{
return feof($this->stream);
return feof($this->_stream);
}

/**
Expand All @@ -552,8 +564,8 @@ public function eof()
*/
public function close()
{
if ($this->stream) {
fclose($this->stream);
if ($this->_stream) {
fclose($this->_stream);
}
}

Expand Down
2 changes: 1 addition & 1 deletion framework/Stream/lib/Horde/Stream/Existing.php
Expand Up @@ -39,7 +39,7 @@ public function __construct(array $opts = array())
throw new Horde_Stream_Exception('Need a stream resource.');
}

$this->stream = $opts['stream'];
$this->_stream = $opts['stream'];
unset($opts['stream']);

parent::__construct($opts);
Expand Down
2 changes: 1 addition & 1 deletion framework/Stream/lib/Horde/Stream/Temp.php
Expand Up @@ -48,7 +48,7 @@ protected function _init()
$cmd .= '/maxmemory:' . intval($this->_params['max_memory']);
}

if (($this->stream = @fopen($cmd, 'r+')) === false) {
if (($this->_stream = @fopen($cmd, 'r+')) === false) {
throw new Horde_Stream_Exception('Failed to open temporary memory stream.');
}
}
Expand Down

0 comments on commit 6e785a4

Please sign in to comment.