ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAppEventHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
52{
53 protected array $listener = [];
54 protected ilLogger $logger;
58
59 public function __construct(?ilLogger $logger = null)
60 {
61 global $DIC;
62
63 $this->event_handling_data = new ilArtifactEventHandlingData();
64 $this->component_repository = $DIC["component.repository"];
65 $this->component_factory = $DIC["component.factory"];
66 $this->initListeners();
67
68 $this->logger = $logger ?? \ilLoggerFactory::getLogger('evnt');
69 }
70
71 protected function initListeners(): void
72 {
73 $listener_events = $this->event_handling_data->getEventsByType("listen");
74 foreach ($listener_events as $event_key => $event_value) {
75 $this->listener[$event_value["type_specification"]][] = $event_value["component"];
76 }
77 }
78
86 public function raise(
87 string $a_component,
88 string $a_event,
89 array $a_parameter = []
90 ): void {
91 $this->logger->debug(sprintf(
92 "Received event '%s' from component '%s'.",
93 $a_event,
94 $a_component
95 ));
96
97 $parameter_formatter = static function ($value) use (&$parameter_formatter) {
98 if (is_object($value)) {
99 return get_class($value);
100 }
101
102 if (is_array($value)) {
103 return array_map(
104 $parameter_formatter,
105 $value
106 );
107 }
108
109 return $value;
110 };
111
112 $this->logger->debug('Event data: ' . var_export(array_map(
113 $parameter_formatter,
114 $a_parameter
115 ), true));
116
117 $this->logger->debug("Started event propagation for event listeners ...");
118
119 if (is_array($this->listener[$a_component] ?? null)) {
120 foreach ($this->listener[$a_component] as $listener) {
121 // Allow listeners like Services/WebServices/ECS
122 $last_slash = strripos($listener, '/');
123 $comp = substr($listener, 0, $last_slash);
124 // any kind of plugins with events in their plugin.xml
125 if ($comp == 'Plugins') {
126 $name = substr($listener, $last_slash + 1);
127
128
129 foreach ($this->component_repository->getPlugins() as $pl) {
130 if ($pl->getName() !== $name || !$pl->isActive()) {
131 continue;
132 }
133 $plugin = $this->component_factory->getPlugin($pl->getId());
134 $plugin->handleEvent($a_component, $a_event, $a_parameter);
135 }
136 } else {
137 $class = 'il' . substr($listener, $last_slash + 1) . 'AppEventListener';
138
139 if (class_exists($class)) {
140 call_user_func(array($class, 'handleEvent'), $a_component, $a_event, $a_parameter);
141 }
142 }
143 }
144 }
145
146 $this->logger->debug("Finished event listener handling, started event propagation for event hook plugins ...");
147
148 // get all event hook plugins and forward the event to them
149 foreach ($this->component_factory->getActivePluginsInSlot("evhk") as $plugin) {
150 $plugin->handleEvent($a_component, $a_event, $a_parameter);
151 }
152 }
153}
Global event handler.
ilArtifactEventHandlingData $event_handling_data
ilComponentFactory $component_factory
ilComponentRepository $component_repository
__construct(?ilLogger $logger=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
Readable part of repository interface to ilComponentDataDB.
global $DIC
Definition: shib_login.php:26