HookEvent.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains an abstract Hook event class
  5. * Used for Hook Events (e.g Create user, Webservice registration).
  6. *
  7. * @package chamilo.library.hook
  8. */
  9. /**
  10. * Class HookEvent
  11. * This abstract class implements Hook Event Interface to build the base
  12. * for Hook Events. This class have some public static method,
  13. * e.g for create Hook Events.
  14. */
  15. abstract class HookEvent implements HookEventInterface
  16. {
  17. public $observers;
  18. public $eventName;
  19. public $eventData;
  20. public $manager;
  21. public static $hook;
  22. /**
  23. * Construct Method.
  24. *
  25. * @param string $eventName
  26. *
  27. * @throws Exception
  28. */
  29. protected function __construct($eventName)
  30. {
  31. $this->observers = new SplObjectStorage();
  32. $this->eventName = $eventName;
  33. $this->eventData = [];
  34. $this->manager = HookManagement::create();
  35. $this->loadAttachments();
  36. }
  37. /**
  38. * Return the singleton instance of Hook event.
  39. *
  40. * @return static
  41. */
  42. public static function create()
  43. {
  44. static $result = null;
  45. if ($result) {
  46. return $result;
  47. } else {
  48. try {
  49. $class = get_called_class();
  50. return new $class();
  51. } catch (Exception $e) {
  52. return null;
  53. }
  54. }
  55. }
  56. /**
  57. * Attach an HookObserver.
  58. *
  59. * @see http://php.net/manual/en/splsubject.attach.php
  60. *
  61. * @param \HookObserverInterface| $observer <p>
  62. * The <b>HookObserver</b> to attach.
  63. * </p>
  64. */
  65. public function attach(HookObserverInterface $observer)
  66. {
  67. $observerClass = get_class($observer);
  68. self::$hook[$this->eventName][$observerClass] = [
  69. 'class_name' => $observerClass,
  70. 'path' => $observer->getPath(),
  71. 'plugin_name' => $observer->getPluginName(),
  72. ];
  73. $this->observers->attach($observer);
  74. $this->manager->insertHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
  75. }
  76. /**
  77. * Detach an HookObserver.
  78. *
  79. * @see http://php.net/manual/en/splsubject.detach.php
  80. *
  81. * @param \HookObserverInterface| $observer <p>
  82. * The <b>HookObserver</b> to detach.
  83. * </p>
  84. */
  85. public function detach(HookObserverInterface $observer)
  86. {
  87. $observerClass = get_class($observer);
  88. unset(self::$hook[$this->eventName][$observerClass]);
  89. $this->observers->detach($observer);
  90. $this->manager->deleteHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
  91. }
  92. /**
  93. * Notify an observer.
  94. *
  95. * @see http://php.net/manual/en/splsubject.notify.php
  96. */
  97. public function notify()
  98. {
  99. foreach ($this->observers as $observer) {
  100. $observer->update($this);
  101. }
  102. }
  103. /**
  104. * Return the event name refer to where hook is used.
  105. *
  106. * @return string
  107. */
  108. public function getEventName()
  109. {
  110. return $this->eventName;
  111. }
  112. /**
  113. * Return an array containing all data needed by the hook observer to update.
  114. *
  115. * @return array
  116. */
  117. public function getEventData()
  118. {
  119. return $this->eventData;
  120. }
  121. /**
  122. * Set an array with data needed by hooks.
  123. *
  124. * @param array $data
  125. *
  126. * @return $this
  127. */
  128. public function setEventData(array $data)
  129. {
  130. foreach ($data as $key => $value) {
  131. // Assign value for each array item
  132. $this->eventData[$key] = $value;
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Detach all hook observers.
  138. *
  139. * @return $this
  140. */
  141. public function detachAll()
  142. {
  143. self::$hook[$this->eventName] = null;
  144. $this->observers->removeAll($this->observers);
  145. }
  146. /**
  147. * Clear all hookObservers without detach them.
  148. *
  149. * @return mixed
  150. */
  151. public function clearAttachments()
  152. {
  153. $this->observers->removeAll($this->observers);
  154. }
  155. /**
  156. * Load all hook observer already registered from Session or Database.
  157. *
  158. * @return $this
  159. */
  160. public function loadAttachments()
  161. {
  162. if (isset(self::$hook[$this->eventName]) && is_array(self::$hook[$this->eventName])) {
  163. foreach (self::$hook[$this->eventName] as $hookObserver => $val) {
  164. $hookObserverInstance = $hookObserver::create();
  165. $this->observers->attach($hookObserverInstance);
  166. }
  167. } else {
  168. // Load from Database and save into global name
  169. self::$hook[$this->eventName] = $this->manager->listHookObservers($this->eventName);
  170. if (isset(self::$hook[$this->eventName]) && is_array(self::$hook[$this->eventName])) {
  171. foreach (self::$hook[$this->eventName] as $hookObserver => $val) {
  172. $hookObserverInstance = $hookObserver::create();
  173. $this->observers->attach($hookObserverInstance);
  174. }
  175. }
  176. }
  177. return $this;
  178. }
  179. }