src/Sentry/SentryComponentSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Sentry;
  3. use Sentry\State\HubInterface;
  4. use Symfony\Component\Console\ConsoleEvents;
  5. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. final class SentryComponentSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private readonly ComponentContext $componentContext,
  13.         private readonly HttpComponentResolver $httpComponentResolver,
  14.         private readonly HubInterface $hub,
  15.     ) {}
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             KernelEvents::REQUEST => ['onKernelRequest'100],
  20.             KernelEvents::TERMINATE => 'reset',
  21.             ConsoleEvents::COMMAND => ['onConsoleCommand'100],
  22.             ConsoleEvents::TERMINATE => 'reset',
  23.         ];
  24.     }
  25.     public function onKernelRequest(RequestEvent $event): void
  26.     {
  27.         if (!$event->isMainRequest()) {
  28.             return;
  29.         }
  30.         $this->applyComponent(
  31.             $this->httpComponentResolver->resolveFromPathInfo($event->getRequest()->getPathInfo())
  32.         );
  33.     }
  34.     public function onConsoleCommand(ConsoleCommandEvent $event): void
  35.     {
  36.         $command $event->getCommand();
  37.         if (null !== $command && 'messenger:consume' === $command->getName()) {
  38.             return;
  39.         }
  40.         $this->applyComponent(Component::Cli);
  41.     }
  42.     public function reset(): void
  43.     {
  44.         $this->componentContext->reset();
  45.     }
  46.     private function applyComponent(Component $component): void
  47.     {
  48.         $this->componentContext->set($component);
  49.         $this->hub->configureScope(static function ($scope) use ($component): void {
  50.             $scope->setTag('component'$component->value);
  51.         });
  52.     }
  53. }