Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Private Member Functions | Static Private Attributes | List of all members
Hoa\Stream\Stream Class Reference
Inheritance diagram for Hoa\Stream\Stream:

Public Member Functions

 __construct ($streamName, $context=null, $wait=false)
 
 open ()
 
 close ()
 
 getStreamName ()
 
 getStreamContext ()
 
 _setStream ($stream)
 
 isOpened ()
 
 setStreamTimeout ($seconds, $microseconds=0)
 
 setStreamBlocking ($mode)
 
 setStreamBuffer ($buffer)
 
 disableStreamBuffer ()
 
 getStreamBufferSize ()
 
 getStreamWrapperName ()
 
 getStreamMetaData ()
 
 isBorrowing ()
 
 on ($listenerId, $callable)
 
 _notify ($ncode, $severity, $message, $code, $transferred, $max)
 
 __toString ()
 
 __destruct ()
 

Static Public Member Functions

static getStreamHandler ($streamName)
 
static _Hoa_Stream ()
 

Public Attributes

const NAME = 0
 
const HANDLER = 1
 
const RESOURCE = 2
 
const CONTEXT = 3
 

Protected Member Functions

_open ($streamName, Context $context=null)
 
 _close ()
 
 getStream ()
 

Protected Attributes

 $_bucket = []
 
 $_bufferSize = 8192
 
 $_streamName = null
 
 $_context = null
 
 $_hasBeenDiffered = false
 
 $_on = null
 
 $_borrowed = false
 

Static Private Member Functions

static & _getStream ($streamName, Stream $handler, $context=null)
 

Static Private Attributes

static $_register = []
 

Detailed Description

Class .

Static register for all streams (files, sockets etc.).

Definition at line 49 of file Stream.php.

Constructor & Destructor Documentation

Hoa\Stream\Stream::__construct (   $streamName,
  $context = null,
  $wait = false 
)

Set the current stream. If not exists in the register, try to call the $this->_open() method. Please, see the self::_getStream() method.

Parameters
string$streamNameStream name (e.g. path or URL).
string$contextContext ID (please, see the class).
bool$waitDiffer opening or not.
Returns
void

Definition at line 148 of file Stream.php.

149  {
150  $this->_streamName = $streamName;
151  $this->_context = $context;
152  $this->_hasBeenDiffered = $wait;
153  $this->_on = new Core\Event\Listener($this, [
154  'authrequire',
155  'authresult',
156  'complete',
157  'connect',
158  'failure',
159  'mimetype',
160  'progress',
161  'redirect',
162  'resolve',
163  'size'
164  ]);
165 
166  if (true === $wait) {
167  return;
168  }
169 
170  $this->open();
171 
172  return;
173  }

Here is the call graph for this function:

Hoa\Stream\Stream::__destruct ( )

Close the stream when destructing.

Returns
void

Definition at line 627 of file Stream.php.

628  {
629  $this->close();
630 
631  return;
632  }

Here is the call graph for this function:

Member Function Documentation

Hoa\Stream\Stream::__toString ( )

Transform object to string.

Returns
string

Definition at line 617 of file Stream.php.

