Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

RegisterListenersPass.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 5.16 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\EventDispatcher\DependencyInjection;
013   
014  use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
015  use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
016  use Symfony\Component\DependencyInjection\ContainerBuilder;
017  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
018  use Symfony\Component\DependencyInjection\Reference;
019  use Symfony\Component\EventDispatcher\EventDispatcher;
020  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
021   
022  /**
023   * Compiler pass to register tagged services for an event dispatcher.
024   */
025  class RegisterListenersPass implements CompilerPassInterface
026  {
027      protected $dispatcherService;
028      protected $listenerTag;
029      protected $subscriberTag;
030   
031      private $hotPathEvents = [];
032      private $hotPathTagName;
033   
034      /**
035       * @param string $dispatcherService Service name of the event dispatcher in processed container
036       * @param string $listenerTag       Tag name used for listener
037       * @param string $subscriberTag     Tag name used for subscribers
038       */
039      public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
040      {
041          $this->dispatcherService = $dispatcherService;
042          $this->listenerTag = $listenerTag;
043          $this->subscriberTag = $subscriberTag;
044      }
045   
046      public function setHotPathEvents(array $hotPathEvents, $tagName = 'container.hot_path')
047      {
048          $this->hotPathEvents = array_flip($hotPathEvents);
049          $this->hotPathTagName = $tagName;
050   
051          return $this;
052      }
053   
054      public function process(ContainerBuilder $container)
055      {
056          if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
057              return;
058          }
059   
060          $definition = $container->findDefinition($this->dispatcherService);
061   
062          foreach ($container->findTaggedServiceIds($this->listenerTag, true) as $id => $events) {
063              foreach ($events as $event) {
064                  $priority = isset($event['priority']) ? $event['priority'] : 0;
065   
066                  if (!isset($event['event'])) {
067                      throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
068                  }
069   
070                  if (!isset($event['method'])) {
071                      $event['method'] = 'on'.preg_replace_callback([
072                          '/(?<=\b)[a-z]/i',
073                          '/[^a-z0-9]/i',
074                      ], function ($matches) { return strtoupper($matches[0]); }, $event['event']);
075                      $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
076                  }
077   
078                  $definition->addMethodCall('addListener', [$event['event'], [new ServiceClosureArgument(new Reference($id)), $event['method']], $priority]);
079   
080                  if (isset($this->hotPathEvents[$event['event']])) {
081                      $container->getDefinition($id)->addTag($this->hotPathTagName);
082                  }
083              }
084          }
085   
086          $extractingDispatcher = new ExtractingEventDispatcher();
087   
088          foreach ($container->findTaggedServiceIds($this->subscriberTag, true) as $id => $attributes) {
089              $def = $container->getDefinition($id);
090   
091              // We must assume that the class value has been correctly filled, even if the service is created by a factory
092              $class = $def->getClass();
093   
094              if (!$r = $container->getReflectionClass($class)) {
095                  throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
096              }
097              if (!$r->isSubclassOf(EventSubscriberInterface::class)) {
098                  throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class));
099              }
100              $class = $r->name;
101   
102              ExtractingEventDispatcher::$subscriber = $class;
103              $extractingDispatcher->addSubscriber($extractingDispatcher);
104              foreach ($extractingDispatcher->listeners as $args) {
105                  $args[1] = [new ServiceClosureArgument(new Reference($id)), $args[1]];
106                  $definition->addMethodCall('addListener', $args);
107   
108                  if (isset($this->hotPathEvents[$args[0]])) {
109                      $container->getDefinition($id)->addTag($this->hotPathTagName);
110                  }
111              }
112              $extractingDispatcher->listeners = [];
113          }
114      }
115  }
116   
117  /**
118   * @internal
119   */
120  class ExtractingEventDispatcher extends EventDispatcher implements EventSubscriberInterface
121  {
122      public $listeners = [];
123   
124      public static $subscriber;
125   
126      public function addListener($eventName, $listener, $priority = 0)
127      {
128          $this->listeners[] = [$eventName, $listener[1], $priority];
129      }
130   
131      public static function getSubscribedEvents()
132      {
133          $callback = [self::$subscriber, 'getSubscribedEvents'];
134   
135          return $callback();
136      }
137  }
138