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

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