618  {
619  return $this->getStreamName();
620  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Stream\Stream::_close ( )
abstractprotected

Close the current stream. Note: this method is protected, but do not forget that it could be overloaded into a public context.

Returns
bool

Here is the caller graph for this function:

static& Hoa\Stream\Stream::_getStream (   $streamName,
Stream  $handler,
  $context = null 
)
staticfinalprivate

Get a stream in the register. If the stream does not exist, try to open it by calling the $handler->_open() method.

Parameters
string$streamNameStream name.
\Hoa\Stream$handlerStream handler.
string$contextContext ID (please, see the class).
Returns
array
Exceptions

Definition at line 187 of file Stream.php.

191  {
192  $name = md5($streamName);
193 
194  if (null !== $context) {
195  if (false === Context::contextExists($context)) {
196  throw new Exception(
197  'Context %s was not previously declared, cannot retrieve ' .
198  'this context.',
199  0,
200  $context
201  );
202  }
203 
204  $context = Context::getInstance($context);
205  }
206 
207  if (!isset(self::$_register[$name])) {
208  self::$_register[$name] = [
209  self::NAME => $streamName,
210  self::HANDLER => $handler,
211  self::RESOURCE => $handler->_open($streamName, $context),
212  self::CONTEXT => $context
213  ];
214  Core\Event::register(
215  'hoa://Event/Stream/' . $streamName,
216  $handler
217  );
218  // Add :open-ready?
219  Core\Event::register(
220  'hoa://Event/Stream/' . $streamName . ':close-before',
221  $handler
222  );
223  } else {
224  $handler->_borrowed = true;
225  }
226 
227  if (null === self::$_register[$name][self::RESOURCE]) {
228  self::$_register[$name][self::RESOURCE]
229  = $handler->_open($streamName, $context);
230  }
231 
232  return self::$_register[$name];
233  }
static contextExists($id)
Definition: Context.php:114
static getInstance($id)
Definition: Context.php:85

Here is the call graph for this function:

static Hoa\Stream\Stream::_Hoa_Stream ( )
staticfinal

Call the $handler->close() method on each stream in the static stream register. This method does not check the return value of $handler->close(). Thus, if a stream is persistent, the $handler->close() should do anything. It is a very generic method.

Returns
void

Definition at line 603 of file Stream.php.

604  {
605  foreach (self::$_register as $entry) {
606  $entry[self::HANDLER]->close();
607  }
608 
609  return;
610  }
Hoa\Stream\Stream::_notify (   $ncode,
  $severity,
  $message,
  $code,
  $transferred,
  $max 
)

Notification callback.

Parameters
int$ncodeNotification code. Please, see STREAM_NOTIFY_* constants.
int$severitySeverity. Please, see STREAM_NOTIFY_SEVERITY_* constants.
string$messageMessage.
int$codeMessage code.
int$transferredIf applicable, the number of transferred bytes.
int$maxIf applicable, the number of max bytes.
Returns
void

Definition at line 561 of file Stream.php.

568  {
569  static $_map = [
570  STREAM_NOTIFY_AUTH_REQUIRED => 'authrequire',
571  STREAM_NOTIFY_AUTH_RESULT => 'authresult',
572  STREAM_NOTIFY_COMPLETED => 'complete',
573  STREAM_NOTIFY_CONNECT => 'connect',
574  STREAM_NOTIFY_FAILURE => 'failure',
575  STREAM_NOTIFY_MIME_TYPE_IS => 'mimetype',
576  STREAM_NOTIFY_PROGRESS => 'progress',
577  STREAM_NOTIFY_REDIRECTED => 'redirect',
578  STREAM_NOTIFY_RESOLVE => 'resolve',
579  STREAM_NOTIFY_FILE_SIZE_IS => 'size'
580  ];
581 
582  $this->_on->fire($_map[$ncode], new Core\Event\Bucket([
583  'code' => $code,
584  'severity' => $severity,
585  'message' => $message,
586  'code' => $code,
587  'transferred' => $transferred,
588  'max' => $max
589  ]));
590 
591  return;
592  }
& Hoa\Stream\Stream::_open (   $streamName,
Context  $context = null 
)
abstractprotected

Open the stream and return the associated resource. Note: this method is protected, but do not forget that it could be overloaded into a public context.

Parameters
string$streamNameStream name (e.g. path or URL).
\Hoa\Stream\Context$contextContext.
Returns
resource
Exceptions

Here is the caller graph for this function:

Hoa\Stream\Stream::_setStream (   $stream)

Set the current stream. Useful to manage a stack of streams (e.g. socket and select). Notice that it could be unsafe to use this method without taking time to think about it two minutes. Resource of type “Unknown” is considered as valid.

Returns
resource
Exceptions

Definition at line 399 of file Stream.php.

400  {
401  if (false === is_resource($stream) &&
402  ('resource' === gettype($stream) &&
403  'Unknown' !== get_resource_type($stream))) {
404  throw new Exception(
405  'Try to change the stream resource with an invalid one; ' .
406  'given %s.',
407  1,
408  gettype($stream)
409  );
410  }
411 
412  $old = $this->_bucket[self::RESOURCE];
413  $this->_bucket[self::RESOURCE] = $stream;
414 
415  return $old;
416  }

Here is the caller graph for this function:

Hoa\Stream\Stream::close ( )
final

Close the current stream.

Returns
void

Definition at line 299 of file Stream.php.

300  {
301  $streamName = $this->getStreamName();
302  $name = md5($streamName);
303 
304  if (!isset(self::$_register[$name])) {
305  return;
306  }
307 
308  Core\Event::notify(
309  'hoa://Event/Stream/' . $streamName . ':close-before',
310  $this,
311  new Core\Event\Bucket()
312  );
313 
314  if (false === $this->_close()) {
315  return;
316  }
317 
318  unset(self::$_register[$name]);
319  $this->_bucket[self::HANDLER] = null;
320  unset($this->_on);
321  Core\Event::unregister(
322  'hoa://Event/Stream/' . $streamName
323  );
324  Core\Event::unregister(
325  'hoa://Event/Stream/' . $streamName . ':close-before'
326  );
327 
328  return;
329  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Stream\Stream::disableStreamBuffer ( )

Disable stream buffering. Alias of $this->setBuffer(0).

Returns
bool

Definition at line 483 of file Stream.php.

484  {
485  return $this->setStreamBuffer(0);
486  }
setStreamBuffer($buffer)
Definition: Stream.php:465

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Stream\Stream::getStream ( )
protected

Get the current stream.

Returns
resource

Definition at line 350 of file Stream.php.

351  {
352  if (empty($this->_bucket)) {
353  return null;
354  }
355 
356  return $this->_bucket[self::RESOURCE];
357  }
Hoa\Stream\Stream::getStreamBufferSize ( )

Get stream buffer size.

Returns
int

Definition at line 493 of file Stream.php.

494  {
495  return $this->_bufferSize;
496  }

Here is the caller graph for this function:

Hoa\Stream\Stream::getStreamContext ( )

Get the current stream context.

Returns

Definition at line 364 of file Stream.php.

365  {
366  if (empty($this->_bucket)) {
367  return null;
368  }
369 
370  return $this->_bucket[self::CONTEXT];
371  }

Here is the caller graph for this function:

static Hoa\Stream\Stream::getStreamHandler (   $streamName)
static

Get stream handler according to its name.

Parameters
string$streamNameStream name.
Returns

Definition at line 379 of file Stream.php.

380  {
381  $name = md5($streamName);
382 
383  if (!isset(self::$_register[$name])) {
384  return null;
385  }
386 
387  return self::$_register[$name][self::HANDLER];
388  }

Here is the caller graph for this function:

Hoa\Stream\Stream::getStreamMetaData ( )

Get stream meta data.

Returns
array

Definition at line 517 of file Stream.php.

518  {
519  return stream_get_meta_data($this->getStream());
520  }

Here is the call graph for this function:

Hoa\Stream\Stream::getStreamName ( )

Get the current stream name.

Returns
string

Definition at line 336 of file Stream.php.

337  {
338  if (empty($this->_bucket)) {
339  return null;
340  }
341 
342  return $this->_bucket[self::NAME];
343  }

Here is the caller graph for this function:

Hoa\Stream\Stream::getStreamWrapperName ( )

Get stream wrapper name.

Returns
string

Definition at line 503 of file Stream.php.

504  {
505  if (false === $pos = strpos($this->getStreamName(), '://')) {
506  return 'file';
507  }
508 
509  return substr($this->getStreamName(), 0, $pos);
510  }

Here is the call graph for this function:

Hoa\Stream\Stream::isBorrowing ( )

Whether this stream is already opened by another handler.

Returns
bool

Definition at line 527 of file Stream.php.

528  {
529  return $this->_borrowed;
530  }
Hoa\Stream\Stream::isOpened ( )

Check if the stream is opened.

Returns
bool

Definition at line 423 of file Stream.php.

424  {
425  return is_resource($this->getStream());
426  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Stream\Stream::on (   $listenerId,
  $callable 
)

Attach a callable to this listenable object.

Parameters
string$listenerIdListener ID.
mixed$callableCallable.
Returns

Implements Hoa\Core\Event\Listenable.

Definition at line 540 of file Stream.php.

541  {
542  $this->_on->attach($listenerId, $callable);
543 
544  return $this;
545  }
Hoa\Stream\Stream::open ( )
final

Open the stream.

Returns
Exceptions

Definition at line 262 of file Stream.php.

263  {
264  $context = $this->_context;
265 
266  if (true === $this->_hasBeenDiffered) {
267  if (null === $context) {
268  $handle = Context::getInstance(uniqid());
269  $handle->setParameters([
270  'notification' => [$this, '_notify']
271  ]);
272  $context = $handle->getId();
273  } elseif (true === Context::contextExists($context)) {
274  $handle = Context::getInstance($context);
275  $parameters = $handle->getParameters();
276 
277  if (!isset($parameters['notification'])) {
278  $handle->setParameters([
279  'notification' => [$this, '_notify']
280  ]);
281  }
282  }
283  }
284 
285  $this->_bucket = self::_getStream(
286  $this->_streamName,
287  $this,
288  $context
289  );
290 
291  return $this;
292  }
static contextExists($id)
Definition: Context.php:114
static getInstance($id)
Definition: Context.php:85

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Stream\Stream::setStreamBlocking (   $mode)

Set blocking/non-blocking mode.

Parameters
bool$modeBlocking mode.
Returns
bool

Definition at line 446 of file Stream.php.

447  {
448  return stream_set_blocking($this->getStream(), (int) $mode);
449  }

Here is the call graph for this function:

Hoa\Stream\Stream::setStreamBuffer (   $buffer)

Set stream buffer. Output using fwrite() (or similar function) is normally buffered at 8 Ko. This means that if there are two processes wanting to write to the same output stream, each is paused after 8 Ko of data to allow the other to write.

Parameters
int$bufferNumber of bytes to buffer. If zero, write operations are unbuffered. This ensures that all writes are completed before other processes are allowed to write to that output stream.
Returns
bool

Definition at line 465 of file Stream.php.

466  {
467  // Zero means success.
468  $out = 0 === stream_set_write_buffer($this->getStream(), $buffer);
469 
470  if (true === $out) {
471  $this->_bufferSize = $buffer;
472  }
473 
474  return $out;
475  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Stream\Stream::setStreamTimeout (   $seconds,
  $microseconds = 0 
)

Set the timeout period.

Parameters
int$secondTimeout period in seconds.
int$microTimeout period in microseconds.
Returns
bool

Definition at line 435 of file Stream.php.

436  {
437  return stream_set_timeout($this->getStream(), $seconds, $microseconds);
438  }

Here is the call graph for this function:

Member Data Documentation

Hoa\Stream\Stream::$_borrowed = false
protected

Definition at line 133 of file Stream.php.

Hoa\Stream\Stream::$_bucket = []
protected

Definition at line 84 of file Stream.php.

Hoa\Stream\Stream::$_bufferSize = 8192
protected

Definition at line 98 of file Stream.php.

Hoa\Stream\Stream::$_context = null
protected

Definition at line 112 of file Stream.php.

Hoa\Stream\Stream::$_hasBeenDiffered = false
protected

Definition at line 119 of file Stream.php.

Hoa\Stream\Stream::$_on = null
protected

Definition at line 126 of file Stream.php.

Hoa\Stream\Stream::$_register = []
staticprivate

Definition at line 91 of file Stream.php.

Hoa\Stream\Stream::$_streamName = null
protected

Definition at line 105 of file Stream.php.

const Hoa\Stream\Stream::CONTEXT = 3

Context index in the stream bucket.

int

Definition at line 77 of file Stream.php.

const Hoa\Stream\Stream::HANDLER = 1

Handler index in the stream bucket.

int

Definition at line 63 of file Stream.php.

const Hoa\Stream\Stream::NAME = 0

Name index in the stream bucket.

int

Definition at line 56 of file Stream.php.

const Hoa\Stream\Stream::RESOURCE = 2

Resource index in the stream bucket.

int

Definition at line 70 of file Stream.php.


The documentation for this class was generated from the following file: