123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- abstract class HookEvent implements HookEventInterface
- {
- public $observers;
- public $eventName;
- public $eventData;
- public $manager;
- public static $hook;
-
- protected function __construct($eventName)
- {
- $this->observers = new SplObjectStorage();
- $this->eventName = $eventName;
- $this->eventData = array();
- $this->manager = HookManagement::create();
- $this->loadAttachments();
- }
-
- public static function create()
- {
- static $result = null;
- if ($result) {
- return $result;
- } else {
- try {
- $class = get_called_class();
- return new $class;
- } catch (Exception $e) {
- return null;
- }
- }
- }
-
- public function attach(HookObserverInterface $observer)
- {
- $observerClass = get_class($observer);
- self::$hook[$this->eventName][$observerClass] = array(
- 'class_name' => $observerClass,
- 'path' => $observer->getPath(),
- 'plugin_name' => $observer->getPluginName(),
- );
- $this->observers->attach($observer);
- $this->manager->insertHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
- }
-
- public function detach(HookObserverInterface $observer)
- {
- $observerClass = get_class($observer);
- unset(self::$hook[$this->eventName][$observerClass]);
- $this->observers->detach($observer);
- $this->manager->deleteHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
- }
-
- public function notify()
- {
- foreach ($this->observers as $observer) {
- $observer->update($this);
- }
- }
-
- public function getEventName()
- {
- return $this->eventName;
- }
-
- public function getEventData()
- {
- return $this->eventData;
- }
-
- public function setEventData(array $data)
- {
- foreach ($data as $key => $value) {
-
- $this->eventData[$key] = $value;
- }
- return $this;
- }
-
- public function detachAll()
- {
- self::$hook[$this->eventName] = null;
- $this->observers->removeAll($this->observers);
- }
-
- public function clearAttachments()
- {
- $this->observers->removeAll($this->observers);
- }
-
- public function loadAttachments()
- {
- if (isset(self::$hook[$this->eventName]) && is_array(self::$hook[$this->eventName])) {
- foreach (self::$hook[$this->eventName] as $hookObserver => $val) {
- $hookObserverInstance = $hookObserver::create();
- $this->observers->attach($hookObserverInstance);
- }
- } else {
-
- self::$hook[$this->eventName] = $this->manager->listHookObservers($this->eventName);
- if (isset(self::$hook[$this->eventName]) && is_array(self::$hook[$this->eventName])) {
- foreach (self::$hook[$this->eventName] as $hookObserver => $val) {
- $hookObserverInstance = $hookObserver::create();
- $this->observers->attach($hookObserverInstance);
- }
- }
- }
- return $this;
- }
- }
|