Skip to content

Commit

Permalink
[mms] Add Horde_Stream_Wrapper_Combine::getStream().
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Feb 4, 2014
1 parent 14ab959 commit 650bac7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
50 changes: 45 additions & 5 deletions framework/Stream_Wrapper/lib/Horde/Stream/Wrapper/Combine.php
Expand Up @@ -23,6 +23,9 @@
*/
class Horde_Stream_Wrapper_Combine
{
/**/
const WRAPPER_NAME = 'horde-stream-wrapper-combine';

/**
* Context.
*
Expand Down Expand Up @@ -65,6 +68,40 @@ class Horde_Stream_Wrapper_Combine
*/
protected $_ateof = false;

/**
* Unique ID tracker for the streams.
*
* @var integer
*/
static private $_id = 0;

/**
* Create a stream from multiple data sources.
*
* @since 2.1.0
*
* @param array $data An array of strings and/or streams to combine into
* a single stream.
*
* @return resource A PHP stream.
*/
static public function getStream($data)
{
if (!self::$_id) {
stream_wrapper_register(self::WRAPPER_NAME, __CLASS__);
}

return fopen(
self::WRAPPER_NAME . '://' . ++self::$_id,
'wb',
false,
stream_context_create(array(
self::WRAPPER_NAME => array(
'data' => $data
)
))
);
}
/**
* @see streamWrapper::stream_open()
*
Expand All @@ -78,12 +115,15 @@ class Horde_Stream_Wrapper_Combine
public function stream_open($path, $mode, $options, &$opened_path)
{
$opts = stream_context_get_options($this->context);
if (empty($opts['horde-combine']['data']) ||
!($opts['horde-combine']['data'] instanceof Horde_Stream_Wrapper_CombineStream)) {
throw new Exception('A combined stream must be created using the Horde_Stream_Wrapper_CombineStream interface.');
}

$data = $opts['horde-combine']['data']->getData();
if (isset($opts[self::WRAPPER_NAME]['data'])) {
$data = $opts[self::WRAPPER_NAME]['data'];
} elseif (isset($opts['horde-combine']['data'])) {
// @deprecated
$data = $opts['horde-combine']['data']->getData();
} else {
throw new Exception('Use ' . __CLASS__ . '::getStream() to initialize the stream.');
}

reset($data);
while (list(,$val) = each($data)) {
Expand Down
Expand Up @@ -14,11 +14,12 @@
/**
* Provides access to the CombineStream stream wrapper.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2009-2014 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Stream_Wrapper
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @deprecated Use Horde_Stream_Wrapper_Combine::getStream()
* @copyright 2009-2014 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Stream_Wrapper
*/
interface Horde_Stream_Wrapper_CombineStream
{
Expand Down
2 changes: 2 additions & 0 deletions framework/Stream_Wrapper/package.xml
Expand Up @@ -28,6 +28,7 @@
</stability>
<license uri="http://www.horde.org/licenses/bsd">BSD-2-Clause</license>
<notes>
* [mms] Add Horde_Stream_Wrapper_Combine::getStream().
* [mms] Add Horde_Stream_Wrapper_String::getStream().
* [mms] Fix documentation regarding proper license for this package (BSD).
</notes>
Expand Down Expand Up @@ -226,6 +227,7 @@
<date>2012-11-22</date>
<license uri="http://www.horde.org/licenses/bsd">BSD-2-Clause</license>
<notes>
* [mms] Add Horde_Stream_Wrapper_Combine::getStream().
* [mms] Add Horde_Stream_Wrapper_String::getStream().
* [mms] Fix documentation regarding proper license for this package (BSD).
</notes>
Expand Down

0 comments on commit 650bac7

Please sign in to comment.