ContainerAwareEventDispatcher.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\EventDispatcher\Event;
  14. /**
  15. * Lazily loads listeners and subscribers from the dependency injection
  16. * container
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  20. */
  21. class ContainerAwareEventDispatcher extends EventDispatcher
  22. {
  23. /**
  24. * The container from where services are loaded
  25. * @var ContainerInterface
  26. */
  27. private $container;
  28. /**
  29. * The service IDs of the event listeners and subscribers
  30. * @var array
  31. */
  32. private $listenerIds = array();
  33. /**
  34. * The services registered as listeners
  35. * @var array
  36. */
  37. private $listeners = array();
  38. /**
  39. * Constructor.
  40. *
  41. * @param ContainerInterface $container A ContainerInterface instance
  42. */
  43. public function __construct(ContainerInterface $container)
  44. {
  45. $this->container = $container;
  46. }
  47. /**
  48. * Adds a service as event listener
  49. *
  50. * @param string $eventName Event for which the listener is added
  51. * @param array $callback The service ID of the listener service & the method
  52. * name that has to be called
  53. * @param integer $priority The higher this value, the earlier an event listener
  54. * will be triggered in the chain.
  55. * Defaults to 0.
  56. */
  57. public function addListenerService($eventName, $callback, $priority = 0)
  58. {
  59. if (!is_array($callback) || 2 !== count($callback)) {
  60. throw new \InvalidArgumentException('Expected an array("service", "method") argument');
  61. }
  62. $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
  63. }
  64. /**
  65. * {@inheritDoc}
  66. *
  67. * Lazily loads listeners for this event from the dependency injection
  68. * container.
  69. *
  70. * @throws \InvalidArgumentException if the service is not defined
  71. */
  72. public function dispatch($eventName, Event $event = null)
  73. {
  74. if (isset($this->listenerIds[$eventName])) {
  75. foreach ($this->listenerIds[$eventName] as $args) {
  76. list($serviceId, $method, $priority) = $args;
  77. $listener = $this->container->get($serviceId);
  78. $key = $serviceId.'.'.$method;
  79. if (!isset($this->listeners[$eventName][$key])) {
  80. $this->addListener($eventName, array($listener, $method), $priority);
  81. } elseif ($listener !== $this->listeners[$eventName][$key]) {
  82. $this->removeListener($eventName, array($this->listeners[$eventName][$key], $method));
  83. $this->addListener($eventName, array($listener, $method), $priority);
  84. }
  85. $this->listeners[$eventName][$key] = $listener;
  86. }
  87. }
  88. parent::dispatch($eventName, $event);
  89. }
  90. }