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

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