ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ObserverHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
26{
27 protected array $observers = [];
28
29 public function addObserver(object $class, string $method, string $element): void
30 {
31 $this->observers[$element][] = [
32 'class' => $class,
33 'method' => $method
34 ];
35 }
36
37 public function callObservers(string $element): void
38 {
39 foreach ($this->observers[$element] ?? [] as $observer) {
40 $class = $observer['class'];
41 $method = $observer['method'];
42
43 $class->$method($element);
44 }
45 }
46
48 {
49 $categories = [
50 'general' => 'General',
51 'lifecycle' => 'Lifecycle',
52 'metametadata' => 'MetaMetaData',
53 'technical' => 'Technical',
54 'educational' => 'Educational',
55 'rights' => 'Rights',
56 'relation' => 'Relation',
57 'annotation' => 'Annotation',
58 'classification' => 'Classification'
59 ];
60 $category = strtolower($path->steps()->current()?->name() ?? '');
61 if ($path->isRelative() || !isset($category) || !isset($categories[$category])) {
62 throw new \ilMDEditorException(
63 'Cannot call observers via relative or invalid path.'
64 );
65 }
66 $this->callObservers($categories[$category]);
67 }
68}
addObserver(object $class, string $method, string $element)
$path
Definition: ltiservices.php:30