‪TYPO3CMS  ‪main
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 
52  public function ‪addListener(string $event, string $service, string $method = null, string ‪$identifier = null): void
53  {
54  $this->listeners[$event][‪$identifier ?? $service] = [
55  'service' => $service,
56  'method' => $method,
57  ];
58  }
59 
65  public function ‪getAllListenerDefinitions(): array
66  {
67  return ‪$this->listeners;
68  }
69 
70  public function ‪getListenersForEvent(object $event): iterable
71  {
72  $eventClasses = [get_class($event)];
73  $classParents = class_parents($event);
74  $classInterfaces = class_implements($event);
75  if (is_array($classParents) && !empty($classParents)) {
76  array_push($eventClasses, ...array_values($classParents));
77  }
78  if (is_array($classInterfaces) && !empty($classInterfaces)) {
79  array_push($eventClasses, ...array_values($classInterfaces));
80  }
81  foreach ($eventClasses as $className) {
82  if (isset($this->listeners[$className])) {
83  foreach ($this->listeners[$className] as $listener) {
84  yield $this->‪getCallable($listener['service'], $listener['method']);
85  }
86  }
87  }
88  }
89 
94  protected function ‪getCallable(string $service, string $method = null): callable
95  {
96  $target = $this->container->get($service);
97  if ($method !== null) {
98  // Dispatch to configured method name instead of __invoke()
99  $target = [ $target, $method ];
100  }
101 
102  if (!is_callable($target)) {
103  throw new \InvalidArgumentException(
104  sprintf('Event listener "%s%s%s" is not callable"', $service, ($method !== null ? '::' : ''), $method),
105  1549988537
106  );
107  }
108 
109  return $target;
110  }
111 }
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\getCallable
‪getCallable(string $service, string $method=null)
Definition: ListenerProvider.php:92
‪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\$listeners
‪array $listeners
Definition: ListenerProvider.php:37
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\addListener
‪addListener(string $event, string $service, string $method=null, string $identifier=null)
Definition: ListenerProvider.php:50
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\getAllListenerDefinitions
‪getAllListenerDefinitions()
Definition: ListenerProvider.php:63
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\getListenersForEvent
‪getListenersForEvent(object $event)
Definition: ListenerProvider.php:68
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\__construct
‪__construct(ContainerInterface $container)
Definition: ListenerProvider.php:39
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37