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

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