Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
string.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage FileSystem
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 jimport('joomla.filesystem.support.stringcontroller');
13 
14 /**
15  * String Stream Wrapper
16  *
17  * This class allows you to use a PHP string in the same way that
18  * you would normally use a regular stream wrapper
19  *
20  * @package Joomla.Platform
21  * @subpackage FileSystem
22  * @since 11.1
23  */
25 {
26  /**
27  * The current string
28  *
29  * @var string
30  * @since 12.1
31  */
32  protected $currentString;
33 
34  /**
35  *
36  * The path
37  *
38  * @var string
39  * @since 12.1
40  */
41  protected $path;
42 
43  /**
44  *
45  * The mode
46  *
47  * @var string
48  * @since 12.1
49  */
50  protected $mode;
51 
52  /**
53  *
54  * Enter description here ...
55  * @var string
56  *
57  * @since 12.1
58  */
59  protected $options;
60 
61  /**
62  *
63  * Enter description here ...
64  * @var string
65  *
66  * @since 12.1
67  */
68  protected $openedPath;
69 
70  /**
71  * Current position
72  *
73  * @var integer
74  * @since 12.1
75  */
76  protected $pos;
77 
78  /**
79  * Length of the string
80  *
81  * @var string
82  *
83  * @since 12.1
84  */
85  protected $len;
86 
87  /**
88  * Statistics for a file
89  *
90  * @var array
91  * @since 12.1
92  *
93  * @see http://us.php.net/manual/en/function.stat.php
94  */
95  protected $stat;
96 
97  /**
98  * Method to open a file or URL.
99  *
100  * @param string $path The stream path.
101  * @param string $mode Not used.
102  * @param integer $options Not used.
103  * @param string &$opened_path Not used.
104  *
105  * @return boolean
106  *
107  * @since 11.1
108  */
109  public function stream_open($path, $mode, $options, &$opened_path)
110  {
111  $this->currentString = &JStringController::getRef(str_replace('string://', '', $path));
112 
113  if ($this->currentString)
114  {
115  $this->len = strlen($this->currentString);
116  $this->pos = 0;
117  $this->stat = $this->url_stat($path, 0);
118 
119  return true;
120  }
121  else
122  {
123  return false;
124  }
125  }
126 
127  /**
128  * Method to retrieve information from a file resource
129  *
130  * @return array
131  *
132  * @see http://www.php.net/manual/en/streamwrapper.stream-stat.php
133  * @since 11.1
134  */
135  public function stream_stat()
136  {
137  return $this->stat;
138  }
139 
140  /**
141  * Method to retrieve information about a file.
142  *
143  * @param string $path File path or URL to stat
144  * @param integer $flags Additional flags set by the streams API
145  *
146  * @return array
147  *
148  * @see http://php.net/manual/en/streamwrapper.url-stat.php
149  * @since 11.1
150  */
151  public function url_stat($path, $flags = 0)
152  {
153  $now = time();
154  $string = &JStringController::getRef(str_replace('string://', '', $path));
155  $stat = array(
156  'dev' => 0,
157  'ino' => 0,
158  'mode' => 0,
159  'nlink' => 1,
160  'uid' => 0,
161  'gid' => 0,
162  'rdev' => 0,
163  'size' => strlen($string),
164  'atime' => $now,
165  'mtime' => $now,
166  'ctime' => $now,
167  'blksize' => '512',
168  'blocks' => ceil(strlen($string) / 512));
169 
170  return $stat;
171  }
172 
173  /**
174  * Method to read a given number of bytes starting at the current position
175  * and moving to the end of the string defined by the current position plus the
176  * given number.
177  *
178  * @param integer $count Bytes of data from the current position should be returned.
179  *
180  * @return void
181  *
182  * @since 11.1
183  *
184  * @see http://www.php.net/manual/en/streamwrapper.stream-read.php
185  */
186  public function stream_read($count)
187  {
188  $result = substr($this->currentString, $this->pos, $count);
189  $this->pos += $count;
190 
191  return $result;
192  }
193 
194  /**
195  * Stream write, always returning false.
196  *
197  * @param string $data The data to write.
198  *
199  * @return boolean
200  *
201  * @since 11.1
202  * @note Updating the string is not supported.
203  */
204  public function stream_write($data)
205  {
206  // We don't support updating the string.
207  return false;
208  }
209 
210  /**
211  * Method to get the current position
212  *
213  * @return integer The position
214  *
215  * @since 11.1
216  */
217  public function stream_tell()
218  {
219  return $this->pos;
220  }
221 
222  /**
223  * End of field check
224  *
225  * @return boolean True if at end of field.
226  *
227  * @since 11.1
228  */
229  public function stream_eof()
230  {
231  if ($this->pos > $this->len)
232  {
233  return true;
234  }
235 
236  return false;
237  }
238 
239  /**
240  * Stream offset
241  *
242  * @param integer $offset The starting offset.
243  * @param integer $whence SEEK_SET, SEEK_CUR, SEEK_END
244  *
245  * @return boolean True on success.
246  *
247  * @since 11.1
248  */
249  public function stream_seek($offset, $whence)
250  {
251  // $whence: SEEK_SET, SEEK_CUR, SEEK_END
252  if ($offset > $this->len)
253  {
254  // We can't seek beyond our len.
255  return false;
256  }
257 
258  switch ($whence)
259  {
260  case SEEK_SET:
261  $this->pos = $offset;
262  break;
263 
264  case SEEK_CUR:
265  if (($this->pos + $offset) < $this->len)
266  {
267  $this->pos += $offset;
268  }
269  else
270  {
271  return false;
272  }
273  break;
274 
275  case SEEK_END:
276  $this->pos = $this->len - $offset;
277  break;
278  }
279 
280  return true;
281  }
282 
283  /**
284  * Stream flush, always returns true.
285  *
286  * @return boolean
287  *
288  * @since 11.1
289  * @note Data storage is not supported
290  */
291  public function stream_flush()
292  {
293  // We don't store data.
294  return true;
295  }
296 }
297 
298 stream_wrapper_register('string', 'JStreamString') or die('JStreamString Wrapper Registration Failed');