From: Chuck Hagenbuch Date: Tue, 15 Sep 2009 02:45:27 +0000 (-0400) Subject: Add Horde_Support_StringStream and the supporting Horde_Stream_Wrapper_String class X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=deea181902fc91992730534fc05bdf246608e8d4;p=horde.git Add Horde_Support_StringStream and the supporting Horde_Stream_Wrapper_String class Provides a way to treat a string variable as a stream without duplicating it or using global scope. --- diff --git a/framework/Stream_Wrapper/lib/Horde/Stream/Wrapper/String.php b/framework/Stream_Wrapper/lib/Horde/Stream/Wrapper/String.php new file mode 100644 index 000000000..fba4b716e --- /dev/null +++ b/framework/Stream_Wrapper/lib/Horde/Stream/Wrapper/String.php @@ -0,0 +1,139 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Stream_Wrapper + */ + +/** + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Stream_Wrapper + */ +class Horde_Stream_Wrapper_String +{ + /** + * @var resource + */ + public $context; + + /** + * @var string + */ + protected $_string; + + /** + * @var integer + */ + protected $_length; + + /** + * @var integer + */ + protected $_position; + + /** + * @param string $path + * @param string $mode + * @param integer $options + * @param string &$opened_path + */ + public function stream_open($path, $mode, $options, &$opened_path) + { + $options = stream_context_get_options($this->context); + if (empty($options['horde-string']['string']) || ! $options['horde-string']['string'] instanceof Horde_Support_StringStream) { + throw new Exception('String streams must be created using the Horde_Stream_Wrapper_String_Factory class'); + } + + $this->_string =& $options['horde-string']['string']->getString(); + if (is_null($this->_string)) { + return false; + } + + $this->_length = strlen($this->_string); + $this->_position = 0; + return true; + } + + /** + * @return boolean + */ + public function stream_stat() + { + return false; + } + + /** + * @param integer $count + * + * @return string + */ + public function stream_read($count) + { + $current = $this->_position; + $this->_position += $count; + return substr($this->_string, $current, $count); + } + + /** + * @param string $data + * + * @return integer + */ + public function stream_write($data) + { + return strlen($data); + } + + /** + * @return integer + */ + public function stream_tell() + { + return $this->_position; + } + + /** + * @return boolean + */ + public function stream_eof() + { + return ($this->_position > $this->_length); + } + + /** + * @param integer $offset + * @param integer $whence SEEK_SET, SEEK_CUR, or SEEK_END + */ + public function stream_seek($offset, $whence) + { + if ($offset > $this->_length) { + return false; + } + + switch ($whence) { + case SEEK_SET: + $this->_position = $offset; + break; + + case SEEK_CUR: + $target = $this->_position + $offset; + if ($target < $this->_length) { + $this->_position = $target; + } else { + return false; + } + break; + + case SEEK_END: + $this->_position = $this->_length - $offset; + break; + } + + return true; + } +} diff --git a/framework/Stream_Wrapper/package.xml b/framework/Stream_Wrapper/package.xml new file mode 100644 index 000000000..9a4de92b2 --- /dev/null +++ b/framework/Stream_Wrapper/package.xml @@ -0,0 +1,70 @@ + + + Stream_Wrapper + pear.horde.org + Horde Stream filters + This package provides various stream filters. + + + Chuck Hagenbuch + chuck + chuck@horde.org + yes + + + Jan Schneider + jan + jan@horde.org + yes + + + Michael Slusarz + slusarz + slusarz@horde.org + yes + + 2009-07-08 + + 0.1.0 + 0.1.0 + + + beta + beta + + LGPL + * Initial release. + + + + + + + + + + + + + + + + + + 5.2.0 + + + 1.7.0 + + + + + + + + + + diff --git a/framework/Support/lib/Horde/Support/StringStream.php b/framework/Support/lib/Horde/Support/StringStream.php new file mode 100644 index 000000000..a4927df92 --- /dev/null +++ b/framework/Support/lib/Horde/Support/StringStream.php @@ -0,0 +1,67 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Support + */ + +/** + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Support + */ +class Horde_Support_StringStream +{ + /** + * @var string + */ + protected $_string; + + /** + * Constructor + * + * @param string &$string Reference to the string to wrap as a stream + */ + public function __construct(&$string) + { + $this->installWrapper(); + $this->_string =& $string; + } + + /** + * Return a stream handle to this string stream. + * + * @return resource + */ + public function fopen() + { + $context = stream_context_create(array('horde-string' => array('string' => $this))); + return fopen('horde-string://' . spl_object_hash($this), 'rb', false, $context); + } + + /** + * Install the horde-string stream wrapper if it isn't already registered. + */ + public function installWrapper() + { + if (!in_array('horde-string', stream_get_wrappers())) { + if (!stream_wrapper_register('horde-string', 'Horde_Stream_Wrapper_String')) { + throw new Exception('Unable to register horde-string stream wrapper'); + } + } + } + + /** + * Return a reference to the wrapped string. + * + * @return string + */ + public function &getString() + { + return $this->_string; + } +} diff --git a/framework/Support/package.xml b/framework/Support/package.xml index d88274013..befa48075 100644 --- a/framework/Support/package.xml +++ b/framework/Support/package.xml @@ -30,6 +30,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> * Initial Horde_Support_ConsistentHash object * Initial Horde_Support_Inflector object * Initial Horde_Support_Stack object + * Initial Horde_Support_StringStream object * Initial Horde_Support_Stub object * Initial Horde_Support_Timer object * Initial Horde_Support_Uuid object @@ -51,6 +52,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -68,6 +70,12 @@ http://pear.php.net/dtd/package-2.0.xsd"> 1.5.0 + + + Stream_Wrapper + pear.horde.org + + @@ -78,6 +86,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> +