vendor/shopware/storefront/Theme/Subscriber/ThemeCompilerEnrichScssVarSubscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Subscriber;
  3. use Doctrine\DBAL\Exception as DBALException;
  4. use Shopware\Core\System\SystemConfig\Service\ConfigurationService;
  5. use Shopware\Storefront\Theme\Event\ThemeCompilerEnrichScssVariablesEvent;
  6. use Shopware\Storefront\Theme\StorefrontPluginRegistryInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  10.  */
  11. class ThemeCompilerEnrichScssVarSubscriber implements EventSubscriberInterface
  12. {
  13.     private ConfigurationService $configurationService;
  14.     private StorefrontPluginRegistryInterface $storefrontPluginRegistry;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(
  19.         ConfigurationService $configurationService,
  20.         StorefrontPluginRegistryInterface $storefrontPluginRegistry
  21.     ) {
  22.         $this->configurationService $configurationService;
  23.         $this->storefrontPluginRegistry $storefrontPluginRegistry;
  24.     }
  25.     /**
  26.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             ThemeCompilerEnrichScssVariablesEvent::class => 'enrichExtensionVars',
  32.         ];
  33.     }
  34.     /**
  35.      * @internal
  36.      */
  37.     public function enrichExtensionVars(ThemeCompilerEnrichScssVariablesEvent $event): void
  38.     {
  39.         $allConfigs = [];
  40.         if ($this->storefrontPluginRegistry->getConfigurations()->count() === 0) {
  41.             return;
  42.         }
  43.         try {
  44.             foreach ($this->storefrontPluginRegistry->getConfigurations() as $configuration) {
  45.                 $allConfigs array_merge(
  46.                     $allConfigs,
  47.                     $this->configurationService->getResolvedConfiguration(
  48.                         $configuration->getTechnicalName() . '.config',
  49.                         $event->getContext(),
  50.                         $event->getSalesChannelId()
  51.                     )
  52.                 );
  53.             }
  54.         } catch (DBALException $e) {
  55.             if (\defined('\STDERR')) {
  56.                 fwrite(
  57.                     \STDERR,
  58.                     'Warning: Failed to load plugin css configuration. Ignoring plugin css customizations. Message: '
  59.                     $e->getMessage() . \PHP_EOL
  60.                 );
  61.             }
  62.         }
  63.         foreach ($allConfigs as $card) {
  64.             if (!isset($card['elements']) || !\is_array($card['elements'])) {
  65.                 continue;
  66.             }
  67.             foreach ($card['elements'] as $element) {
  68.                 if (!$this->hasCssValue($element)) {
  69.                     continue;
  70.                 }
  71.                 $event->addVariable($element['config']['css'], $element['value'] ?? $element['defaultValue']);
  72.             }
  73.         }
  74.     }
  75.     /**
  76.      * @param mixed $element
  77.      */
  78.     private function hasCssValue($element): bool
  79.     {
  80.         if (!\is_array($element)) {
  81.             return false;
  82.         }
  83.         if (!\is_array($element['config'])) {
  84.             return false;
  85.         }
  86.         if (!isset($element['config']['css'])) {
  87.             return false;
  88.         }
  89.         if (!\is_string($element['value'] ?? $element['defaultValue'])) {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94. }