Event.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Event;
  36. use Hoa\Consistency;
  37. /**
  38. * Class \Hoa\Event\Event.
  39. *
  40. * Events are asynchronous at registration, anonymous at use (until we
  41. * receive a bucket) and useful to largely spread data through components
  42. * without any known connection between them.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Event
  48. {
  49. /**
  50. * Event ID key.
  51. *
  52. * @const int
  53. */
  54. const KEY_EVENT = 0;
  55. /**
  56. * Source object key.
  57. *
  58. * @const int
  59. */
  60. const KEY_SOURCE = 1;
  61. /**
  62. * Static register of all observable objects, i.e. \Hoa\Event\Source
  63. * object, i.e. object that can send event.
  64. *
  65. * @var array
  66. */
  67. private static $_register = [];
  68. /**
  69. * Callables, i.e. observer objects.
  70. *
  71. * @var array
  72. */
  73. protected $_callable = [];
  74. /**
  75. * Privatize the constructor.
  76. *
  77. */
  78. private function __construct()
  79. {
  80. return;
  81. }
  82. /**
  83. * Manage multiton of events, with the principle of asynchronous
  84. * attachments.
  85. *
  86. * @param string $eventId Event ID.
  87. * @return \Hoa\Event\Event
  88. */
  89. public static function getEvent($eventId)
  90. {
  91. if (!isset(self::$_register[$eventId][self::KEY_EVENT])) {
  92. self::$_register[$eventId] = [
  93. self::KEY_EVENT => new self(),
  94. self::KEY_SOURCE => null
  95. ];
  96. }
  97. return self::$_register[$eventId][self::KEY_EVENT];
  98. }
  99. /**
  100. * Declare a new object in the observable collection.
  101. * Note: Hoa's libraries use hoa://Event/AnID for their observable objects;
  102. *
  103. * @param string $eventId Event ID.
  104. * @param \Hoa\Event\Source|string $source Observable object or class.
  105. * @return void
  106. * @throws \Hoa\Event\Exception
  107. */
  108. public static function register($eventId, $source)
  109. {
  110. if (true === self::eventExists($eventId)) {
  111. throw new Exception(
  112. 'Cannot redeclare an event with the same ID, i.e. the event ' .
  113. 'ID %s already exists.',
  114. 0,
  115. $eventId
  116. );
  117. }
  118. if (is_object($source) && !($source instanceof Source)) {
  119. throw new Exception(
  120. 'The source must implement \Hoa\Event\Source ' .
  121. 'interface; given %s.',
  122. 1,
  123. get_class($source)
  124. );
  125. } else {
  126. $reflection = new \ReflectionClass($source);
  127. if (false === $reflection->implementsInterface('\Hoa\Event\Source')) {
  128. throw new Exception(
  129. 'The source must implement \Hoa\Event\Source ' .
  130. 'interface; given %s.',
  131. 2,
  132. $source
  133. );
  134. }
  135. }
  136. if (!isset(self::$_register[$eventId][self::KEY_EVENT])) {
  137. self::$_register[$eventId][self::KEY_EVENT] = new self();
  138. }
  139. self::$_register[$eventId][self::KEY_SOURCE] = $source;
  140. return;
  141. }
  142. /**
  143. * Undeclare an object in the observable collection.
  144. *
  145. * @param string $eventId Event ID.
  146. * @param bool $hard If false, just delete the source, else,
  147. * delete source and attached callables.
  148. * @return void
  149. */
  150. public static function unregister($eventId, $hard = false)
  151. {
  152. if (false !== $hard) {
  153. unset(self::$_register[$eventId]);
  154. } else {
  155. self::$_register[$eventId][self::KEY_SOURCE] = null;
  156. }
  157. return;
  158. }
  159. /**
  160. * Attach an object to an event.
  161. * It can be a callable or an accepted callable form (please, see the
  162. * \Hoa\Consistency\Xcallable class).
  163. *
  164. * @param mixed $callable Callable.
  165. * @return \Hoa\Event\Event
  166. */
  167. public function attach($callable)
  168. {
  169. $callable = xcallable($callable);
  170. $this->_callable[$callable->getHash()] = $callable;
  171. return $this;
  172. }
  173. /**
  174. * Detach an object to an event.
  175. * Please see $this->attach() method.
  176. *
  177. * @param mixed $callable Callable.
  178. * @return \Hoa\Event\Event
  179. */
  180. public function detach($callable)
  181. {
  182. unset($this->_callable[xcallable($callable)->getHash()]);
  183. return $this;
  184. }
  185. /**
  186. * Check if at least one callable is attached to an event.
  187. *
  188. * @return bool
  189. */
  190. public function isListened()
  191. {
  192. return !empty($this->_callable);
  193. }
  194. /**
  195. * Notify, i.e. send data to observers.
  196. *
  197. * @param string $eventId Event ID.
  198. * @param \Hoa\Event\Source $source Source.
  199. * @param \Hoa\Event\Bucket $data Data.
  200. * @return void
  201. * @throws \Hoa\Event\Exception
  202. */
  203. public static function notify($eventId, Source $source, Bucket $data)
  204. {
  205. if (false === self::eventExists($eventId)) {
  206. throw new Exception(
  207. 'Event ID %s does not exist, cannot send notification.',
  208. 3,
  209. $eventId
  210. );
  211. }
  212. $data->setSource($source);
  213. $event = self::getEvent($eventId);
  214. foreach ($event->_callable as $callable) {
  215. $callable($data);
  216. }
  217. return;
  218. }
  219. /**
  220. * Check whether an event exists.
  221. *
  222. * @param string $eventId Event ID.
  223. * @return bool
  224. */
  225. public static function eventExists($eventId)
  226. {
  227. return
  228. array_key_exists($eventId, self::$_register) &&
  229. self::$_register[$eventId][self::KEY_SOURCE] !== null;
  230. }
  231. }
  232. /**
  233. * Flex entity.
  234. */
  235. Consistency::flexEntity('Hoa\Event\Event');