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