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

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