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

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. /**
  15.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  16.  *
  17.  * @phpstan-type PluginData array{'composerName': string, 'active': bool, 'version': string}
  18.  */
  19. class ExpectationSubscriber implements EventSubscriberInterface
  20. {
  21.     private const SHOPWARE_CORE_PACKAGES = [
  22.         'shopware/platform',
  23.         'shopware/core',
  24.         'shopware/administration',
  25.         'shopware/elasticsearch',
  26.         'shopware/storefront',
  27.     ];
  28.     private string $shopwareVersion;
  29.     /**
  30.      * @var list<PluginData>
  31.      */
  32.     private array $plugins;
  33.     /**
  34.      * @internal
  35.      *
  36.      * @param list<PluginData> $plugins
  37.      */
  38.     public function __construct(string $shopwareVersion, array $plugins)
  39.     {
  40.         $this->shopwareVersion $shopwareVersion;
  41.         $this->plugins $plugins;
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             KernelEvents::CONTROLLER => ['checkExpectations'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  47.         ];
  48.     }
  49.     public function checkExpectations(ControllerEvent $event): void
  50.     {
  51.         $request $event->getRequest();
  52.         if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE)) {
  53.             return;
  54.         }
  55.         /** @var RouteScope|list<string> $scope */
  56.         $scope $request->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  57.         if ($scope instanceof RouteScope) {
  58.             $scope $scope->getScopes();
  59.         }
  60.         if (!\in_array(ApiRouteScope::ID$scopetrue)) {
  61.             return;
  62.         }
  63.         $expectations $this->checkPackages($request);
  64.         if (\count($expectations)) {
  65.             throw new ExceptionFailedException($expectations);
  66.         }
  67.     }
  68.     /**
  69.      * @return list<string>
  70.      */
  71.     private function checkPackages(Request $request): array
  72.     {
  73.         // swag/plugin1:~6.1,swag/plugin2:~6.1
  74.         $extensionConstraints array_filter(explode(',', (string) $request->headers->get(PlatformRequest::HEADER_EXPECT_PACKAGES)));
  75.         if ($extensionConstraints === []) {
  76.             return [];
  77.         }
  78.         $plugins $this->getIndexedPackages();
  79.         $fails = [];
  80.         foreach ($extensionConstraints as $extension) {
  81.             $explode explode(':'$extension);
  82.             if (\count($explode) !== 2) {
  83.                 $fails[] = sprintf('Got invalid string: "%s"'$extension);
  84.                 continue;
  85.             }
  86.             $name $explode[0];
  87.             $constraint $explode[1];
  88.             if (isset($plugins[$name])) {
  89.                 $installedVersion $plugins[$name];
  90.             } else {
  91.                 try {
  92.                     $installedVersion InstalledVersions::getPrettyVersion($name);
  93.                 } catch (\OutOfBoundsException $e) {
  94.                     $fails[] = sprintf('Requested package: %s is not available'$name);
  95.                     continue;
  96.                 }
  97.                 if (\in_array($nameself::SHOPWARE_CORE_PACKAGEStrue)) {
  98.                     $installedVersion $this->shopwareVersion;
  99.                 }
  100.             }
  101.             if ($installedVersion === null) {
  102.                 // should never happen, but phpstan would complain otherwise
  103.                 continue;
  104.             }
  105.             if (Semver::satisfies($installedVersion$constraint)) {
  106.                 continue;
  107.             }
  108.             $fails[] = sprintf('Version constraint for %s is failed. Installed is: %s'$name$installedVersion);
  109.         }
  110.         return $fails;
  111.     }
  112.     /**
  113.      * Plugins are not in the InstalledPackages file until now
  114.      *
  115.      * @return array<string, string>
  116.      */
  117.     private function getIndexedPackages(): array
  118.     {
  119.         $versions = [];
  120.         foreach ($this->plugins as $plugin) {
  121.             if (!$plugin['active']) {
  122.                 continue;
  123.             }
  124.             $versions[$plugin['composerName']] = $plugin['version'];
  125.         }
  126.         return $versions;
  127.     }
  128. }