vendor/shopware/core/Framework/Api/EventListener/ExpectationSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\EventListener;
  3. use Composer\InstalledVersions;
  4. use Composer\Semver\Semver;
  5. use Shopware\Core\Framework\Api\Exception\ExceptionFailedException;
  6. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  7. use Shopware\Core\Framework\Routing\ApiRouteScope;
  8. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  9. use Shopware\Core\PlatformRequest;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class ExpectationSubscriber implements EventSubscriberInterface
  15. {
  16.     private const SHOPWARE_CORE_PACKAGES = [
  17.         'shopware/platform',
  18.         'shopware/core',
  19.         'shopware/administration',
  20.         'shopware/elasticsearch',
  21.         'shopware/storefront',
  22.     ];
  23.     private string $shopwareVersion;
  24.     /**
  25.      * @var array{'composerName': string, 'active': bool, 'version': string}[]
  26.      */
  27.     private array $plugins;
  28.     /**
  29.      * @internal
  30.      */
  31.     public function __construct(string $shopwareVersion, array $plugins)
  32.     {
  33.         $this->shopwareVersion $shopwareVersion;
  34.         $this->plugins $plugins;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             KernelEvents::CONTROLLER => ['checkExpectations'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  40.         ];
  41.     }
  42.     public function checkExpectations(ControllerEvent $event): void
  43.     {
  44.         $request $event->getRequest();
  45.         if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE)) {
  46.             return;
  47.         }
  48.         /** @var RouteScope|array $scope */
  49.         $scope $request->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  50.         if ($scope instanceof RouteScope) {
  51.             $scope $scope->getScopes();
  52.         }
  53.         if (!\in_array(ApiRouteScope::ID$scopetrue)) {
  54.             return;
  55.         }
  56.         $expectations $this->checkPackages($request);
  57.         if (\count($expectations)) {
  58.             throw new ExceptionFailedException($expectations);
  59.         }
  60.     }
  61.     private function checkPackages(Request $request): array
  62.     {
  63.         // swag/plugin1:~6.1,swag/plugin2:~6.1
  64.         $extensionConstraints array_filter(explode(',', (string) $request->headers->get(PlatformRequest::HEADER_EXPECT_PACKAGES)));
  65.         if ($extensionConstraints === []) {
  66.             return [];
  67.         }
  68.         $plugins $this->getIndexedPackages();
  69.         $fails = [];
  70.         foreach ($extensionConstraints as $extension) {
  71.             $explode explode(':'$extension);
  72.             if (\count($explode) !== 2) {
  73.                 $fails[] = sprintf('Got invalid string: "%s"'$extension);
  74.                 continue;
  75.             }
  76.             $name $explode[0];
  77.             $constraint $explode[1];
  78.             if (isset($plugins[$name])) {
  79.                 $installedVersion $plugins[$name];
  80.             } else {
  81.                 try {
  82.                     $installedVersion InstalledVersions::getPrettyVersion($name);
  83.                 } catch (\OutOfBoundsException $e) {
  84.                     $fails[] = sprintf('Requested package: %s is not available'$name);
  85.                     continue;
  86.                 }
  87.                 if (\in_array($nameself::SHOPWARE_CORE_PACKAGEStrue)) {
  88.                     $installedVersion $this->shopwareVersion;
  89.                 }
  90.             }
  91.             if (Semver::satisfies($installedVersion$constraint)) {
  92.                 continue;
  93.             }
  94.             $fails[] = sprintf('Version constraint for %s is failed. Installed is: %s'$name$installedVersion);
  95.         }
  96.         return $fails;
  97.     }
  98.     /**
  99.      * Plugins are not in the InstalledPackages file until now
  100.      */
  101.     private function getIndexedPackages(): array
  102.     {
  103.         $versions = [];
  104.         foreach ($this->plugins as $plugin) {
  105.             if (!$plugin['active']) {
  106.                 continue;
  107.             }
  108.             $versions[$plugin['composerName']] = $plugin['version'];
  109.         }
  110.         return $versions;
  111.     }
  112. }