ILIAS  release_8 Revision v8.24
class.ilAppEventHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
52{
53 protected ilDBInterface $db;
54 protected array $listener;
55 protected ilLogger $logger;
58
59 public function __construct()
60 {
61 global $DIC;
62
63 $this->db = $DIC->database();
64 $this->component_repository = $DIC["component.repository"];
65 $this->component_factory = $DIC["component.factory"];
66 $this->initListeners();
67
68 $this->logger = \ilLoggerFactory::getLogger('evnt');
69 }
70
71 protected function initListeners(): void
72 {
74 $cached_listeners = $ilGlobalCache->get('listeners');
75 if (is_array($cached_listeners)) {
76 $this->listener = $cached_listeners;
77
78 return;
79 }
80
82
83 $this->listener = array();
84
85 $sql = "SELECT * FROM il_event_handling" .
86 " WHERE type = " . $ilDB->quote("listen", "text");
87 $res = $ilDB->query($sql);
88 while ($row = $ilDB->fetchAssoc($res)) {
89 $this->listener[$row["id"]][] = $row["component"];
90 }
91
92 $ilGlobalCache->set('listeners', $this->listener);
93 }
94
102 public function raise(
103 string $a_component,
104 string $a_event,
105 array $a_parameter = []
106 ): void {
107 $this->logger->debug(sprintf(
108 "Received event '%s' from component '%s'.",
109 $a_event,
110 $a_component
111 ));
112
113 $parameter_formatter = static function ($value) use (&$parameter_formatter) {
114 if (is_object($value)) {
115 return get_class($value);
116 }
117
118 if (is_array($value)) {
119 return array_map(
120 $parameter_formatter,
121 $value
122 );
123 }
124
125 return $value;
126 };
127
128 $this->logger->debug('Event data: ' . var_export(array_map(
129 $parameter_formatter,
130 $a_parameter
131 ), true));
132
133 $this->logger->debug("Started event propagation for event listeners ...");
134
135 if (is_array($this->listener[$a_component] ?? null)) {
136 foreach ($this->listener[$a_component] as $listener) {
137 // Allow listeners like Services/WebServices/ECS
138 $last_slash = strripos($listener, '/');
139 $comp = substr($listener, 0, $last_slash);
140
141 // any kind of plugins with events in their plugin.xml
142 if ($comp == 'Plugins') {
143 $name = substr($listener, $last_slash + 1);
144
145
146 foreach ($this->component_repository->getPlugins() as $pl) {
147 if ($pl->getName() !== $name || !$pl->isActive()) {
148 continue;
149 }
150 $plugin = $this->component_factory->getPlugin($pl->getId());
151 $plugin->handleEvent($a_component, $a_event, $a_parameter);
152 }
153 } else {
154 $class = 'il' . substr($listener, $last_slash + 1) . 'AppEventListener';
155 $file = "./" . $listener . "/classes/class." . $class . ".php";
156 // if file exists, call listener
157 if (is_file($file)) {
158 include_once($file);
159 call_user_func(array($class, 'handleEvent'), $a_component, $a_event, $a_parameter);
160 }
161 }
162 }
163 }
164
165 $this->logger->debug("Finished event listener handling, started event propagation for event hook plugins ...");
166
167 // get all event hook plugins and forward the event to them
168 foreach ($this->component_factory->getActivePluginsInSlot("evhk") as $plugin) {
169 $plugin->handleEvent($a_component, $a_event, $a_parameter);
170 }
171 }
172}
Global event handler.
ilComponentFactory $component_factory
ilComponentRepository $component_repository
static getInstance(?string $component)
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
global $DIC
Definition: feed.php:28
Readable part of repository interface to ilComponentDataDB.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if($format !==null) $name
Definition: metadata.php:247