vendor/symfony/messenger/Middleware/TraceableMiddleware.php line 40

  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\Messenger\Middleware;
  11. use Symfony\Component\Messenger\Envelope;
  12. use Symfony\Component\Stopwatch\Stopwatch;
  13. /**
  14.  * Collects some data about a middleware.
  15.  *
  16.  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  17.  */
  18. class TraceableMiddleware implements MiddlewareInterface
  19. {
  20.     private Stopwatch $stopwatch;
  21.     private string $busName;
  22.     private string $eventCategory;
  23.     public function __construct(Stopwatch $stopwatchstring $busNamestring $eventCategory 'messenger.middleware')
  24.     {
  25.         $this->stopwatch $stopwatch;
  26.         $this->busName $busName;
  27.         $this->eventCategory $eventCategory;
  28.     }
  29.     public function handle(Envelope $envelopeStackInterface $stack): Envelope
  30.     {
  31.         $stack = new TraceableStack($stack$this->stopwatch$this->busName$this->eventCategory);
  32.         try {
  33.             return $stack->next()->handle($envelope$stack);
  34.         } finally {
  35.             $stack->stop();
  36.         }
  37.     }
  38. }
  39. /**
  40.  * @internal
  41.  */
  42. class TraceableStack implements StackInterface
  43. {
  44.     private StackInterface $stack;
  45.     private Stopwatch $stopwatch;
  46.     private string $busName;
  47.     private string $eventCategory;
  48.     private ?string $currentEvent null;
  49.     public function __construct(StackInterface $stackStopwatch $stopwatchstring $busNamestring $eventCategory)
  50.     {
  51.         $this->stack $stack;
  52.         $this->stopwatch $stopwatch;
  53.         $this->busName $busName;
  54.         $this->eventCategory $eventCategory;
  55.     }
  56.     public function next(): MiddlewareInterface
  57.     {
  58.         if (null !== $this->currentEvent && $this->stopwatch->isStarted($this->currentEvent)) {
  59.             $this->stopwatch->stop($this->currentEvent);
  60.         }
  61.         if ($this->stack === $nextMiddleware $this->stack->next()) {
  62.             $this->currentEvent 'Tail';
  63.         } else {
  64.             $this->currentEvent sprintf('"%s"'get_debug_type($nextMiddleware));
  65.         }
  66.         $this->currentEvent .= sprintf(' on "%s"'$this->busName);
  67.         $this->stopwatch->start($this->currentEvent$this->eventCategory);
  68.         return $nextMiddleware;
  69.     }
  70.     public function stop(): void
  71.     {
  72.         if (null !== $this->currentEvent && $this->stopwatch->isStarted($this->currentEvent)) {
  73.             $this->stopwatch->stop($this->currentEvent);
  74.         }
  75.         $this->currentEvent null;
  76.     }
  77. }