Hoa central
Stringbuffer.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Stringbuffer;
38 
39 use Hoa\Core;
40 use Hoa\Stream;
41 
50 abstract class Stringbuffer
51  extends Stream
52  implements Stream\IStream\Bufferable,
55 {
61  private static $_i = 0;
62 
63 
64 
72  public function __construct($streamName = null)
73  {
74  if (null === $streamName) {
75  $streamName = 'hoa://Library/Stringbuffer#' . self::$_i++;
76  }
77 
78  parent::__construct($streamName, null);
79 
80  return;
81  }
82 
92  protected function &_open($streamName, Stream\Context $context = null)
93  {
94  if (false === $out = @tmpfile()) {
95  throw new Exception('Failed to open a string buffer.', 0);
96  }
97 
98  return $out;
99  }
100 
106  protected function _close()
107  {
108  return @fclose($this->getStream());
109  }
110 
119  public function newBuffer($callable = null, $size = null)
120  {
121  $this->setStreamBuffer($size);
122 
123  //@TODO manage $callable as a filter?
124 
125  return 1;
126  }
127 
133  public function flush()
134  {
135  return fflush($this->getStream());
136  }
137 
143  public function deleteBuffer()
144  {
145  return $this->disableStreamBuffer();
146  }
147 
153  public function getBufferLevel()
154  {
155  return 1;
156  }
157 
163  public function getBufferSize()
164  {
165  return $this->getStreamBufferSize();
166  }
167 
175  public function lock($operation)
176  {
177  return flock($this->getStream(), $operation);
178  }
179 
185  public function rewind()
186  {
187  return rewind($this->getStream());
188  }
189 
198  public function seek($offset, $whence = Stream\IStream\Pointable::SEEK_SET)
199  {
200  return fseek($this->getStream(), $offset, $whence);
201  }
202 
208  public function tell()
209  {
210  $stream = $this->getStream();
211 
212  if (null === $stream) {
213  return 0;
214  }
215 
216  return ftell($stream);
217  }
218 
225  public function initializeWith($string)
226  {
227  ftruncate($this->getStream(), 0);
228  fwrite($this->getStream(), $string, strlen($string));
229  $this->rewind();
230 
231  return $this;
232  }
233 }
234 
243 class _Protocol extends Core\Protocol
244 {
250  protected $_name = 'Stringbuffer';
251 
252 
253 
260  public function reachId($id)
261  {
262  $stream = resolve(
263  'hoa://Library/Stream#hoa://Library/Stringbuffer#' . $id
264  );
265 
266  if (null === $stream) {
267  return null;
268  }
269 
270  $meta = $stream->getStreamMetaData();
271 
272  return $meta['uri'];
273  }
274 }
275 
279 Core\Consistency::flexEntity('Hoa\Stringbuffer\Stringbuffer');
280 
285 $protocol = Core::getInstance()->getProtocol();
286 $protocol['Library'][] = new _Protocol();
& _open($streamName, Stream\Context $context=null)
newBuffer($callable=null, $size=null)
__construct($streamName=null)
static getInstance()
Definition: Core.php:193
setStreamBuffer($buffer)
Definition: Stream.php:465
seek($offset, $whence=Stream\IStream\Pointable::SEEK_SET)