|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Storefront\Framework\Twig\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Shopware\Core\Framework\Log\Package; |
|
6
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
|
7
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelEntity; |
|
|
|
|
|
|
8
|
|
|
use Shopware\Storefront\Framework\Twig\TemplateConfigAccessor; |
|
9
|
|
|
use Twig\Extension\AbstractExtension; |
|
10
|
|
|
use Twig\TwigFunction; |
|
11
|
|
|
|
|
12
|
|
|
#[Package('storefront')] |
|
13
|
|
|
class ConfigExtension extends AbstractExtension |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(private readonly TemplateConfigAccessor $config) |
|
19
|
|
|
{ |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getFunctions(): array |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
new TwigFunction('config', $this->config(...), ['needs_context' => true]), |
|
26
|
|
|
new TwigFunction('theme_config', $this->theme(...), ['needs_context' => true]), |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return string|bool|array|float|int|null |
|
32
|
|
|
*/ |
|
33
|
|
|
public function config(array $context, string $key) |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->config->config($key, $this->getSalesChannelId($context)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return string|bool|array|float|int|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function theme(array $context, string $key) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->config->theme($key, $this->getContext($context), $this->getThemeId($context)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function getSalesChannelId(array $context): ?string |
|
47
|
|
|
{ |
|
48
|
|
|
if (isset($context['context'])) { |
|
49
|
|
|
$salesChannelContext = $context['context']; |
|
50
|
|
|
if ($salesChannelContext instanceof SalesChannelContext) { |
|
51
|
|
|
return $salesChannelContext->getSalesChannelId(); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
if (isset($context['salesChannel'])) { |
|
55
|
|
|
$salesChannel = $context['salesChannel']; |
|
56
|
|
|
if ($salesChannel instanceof SalesChannelEntity) { |
|
57
|
|
|
return $salesChannel->getId(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function getThemeId(array $context): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
return $context['themeId'] ?? null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function getContext(array $context): SalesChannelContext |
|
70
|
|
|
{ |
|
71
|
|
|
if (!isset($context['context'])) { |
|
72
|
|
|
throw new \RuntimeException('Missing sales channel context object'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$context = $context['context']; |
|
76
|
|
|
|
|
77
|
|
|
if (!$context instanceof SalesChannelContext) { |
|
78
|
|
|
throw new \RuntimeException('Missing sales channel context object'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $context; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths