Hoa central
Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | Static Private Attributes | List of all members
Hoa\Core\Event\Event Class Reference

Public Member Functions

 attach ($callable)
 
 detach ($callable)
 
 isListened ()
 

Static Public Member Functions

static getEvent ($eventId)
 
static register ($eventId, $source)
 
static unregister ($eventId, $hard=false)
 
static notify ($eventId, Source $source, Bucket $data)
 
static eventExists ($eventId)
 

Protected Attributes

 $_callable = []
 

Private Member Functions

 __construct ()
 

Static Private Attributes

static $_register = []
 

Detailed Description

Class .

Events are asynchronous at registration, anonymous at use (until we receive a bucket) and useful to largely spread data through components without any known connection between them.

Definition at line 164 of file Event.php.

Constructor & Destructor Documentation

Hoa\Core\Event\Event::__construct ( )
private

Privatize the constructor.

Returns
void

Definition at line 188 of file Event.php.

189  {
190  return;
191  }

Member Function Documentation

Hoa\Core\Event\Event::attach (   $callable)

Attach an object to an event. It can be a callable or an accepted callable form (please, see the class).

Parameters
mixed$callableCallable.
Returns

Definition at line 288 of file Event.php.

289  {
290  $callable = xcallable($callable);
291  $this->_callable[$callable->getHash()] = $callable;
292 
293  return $this;
294  }
Hoa\Core\Event\Event::detach (   $callable)

Detach an object to an event. Please see $this->attach() method.

Parameters
mixed$callableCallable.
Returns

Definition at line 303 of file Event.php.

304  {
305  unset($this->_callable[xcallable($callable)->getHash()]);
306 
307  return $this;
308  }
static Hoa\Core\Event\Event::eventExists (   $eventId)
static

Check whether an event exists.

Parameters
string$eventIdEvent ID.
Returns
bool

Definition at line 355 of file Event.php.

356  {
357  return
358  array_key_exists($eventId, self::$_register) &&
359  self::$_register[$eventId][1] !== null;
360  }
static Hoa\Core\Event\Event::getEvent (   $eventId)
static

Manage multiton of events, with the principle of asynchronous attachements.

Parameters
string$eventIdEvent ID.
Returns

Definition at line 200 of file Event.php.

201  {
202  if (!isset(self::$_register[$eventId][0])) {
203  self::$_register[$eventId] = [
204  0 => new self(),
205  1 => null
206  ];
207  }
208 
209  return self::$_register[$eventId][0];
210  }
Hoa\Core\Event\Event::isListened ( )

Check if at least one callable is attached to an event.

Returns
bool

Definition at line 315 of file Event.php.

316  {
317  return !empty($this->_callable);
318  }
static Hoa\Core\Event\Event::notify (   $eventId,
Source  $source,
Bucket  $data 
)
static

Notify, i.e. send data to observers.

Parameters
stringEvent ID.
\Hoa\Core\Event\Source$sourceSource.
\Hoa\Core\Event\Bucket$dataData.
Returns
void
Exceptions

Definition at line 329 of file Event.php.

330  {
331  if (false === self::eventExists($eventId)) {
332  throw new Core\Exception(
333  'Event ID %s does not exist, cannot send notification.',
334  3,
335  $eventId
336  );
337  }
338 
339  $data->setSource($source);
340  $event = self::getEvent($eventId);
341 
342  foreach ($event->_callable as $callable) {
343  $callable($data);
344  }
345 
346  return;
347  }

Here is the call graph for this function:

Here is the caller graph for this function:

static Hoa\Core\Event\Event::register (   $eventId,
  $source 
)
static

Declare a new object in the observable collection. Note: Hoa's libraries use hoa://Event/AnID for their observable objects;

Parameters
string$eventIdEvent ID.
\Hoa\Core\Event\Source$sourceObservable object.
Returns
void
Exceptions

Definition at line 221 of file Event.php.

222  {
223  if (true === self::eventExists($eventId)) {
224  throw new Core\Exception(
225  'Cannot redeclare an event with the same ID, i.e. the event ' .
226  'ID %s already exists.',
227  0,
228  $eventId
229  );
230  }
231 
232  if (is_object($source) && !($source instanceof Source)) {
233  throw new Core\Exception(
234  'The source must implement \Hoa\Core\Event\Source ' .
235  'interface; given %s.',
236  1,
237  get_class($source)
238  );
239  } else {
240  $reflection = new \ReflectionClass($source);
241 
242  if (false === $reflection->implementsInterface('\Hoa\Core\Event\Source')) {
243  throw new Core\Exception(
244  'The source must implement \Hoa\Core\Event\Source ' .
245  'interface; given %s.',
246  2,
247  $source
248  );
249  }
250  }
251 
252  if (!isset(self::$_register[$eventId][0])) {
253  self::$_register[$eventId][0] = new self();
254  }
255 
256  self::$_register[$eventId][1] = $source;
257 
258  return;
259  }
static Hoa\Core\Event\Event::unregister (   $eventId,
  $hard = false 
)
static

Undeclare an object in the observable collection.

Parameters
string$eventIdEvent ID.
bool$hardIf false, just delete the source, else, delete source and attached callables.
Returns
void

Definition at line 269 of file Event.php.

270  {
271  if (false !== $hard) {
272  unset(self::$_register[$eventId]);
273  } else {
274  self::$_register[$eventId][1] = null;
275  }
276 
277  return;
278  }

Member Data Documentation

Hoa\Core\Event\Event::$_callable = []
protected

Definition at line 179 of file Event.php.

Hoa\Core\Event\Event::$_register = []
staticprivate

Definition at line 172 of file Event.php.


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