LazyEventDispatcher.php 880 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace JMS\Serializer\EventDispatcher;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. class LazyEventDispatcher extends EventDispatcher
  5. {
  6. private $container;
  7. public function __construct(ContainerInterface $container)
  8. {
  9. $this->container = $container;
  10. }
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected function initializeListeners($eventName, $loweredClass, $format)
  15. {
  16. $listeners = parent::initializeListeners($eventName, $loweredClass, $format);
  17. foreach ($listeners as &$listener) {
  18. if ( ! is_array($listener) || ! is_string($listener[0])) {
  19. continue;
  20. }
  21. if ( ! $this->container->has($listener[0])) {
  22. continue;
  23. }
  24. $listener[0] = $this->container->get($listener[0]);
  25. }
  26. return $listeners;
  27. }
  28. }