‪TYPO3CMS  10.4
ListenerProviderPass.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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
25 
29 final class ‪ListenerProviderPass implements CompilerPassInterface
30 {
34  private ‪$tagName;
35 
39  public function ‪__construct(string ‪$tagName)
40  {
41  $this->tagName = ‪$tagName;
42  }
43 
47  public function ‪process(ContainerBuilder $container)
48  {
49  $listenerProviderDefinition = $container->findDefinition(ListenerProvider::class);
50  if (!$listenerProviderDefinition) {
51  return;
52  }
53 
54  $unorderedEventListeners = [];
55  foreach ($container->findTaggedServiceIds($this->tagName) as $serviceName => $tags) {
56  $container->findDefinition($serviceName)->setPublic(true);
57  foreach ($tags as $attributes) {
58  if (!isset($attributes['event'])) {
59  throw new \InvalidArgumentException(
60  'Service tag "event.listener" requires an event attribute to be defined, missing in: ' . $serviceName,
61  1563217364
62  );
63  }
64 
65  $eventIdentifier = $attributes['event'];
66  $listenerIdentifier = $attributes['identifier'] ?? $serviceName;
67  $unorderedEventListeners[$eventIdentifier][$listenerIdentifier] = [
68  'service' => $serviceName,
69  'method' => $attributes['method'] ?? null,
70  'before' => ‪GeneralUtility::trimExplode(',', $attributes['before'] ?? '', true),
71  'after' => ‪GeneralUtility::trimExplode(',', $attributes['after'] ?? '', true),
72  ];
73  }
74  }
75 
76  $dependencyOrderingService = new ‪DependencyOrderingService();
77  foreach ($unorderedEventListeners as $eventName => $listeners) {
78  // Sort them
79  $listeners = $dependencyOrderingService->orderByDependencies($listeners);
80  // Configure ListenerProvider factory to include these listeners
81  foreach ($listeners as $listener) {
82  $listenerProviderDefinition->addMethodCall('addListener', [
83  $eventName,
84  $listener['service'],
85  $listener['method'],
86  ]);
87  }
88  }
89  }
90 }
‪TYPO3\CMS\Core\DependencyInjection\ListenerProviderPass\$tagName
‪string $tagName
Definition: ListenerProviderPass.php:33
‪TYPO3\CMS\Core\DependencyInjection
Definition: AutowireInjectMethodsPass.php:18
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Core\DependencyInjection\ListenerProviderPass\process
‪process(ContainerBuilder $container)
Definition: ListenerProviderPass.php:46
‪TYPO3\CMS\Core\DependencyInjection\ListenerProviderPass
Definition: ListenerProviderPass.php:30
‪TYPO3\CMS\Core\DependencyInjection\ListenerProviderPass\__construct
‪__construct(string $tagName)
Definition: ListenerProviderPass.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30