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

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