ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCtrlSubject.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
31  protected array $observer_groups = [];
32 
33  public function __construct()
34  {
35  $this->initObserverGroup((ilCtrlEvent::ALL)->value);
36  }
37 
38  public function attach(ilCtrlObserver $observer, ilCtrlEvent $event): void
39  {
40  $this->initObserverGroup($event->value);
41 
42  $this->observer_groups[$event->value][] = $observer;
43  }
44 
45  public function detach(ilCtrlObserver $observer, ilCtrlEvent $event = ilCtrlEvent::ALL): void
46  {
47  $this->initObserverGroup($event->value);
48 
49  foreach ($this->observer_groups[$event->value] as $index => $attached_observer) {
50  if ($attached_observer->getId() === $observer->getId()) {
51  unset($this->observer_groups[$event->value][$index]);
52  }
53  }
54  }
55 
56  public function notify(ilCtrlEvent $event, ?string $data): void
57  {
58  $this->initObserverGroup($event->value);
59 
60  $observers = array_merge(
61  $this->observer_groups[(ilCtrlEvent::ALL)->value],
62  $this->observer_groups[$event->value],
63  );
64 
65  foreach ($observers as $interessted_observer) {
66  $interessted_observer->update($event, $data);
67  }
68  }
69 
70  protected function initObserverGroup(string $group): void
71  {
72  if (!isset($this->observer_groups[$group])) {
73  $this->observer_groups[$group] = [];
74  }
75  }
76 }
initObserverGroup(string $group)
event string being used if
ilCtrlEvent
detach(ilCtrlObserver $observer, ilCtrlEvent $event=ilCtrlEvent::ALL)
attach(ilCtrlObserver $observer, ilCtrlEvent $event)
notify(ilCtrlEvent $event, ?string $data)
getId()
Unique identifier of the implementing event-listener.