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

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