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

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.     public function __construct(string $shopwareVersion, array $plugins)
  29.     {
  30.         $this->shopwareVersion $shopwareVersion;
  31.         $this->plugins $plugins;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             KernelEvents::CONTROLLER => ['checkExpectations'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  37.         ];
  38.     }
  39.     public function checkExpectations(ControllerEvent $event): void
  40.     {
  41.         $request $event->getRequest();
  42.         if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE)) {
  43.             return;
  44.         }
  45.         /** @var RouteScope|array $scope */
  46.         $scope $request->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  47.         if ($scope instanceof RouteScope) {
  48.             $scope $scope->getScopes();
  49.         }
  50.         if (!\in_array(ApiRouteScope::ID$scopetrue)) {
  51.             return;
  52.         }
  53.         $expectations $this->checkPackages($request);
  54.         if (\count($expectations)) {
  55.             throw new ExceptionFailedException($expectations);
  56.         }
  57.     }
  58.     private function checkPackages(Request $request): array
  59.     {
  60.         // swag/plugin1:~6.1,swag/plugin2:~6.1
  61.         $extensionConstraints array_filter(explode(',', (string) $request->headers->get(PlatformRequest::HEADER_EXPECT_PACKAGES)));
  62.         if ($extensionConstraints === []) {
  63.             return [];
  64.         }
  65.         $plugins $this->getIndexedPackages();
  66.         $fails = [];
  67.         foreach ($extensionConstraints as $extension) {
  68.             $explode explode(':'$extension);
  69.             if (\count($explode) !== 2) {
  70.                 $fails[] = sprintf('Got invalid string: "%s"'$extension);
  71.                 continue;
  72.             }
  73.             $name $explode[0];
  74.             $constraint $explode[1];
  75.             if (isset($plugins[$name])) {
  76.                 $installedVersion $plugins[$name];
  77.             } else {
  78.                 try {
  79.                     $installedVersion InstalledVersions::getPrettyVersion($name);
  80.                 } catch (\OutOfBoundsException $e) {
  81.                     $fails[] = sprintf('Requested package: %s is not available'$name);
  82.                     continue;
  83.                 }
  84.                 if (\in_array($nameself::SHOPWARE_CORE_PACKAGEStrue)) {
  85.                     $installedVersion $this->shopwareVersion;
  86.                 }
  87.             }
  88.             if (Semver::satisfies($installedVersion$constraint)) {
  89.                 continue;
  90.             }
  91.             $fails[] = sprintf('Version constraint for %s is failed. Installed is: %s'$name$installedVersion);
  92.         }
  93.         return $fails;
  94.     }
  95.     /**
  96.      * Plugins are not in the InstalledPackages file until now
  97.      */
  98.     private function getIndexedPackages(): array
  99.     {
  100.         $versions = [];
  101.         foreach ($this->plugins as $plugin) {
  102.             if (!$plugin['active']) {
  103.                 continue;
  104.             }
  105.             $versions[$plugin['composerName']] = $plugin['version'];
  106.         }
  107.         return $versions;
  108.     }
  109. }