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

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