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.         // This route can be used to determine the Shopware version on any Shopware version
  27.         if ($path === '/api/v1/_info/version') {
  28.             return;
  29.         }
  30.         $matches = [];
  31.         // https://regex101.com/r/BHG1ab/1
  32.         if (!preg_match('/^\/(api|sales-channel-api)\/v(?P<version>\d)\/.*$/'$path$matches)) {
  33.             return;
  34.         }
  35.         $requestedVersion = (int) $matches['version'];
  36.         if (\in_array($requestedVersion$this->supportedApiVersionstrue)) {
  37.             return;
  38.         }
  39.         throw new NotFoundHttpException(
  40.             sprintf(
  41.                 'Requested api version v%d not available, available versions are v%s.',
  42.                 $requestedVersion,
  43.                 implode(', v'$this->supportedApiVersions)
  44.             )
  45.         );
  46.     }
  47. }