1: <?php
2: /**
3: * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
4: *
5: * @author Chuck Hagenbuch <chuck@horde.org>
6: * @category Horde
7: * @license http://www.horde.org/licenses/bsd BSD
8: * @package Support
9: */
10:
11: /**
12: * @author Chuck Hagenbuch <chuck@horde.org>
13: * @category Horde
14: * @license http://www.horde.org/licenses/bsd BSD
15: * @package Support
16: */
17: class Horde_Support_StringStream implements Horde_Stream_Wrapper_StringStream
18: {
19: /* Wrapper name. */
20: const WNAME = 'horde-string';
21:
22: /**
23: * String data.
24: *
25: * @var string
26: */
27: protected $_string;
28:
29: /**
30: * Constructor
31: *
32: * @param string &$string Reference to the string to wrap as a stream
33: */
34: public function __construct(&$string)
35: {
36: $this->installWrapper();
37: $this->_string =& $string;
38: }
39:
40: /**
41: * Return a stream handle to this string stream.
42: *
43: * @return resource
44: */
45: public function fopen()
46: {
47: return fopen(
48: self::WNAME . '://' . spl_object_hash($this),
49: 'rb',
50: false,
51: stream_context_create(array(
52: self::WNAME => array(
53: 'string' => $this
54: )
55: ))
56: );
57: }
58:
59: /**
60: * Return an SplFileObject representing this string stream
61: *
62: * @return SplFileObject
63: */
64: public function getFileObject()
65: {
66: return new SplFileObject(
67: self::WNAME . '://' . spl_object_hash($this),
68: 'rb',
69: false,
70: stream_context_create(array(
71: self::WNAME => array(
72: 'string' => $this
73: )
74: ))
75: );
76: }
77:
78: /**
79: * Install the stream wrapper if it isn't already registered.
80: */
81: public function installWrapper()
82: {
83: if (!in_array(self::WNAME, stream_get_wrappers()) &&
84: !stream_wrapper_register(self::WNAME, 'Horde_Stream_Wrapper_String')) {
85: throw new Exception('Unable to register stream wrapper.');
86: }
87: }
88:
89: /**
90: * Return a reference to the wrapped string.
91: *
92: * @return string
93: */
94: public function &getString()
95: {
96: return $this->_string;
97: }
98:
99: }
100: