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

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.     private bool $apiEnabled;
  21.     private string $apiRoute;
  22.     public function __construct(bool $apiEnabledstring $apiRoute)
  23.     {
  24.         $this->apiEnabled $apiEnabled;
  25.         $this->apiRoute $apiRoute;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             KernelEvents::REQUEST => ['validateApi'EventPriorities::PRE_VALIDATE],
  31.         ];
  32.     }
  33.     public function validateApi(RequestEvent $event): void
  34.     {
  35.         $pathInfo $event->getRequest()->getPathInfo();
  36.         if ($this->apiEnabled === false && strpos($pathInfo$this->apiRoute) !== false) {
  37.             throw new NotFoundHttpException('Route not found');
  38.         }
  39.     }
  40. }