vendor/shopware/storefront/Theme/ThemeConfigValueAccessor.php line 64

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  5. #[Package('storefront')]
  6. class ThemeConfigValueAccessor
  7. {
  8.     /**
  9.      * @var array<string, mixed>
  10.      */
  11.     private array $themeConfig = [];
  12.     /**
  13.      * @var array<string, bool>
  14.      */
  15.     private array $keys = ['all' => true];
  16.     /**
  17.      * @var array<string, array<string, bool>>
  18.      */
  19.     private array $traces = [];
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(private readonly AbstractResolvedConfigLoader $themeConfigLoader)
  24.     {
  25.     }
  26.     public static function buildName(string $key): string
  27.     {
  28.         return 'theme.' $key;
  29.     }
  30.     /**
  31.      * @return string|bool|array<string, mixed>|float|int|null
  32.      */
  33.     public function get(string $keySalesChannelContext $context, ?string $themeId)
  34.     {
  35.         foreach (array_keys($this->keys) as $trace) {
  36.             $this->traces[$trace][self::buildName($key)] = true;
  37.         }
  38.         $config $this->getThemeConfig($context$themeId);
  39.         if (\array_key_exists($key$config)) {
  40.             return $config[$key];
  41.         }
  42.         return null;
  43.     }
  44.     /**
  45.      * @return mixed|null All kind of data could be cached
  46.      */
  47.     public function trace(string $key\Closure $param)
  48.     {
  49.         $this->traces[$key] = [];
  50.         $this->keys[$key] = true;
  51.         $result $param();
  52.         unset($this->keys[$key]);
  53.         return $result;
  54.     }
  55.     /**
  56.      * @return array<int, string>
  57.      */
  58.     public function getTrace(string $key): array
  59.     {
  60.         $trace = isset($this->traces[$key]) ? array_keys($this->traces[$key]) : [];
  61.         unset($this->traces[$key]);
  62.         return $trace;
  63.     }
  64.     /**
  65.      * @return array<string, mixed>
  66.      */
  67.     private function getThemeConfig(SalesChannelContext $context, ?string $themeId): array
  68.     {
  69.         $key $context->getSalesChannelId() . $context->getDomainId() . $themeId;
  70.         if (isset($this->themeConfig[$key])) {
  71.             return $this->themeConfig[$key];
  72.         }
  73.         $themeConfig = [
  74.             'breakpoint' => [
  75.                 'xs' => 0,
  76.                 'sm' => 576,
  77.                 'md' => 768,
  78.                 'lg' => 992,
  79.                 'xl' => 1200,
  80.                 'xxl' => 1400,
  81.             ],
  82.         ];
  83.         if (!$themeId) {
  84.             return $this->themeConfig[$key] = $this->flatten($themeConfignull);
  85.         }
  86.         $themeConfig array_merge(
  87.             $themeConfig,
  88.             [
  89.                 'assets' => [
  90.                     'css' => [
  91.                         '/css/all.css',
  92.                     ],
  93.                     'js' => [
  94.                         '/js/all.js',
  95.                     ],
  96.                 ],
  97.             ],
  98.             $this->themeConfigLoader->load($themeId$context)
  99.         );
  100.         return $this->themeConfig[$key] = $this->flatten($themeConfignull);
  101.     }
  102.     /**
  103.      * @param array<string, mixed> $values
  104.      *
  105.      * @return array<string, mixed>
  106.      */
  107.     private function flatten(array $values, ?string $prefix): array
  108.     {
  109.         $prefix $prefix $prefix '.' '';
  110.         $flat = [];
  111.         foreach ($values as $key => $value) {
  112.             $isNested \is_array($value) && !isset($value[0]);
  113.             if (!$isNested) {
  114.                 $flat[$prefix $key] = $value;
  115.                 continue;
  116.             }
  117.             $nested $this->flatten($value$prefix $key);
  118.             foreach ($nested as $nestedKey => $nestedValue) {
  119.                 $flat[$nestedKey] = $nestedValue;
  120.             }
  121.         }
  122.         return $flat;
  123.     }
  124. }