vendor/shopware/core/Framework/Api/ApiVersion/ApiVersionSubscriber.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\ApiVersion;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. class ApiVersionSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var int[]
  11.      */
  12.     private $supportedApiVersions;
  13.     public function __construct(array $supportedApiVersions)
  14.     {
  15.         $this->supportedApiVersions $supportedApiVersions;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             KernelEvents::REQUEST => 'checkIfVersionIsSupported',
  21.         ];
  22.     }
  23.     public function checkIfVersionIsSupported(RequestEvent $event): void
  24.     {
  25.         $path $event->getRequest()->getPathInfo();
  26.         $matches = [];
  27.         // https://regex101.com/r/BHG1ab/1
  28.         if (!preg_match('/^\/(api|sales-channel-api)\/v(?P<version>\d)\/.*$/'$path$matches)) {
  29.             return;
  30.         }
  31.         $requestedVersion = (int) $matches['version'];
  32.         if (in_array($requestedVersion$this->supportedApiVersionstrue)) {
  33.             return;
  34.         }
  35.         throw new NotFoundHttpException(
  36.             sprintf(
  37.                 'Requested api version v%d not available, available versions are v%s.',
  38.                 $requestedVersion,
  39.                 implode(', v'$this->supportedApiVersions)
  40.             )
  41.         );
  42.     }
  43. }