‪TYPO3CMS  10.4
ListenerProvider.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\EventDispatcher\ListenerProviderInterface;
22 
29 class ‪ListenerProvider implements ListenerProviderInterface
30 {
34  protected ‪$container;
35 
39  protected ‪$listeners = [];
40 
41  public function ‪__construct(ContainerInterface ‪$container)
42  {
43  $this->container = ‪$container;
44  }
45 
54  public function ‪addListener(string $event, string $service, string $method = null): void
55  {
56  $this->listeners[$event][] = [
57  'service' => $service,
58  'method' => $method
59  ];
60  }
61 
67  public function ‪getAllListenerDefinitions(): array
68  {
69  return ‪$this->listeners;
70  }
71 
75  public function ‪getListenersForEvent(object $event): iterable
76  {
77  $eventClasses = [get_class($event)];
78  $classParents = class_parents($event);
79  $classInterfaces = class_implements($event);
80  if (is_array($classParents) && !empty($classParents)) {
81  array_push($eventClasses, ...array_values($classParents));
82  }
83  if (is_array($classInterfaces) && !empty($classInterfaces)) {
84  array_push($eventClasses, ...array_values($classInterfaces));
85  }
86  foreach ($eventClasses as $className) {
87  if (isset($this->listeners[$className])) {
88  foreach ($this->listeners[$className] as $listener) {
89  yield $this->‪getCallable($listener['service'], $listener['method']);
90  }
91  }
92  }
93  }
94 
101  protected function ‪getCallable(string $service, string $method = null): callable
102  {
103  $target = $this->container->get($service);
104  if ($method !== null) {
105  // Dispatch to configured method name instead of __invoke()
106  $target = [ $target, $method ];
107  }
108 
109  if (!is_callable($target)) {
110  throw new \InvalidArgumentException(
111  sprintf('Event listener "%s%s%s" is not callable"', $service, ($method !== null ? '::' : ''), $method),
112  1549988537
113  );
114  }
115 
116  return $target;
117  }
118 }
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\$container
‪ContainerInterface $container
Definition: ListenerProvider.php:33
‪TYPO3\CMS\Core\EventDispatcher
Definition: EventDispatcher.php:18
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\addListener
‪addListener(string $event, string $service, string $method=null)
Definition: ListenerProvider.php:52
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\$listeners
‪array $listeners
Definition: ListenerProvider.php:37
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\getAllListenerDefinitions
‪getAllListenerDefinitions()
Definition: ListenerProvider.php:65
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\getCallable
‪callable getCallable(string $service, string $method=null)
Definition: ListenerProvider.php:99
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\getListenersForEvent
‪getListenersForEvent(object $event)
Definition: ListenerProvider.php:73
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\__construct
‪__construct(ContainerInterface $container)
Definition: ListenerProvider.php:39