1: <?php
2: /**
3: * Provides access to the Combine stream wrapper.
4: *
5: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
6: *
7: * @author Michael Slusarz <slusarz@horde.org>
8: * @license http://www.horde.org/licenses/bsd BSD
9: * @category Horde
10: * @package Support
11: */
12:
13: /**
14: * @author Michael Slusarz <slusarz@horde.org>
15: * @license http://www.horde.org/licenses/bsd BSD
16: * @category Horde
17: * @package Support
18: */
19: class Horde_Support_CombineStream implements Horde_Stream_Wrapper_CombineStream
20: {
21: /**
22: * Data.
23: *
24: * @var array
25: */
26: protected $_data;
27:
28: /**
29: * Constructor
30: *
31: * @param array $data An array of strings and/or streams to combine into
32: * a single stream.
33: */
34: public function __construct($data)
35: {
36: $this->installWrapper();
37: $this->_data = $data;
38: }
39:
40: /**
41: * Return a stream handle to this stream.
42: *
43: * @return resource
44: */
45: public function fopen()
46: {
47: $context = stream_context_create(array('horde-combine' => array('data' => $this)));
48: return fopen('horde-combine://' . spl_object_hash($this), 'rb', false, $context);
49: }
50:
51: /**
52: * Return an SplFileObject representing this stream
53: *
54: * @return SplFileObject
55: */
56: public function getFileObject()
57: {
58: $context = stream_context_create(array('horde-combine' => array('data' => $this)));
59: return new SplFileObject('horde-combine://' . spl_object_hash($this), 'rb', false, $context);
60: }
61:
62: /**
63: * Install the horde-combine stream wrapper if it isn't already
64: * registered.
65: *
66: * @throws Exception
67: */
68: public function installWrapper()
69: {
70: if (!in_array('horde-combine', stream_get_wrappers()) &&
71: !stream_wrapper_register('horde-combine', 'Horde_Stream_Wrapper_Combine')) {
72: throw new Exception('Unable to register horde-combine stream wrapper.');
73: }
74: }
75:
76: /**
77: * Return a reference to the data.
78: *
79: * @return array
80: */
81: public function getData()
82: {
83: return $this->_data;
84: }
85:
86: }
87: