WrappedListener.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Stopwatch\Stopwatch;
  12. use Symfony\Component\EventDispatcher\Event;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. /**
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class WrappedListener
  18. {
  19. private $listener;
  20. private $name;
  21. private $called;
  22. private $stoppedPropagation;
  23. private $stopwatch;
  24. private $dispatcher;
  25. public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
  26. {
  27. $this->listener = $listener;
  28. $this->name = $name;
  29. $this->stopwatch = $stopwatch;
  30. $this->dispatcher = $dispatcher;
  31. $this->called = false;
  32. $this->stoppedPropagation = false;
  33. }
  34. public function getWrappedListener()
  35. {
  36. return $this->listener;
  37. }
  38. public function wasCalled()
  39. {
  40. return $this->called;
  41. }
  42. public function stoppedPropagation()
  43. {
  44. return $this->stoppedPropagation;
  45. }
  46. public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
  47. {
  48. $this->called = true;
  49. $e = $this->stopwatch->start($this->name, 'event_listener');
  50. call_user_func($this->listener, $event, $eventName, $this->dispatcher ?: $dispatcher);
  51. if ($e->isStarted()) {
  52. $e->stop();
  53. }
  54. if ($event->isPropagationStopped()) {
  55. $this->stoppedPropagation = true;
  56. }
  57. }
  58. }