WrappedListener.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Component\EventDispatcher\Debug;
  11. use Symfony\Component\EventDispatcher\Event;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\Stopwatch\Stopwatch;
  14. use Symfony\Component\VarDumper\Caster\ClassStub;
  15. /**
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class WrappedListener
  19. {
  20. private $listener;
  21. private $name;
  22. private $called;
  23. private $stoppedPropagation;
  24. private $stopwatch;
  25. private $dispatcher;
  26. private $pretty;
  27. private $stub;
  28. private static $hasClassStub;
  29. public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
  30. {
  31. $this->listener = $listener;
  32. $this->name = $name;
  33. $this->stopwatch = $stopwatch;
  34. $this->dispatcher = $dispatcher;
  35. $this->called = false;
  36. $this->stoppedPropagation = false;
  37. if (\is_array($listener)) {
  38. $this->name = \is_object($listener[0]) ? \get_class($listener[0]) : $listener[0];
  39. $this->pretty = $this->name.'::'.$listener[1];
  40. } elseif ($listener instanceof \Closure) {
  41. $this->pretty = $this->name = 'closure';
  42. } elseif (\is_string($listener)) {
  43. $this->pretty = $this->name = $listener;
  44. } else {
  45. $this->name = \get_class($listener);
  46. $this->pretty = $this->name.'::__invoke';
  47. }
  48. if (null !== $name) {
  49. $this->name = $name;
  50. }
  51. if (null === self::$hasClassStub) {
  52. self::$hasClassStub = class_exists(ClassStub::class);
  53. }
  54. }
  55. public function getWrappedListener()
  56. {
  57. return $this->listener;
  58. }
  59. public function wasCalled()
  60. {
  61. return $this->called;
  62. }
  63. public function stoppedPropagation()
  64. {
  65. return $this->stoppedPropagation;
  66. }
  67. public function getPretty()
  68. {
  69. return $this->pretty;
  70. }
  71. public function getInfo($eventName)
  72. {
  73. if (null === $this->stub) {
  74. $this->stub = self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()';
  75. }
  76. return array(
  77. 'event' => $eventName,
  78. 'priority' => null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null,
  79. 'pretty' => $this->pretty,
  80. 'stub' => $this->stub,
  81. );
  82. }
  83. public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
  84. {
  85. $this->called = true;
  86. $e = $this->stopwatch->start($this->name, 'event_listener');
  87. \call_user_func($this->listener, $event, $eventName, $this->dispatcher ?: $dispatcher);
  88. if ($e->isStarted()) {
  89. $e->stop();
  90. }
  91. if ($event->isPropagationStopped()) {
  92. $this->stoppedPropagation = true;
  93. }
  94. }
  95. }