123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace Hoa\Event;
- use Hoa\Consistency;
- class Event
- {
-
- const KEY_EVENT = 0;
-
- const KEY_SOURCE = 1;
-
- private static $_register = [];
-
- protected $_callable = [];
-
- private function __construct()
- {
- return;
- }
-
- public static function getEvent($eventId)
- {
- if (!isset(self::$_register[$eventId][self::KEY_EVENT])) {
- self::$_register[$eventId] = [
- self::KEY_EVENT => new self(),
- self::KEY_SOURCE => null
- ];
- }
- return self::$_register[$eventId][self::KEY_EVENT];
- }
-
- public static function register($eventId, $source)
- {
- if (true === self::eventExists($eventId)) {
- throw new Exception(
- 'Cannot redeclare an event with the same ID, i.e. the event ' .
- 'ID %s already exists.',
- 0,
- $eventId
- );
- }
- if (is_object($source) && !($source instanceof Source)) {
- throw new Exception(
- 'The source must implement \Hoa\Event\Source ' .
- 'interface; given %s.',
- 1,
- get_class($source)
- );
- } else {
- $reflection = new \ReflectionClass($source);
- if (false === $reflection->implementsInterface('\Hoa\Event\Source')) {
- throw new Exception(
- 'The source must implement \Hoa\Event\Source ' .
- 'interface; given %s.',
- 2,
- $source
- );
- }
- }
- if (!isset(self::$_register[$eventId][self::KEY_EVENT])) {
- self::$_register[$eventId][self::KEY_EVENT] = new self();
- }
- self::$_register[$eventId][self::KEY_SOURCE] = $source;
- return;
- }
-
- public static function unregister($eventId, $hard = false)
- {
- if (false !== $hard) {
- unset(self::$_register[$eventId]);
- } else {
- self::$_register[$eventId][self::KEY_SOURCE] = null;
- }
- return;
- }
-
- public function attach($callable)
- {
- $callable = xcallable($callable);
- $this->_callable[$callable->getHash()] = $callable;
- return $this;
- }
-
- public function detach($callable)
- {
- unset($this->_callable[xcallable($callable)->getHash()]);
- return $this;
- }
-
- public function isListened()
- {
- return !empty($this->_callable);
- }
-
- public static function notify($eventId, Source $source, Bucket $data)
- {
- if (false === self::eventExists($eventId)) {
- throw new Exception(
- 'Event ID %s does not exist, cannot send notification.',
- 3,
- $eventId
- );
- }
- $data->setSource($source);
- $event = self::getEvent($eventId);
- foreach ($event->_callable as $callable) {
- $callable($data);
- }
- return;
- }
-
- public static function eventExists($eventId)
- {
- return
- array_key_exists($eventId, self::$_register) &&
- self::$_register[$eventId][self::KEY_SOURCE] !== null;
- }
- }
- Consistency::flexEntity('Hoa\Event\Event');
|