Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
buffer.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Utilities
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Generic Buffer stream handler
14  *
15  * This class provides a generic buffer stream. It can be used to store/retrieve/manipulate
16  * string buffers with the standard PHP filesystem I/O methods.
17  *
18  * @package Joomla.Platform
19  * @subpackage Utilities
20  * @since 11.1
21  */
22 class JBuffer
23 {
24  /**
25  * Stream position
26  *
27  * @var integer
28  * @since 11.1
29  */
30  public $position = 0;
31 
32  /**
33  * Buffer name
34  *
35  * @var string
36  * @since 11.1
37  */
38  public $name = null;
39 
40  /**
41  * Buffer hash
42  *
43  * @var array
44  * @since 12.1
45  */
46  public $buffers = array();
47 
48  /**
49  * Function to open file or url
50  *
51  * @param string $path The URL that was passed
52  * @param string $mode Mode used to open the file @see fopen
53  * @param integer $options Flags used by the API, may be STREAM_USE_PATH and
54  * STREAM_REPORT_ERRORS
55  * @param string &$opened_path Full path of the resource. Used with STREAN_USE_PATH option
56  *
57  * @return boolean
58  *
59  * @since 11.1
60  * @see streamWrapper::stream_open
61  */
62  public function stream_open($path, $mode, $options, &$opened_path)
63  {
64  $url = parse_url($path);
65  $this->name = $url['host'];
66  $this->buffers[$this->name] = null;
67  $this->position = 0;
68 
69  return true;
70  }
71 
72  /**
73  * Read stream
74  *
75  * @param integer $count How many bytes of data from the current position should be returned.
76  *
77  * @return mixed The data from the stream up to the specified number of bytes (all data if
78  * the total number of bytes in the stream is less than $count. Null if
79  * the stream is empty.
80  *
81  * @see streamWrapper::stream_read
82  * @since 11.1
83  */
84  public function stream_read($count)
85  {
86  $ret = substr($this->buffers[$this->name], $this->position, $count);
87  $this->position += strlen($ret);
88 
89  return $ret;
90  }
91 
92  /**
93  * Write stream
94  *
95  * @param string $data The data to write to the stream.
96  *
97  * @return integer
98  *
99  * @see streamWrapper::stream_write
100  * @since 11.1
101  */
102  public function stream_write($data)
103  {
104  $left = substr($this->buffers[$this->name], 0, $this->position);
105  $right = substr($this->buffers[$this->name], $this->position + strlen($data));
106  $this->buffers[$this->name] = $left . $data . $right;
107  $this->position += strlen($data);
108 
109  return strlen($data);
110  }
111 
112  /**
113  * Function to get the current position of the stream
114  *
115  * @return integer
116  *
117  * @see streamWrapper::stream_tell
118  * @since 11.1
119  */
120  public function stream_tell()
121  {
122  return $this->position;
123  }
124 
125  /**
126  * Function to test for end of file pointer
127  *
128  * @return boolean True if the pointer is at the end of the stream
129  *
130  * @see streamWrapper::stream_eof
131  * @since 11.1
132  */
133  public function stream_eof()
134  {
135  return $this->position >= strlen($this->buffers[$this->name]);
136  }
137 
138  /**
139  * The read write position updates in response to $offset and $whence
140  *
141  * @param integer $offset The offset in bytes
142  * @param integer $whence Position the offset is added to
143  * Options are SEEK_SET, SEEK_CUR, and SEEK_END
144  *
145  * @return boolean True if updated
146  *
147  * @see streamWrapper::stream_seek
148  * @since 11.1
149  */
150  public function stream_seek($offset, $whence)
151  {
152  switch ($whence)
153  {
154  case SEEK_SET:
155  if ($offset < strlen($this->buffers[$this->name]) && $offset >= 0)
156  {
157  $this->position = $offset;
158 
159  return true;
160  }
161  else
162  {
163  return false;
164  }
165  break;
166 
167  case SEEK_CUR:
168  if ($offset >= 0)
169  {
170  $this->position += $offset;
171 
172  return true;
173  }
174  else
175  {
176  return false;
177  }
178  break;
179 
180  case SEEK_END:
181  if (strlen($this->buffers[$this->name]) + $offset >= 0)
182  {
183  $this->position = strlen($this->buffers[$this->name]) + $offset;
184 
185  return true;
186  }
187  else
188  {
189  return false;
190  }
191  break;
192 
193  default:
194  return false;
195  }
196  }
197 }
198 // Register the stream
199 stream_wrapper_register('buffer', 'JBuffer');