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