vendor/sylius/sylius/src/Sylius/Bundle/ApiBundle/EventSubscriber/KernelRequestEventSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ApiBundle\EventSubscriber;
  12. use ApiPlatform\Core\EventListener\EventPriorities;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. /** @experimental  */
  18. final class KernelRequestEventSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private bool $apiEnabled,
  22.         private string $apiRoute,
  23.     ) {
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::REQUEST => ['validateApi'EventPriorities::PRE_VALIDATE],
  29.         ];
  30.     }
  31.     public function validateApi(RequestEvent $event): void
  32.     {
  33.         $pathInfo $event->getRequest()->getPathInfo();
  34.         if ($this->apiEnabled === false && str_contains($pathInfo$this->apiRoute)) {
  35.             throw new NotFoundHttpException('Route not found');
  36.         }
  37.     }
  38. }