[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/event-dispatcher/DependencyInjection/ -> RegisterListenersPass.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\EventDispatcher\DependencyInjection;
  13  
  14  use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  15  use Symfony\Component\DependencyInjection\ContainerBuilder;
  16  
  17  /**
  18   * Compiler pass to register tagged services for an event dispatcher.
  19   */
  20  class RegisterListenersPass implements CompilerPassInterface
  21  {
  22      protected $dispatcherService;
  23      protected $listenerTag;
  24      protected $subscriberTag;
  25  
  26      /**
  27       * @param string $dispatcherService Service name of the event dispatcher in processed container
  28       * @param string $listenerTag       Tag name used for listener
  29       * @param string $subscriberTag     Tag name used for subscribers
  30       */
  31      public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
  32      {
  33          $this->dispatcherService = $dispatcherService;
  34          $this->listenerTag = $listenerTag;
  35          $this->subscriberTag = $subscriberTag;
  36      }
  37  
  38      public function process(ContainerBuilder $container)
  39      {
  40          if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
  41              return;
  42          }
  43  
  44          $definition = $container->findDefinition($this->dispatcherService);
  45  
  46          foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
  47              $def = $container->getDefinition($id);
  48              if (!$def->isPublic()) {
  49                  throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
  50              }
  51  
  52              if ($def->isAbstract()) {
  53                  throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
  54              }
  55  
  56              foreach ($events as $event) {
  57                  $priority = isset($event['priority']) ? $event['priority'] : 0;
  58  
  59                  if (!isset($event['event'])) {
  60                      throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
  61                  }
  62  
  63                  if (!isset($event['method'])) {
  64                      $event['method'] = 'on'.preg_replace_callback(array(
  65                          '/(?<=\b)[a-z]/i',
  66                          '/[^a-z0-9]/i',
  67                      ), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
  68                      $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
  69                  }
  70  
  71                  $definition->addMethodCall('addListenerService', array($event['event'], array($id, $event['method']), $priority));
  72              }
  73          }
  74  
  75          foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
  76              $def = $container->getDefinition($id);
  77              if (!$def->isPublic()) {
  78                  throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
  79              }
  80  
  81              if ($def->isAbstract()) {
  82                  throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
  83              }
  84  
  85              // We must assume that the class value has been correctly filled, even if the service is created by a factory
  86              $class = $container->getParameterBag()->resolveValue($def->getClass());
  87              $interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
  88  
  89              if (!is_subclass_of($class, $interface)) {
  90                  if (!class_exists($class, false)) {
  91                      throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  92                  }
  93  
  94                  throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
  95              }
  96  
  97              $definition->addMethodCall('addSubscriberService', array($id, $class));
  98          }
  99      }
 100  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1