HookEvent.php 4.9 KB

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