src/Sentry/SentryComponentSubscriber.php line 29

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 HubInterface $hub,
  14.     ) {}
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             KernelEvents::REQUEST => ['onKernelRequest'100],
  19.             KernelEvents::TERMINATE => 'reset',
  20.             ConsoleEvents::COMMAND => ['onConsoleCommand'100],
  21.             ConsoleEvents::TERMINATE => 'reset',
  22.         ];
  23.     }
  24.     public function onKernelRequest(RequestEvent $event): void
  25.     {
  26.         if (!$event->isMainRequest()) {
  27.             return;
  28.         }
  29.         $this->applyComponent(Component::Web);
  30.     }
  31.     public function onConsoleCommand(ConsoleCommandEvent $event): void
  32.     {
  33.         $command $event->getCommand();
  34.         if (null !== $command && 'messenger:consume' === $command->getName()) {
  35.             return;
  36.         }
  37.         $this->applyComponent(Component::Cli);
  38.     }
  39.     public function reset(): void
  40.     {
  41.         $this->componentContext->reset();
  42.     }
  43.     private function applyComponent(Component $component): void
  44.     {
  45.         $this->componentContext->set($component);
  46.         $this->hub->configureScope(static function ($scope) use ($component): void {
  47.             $scope->setTag('component'$component->value);
  48.         });
  49.     }
  50. }