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

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