|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\System\SystemConfig\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Shopware\Core\Framework\App\AppEntity; |
|
6
|
|
|
use Shopware\Core\Framework\App\Lifecycle\AbstractAppLoader; |
|
7
|
|
|
use Shopware\Core\Framework\Context; |
|
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; |
|
9
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
|
10
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; |
|
11
|
|
|
use Shopware\Core\Framework\Feature; |
|
12
|
|
|
use Shopware\Core\System\SystemConfig\Exception\BundleConfigNotFoundException; |
|
13
|
|
|
use Shopware\Core\System\SystemConfig\Exception\ConfigurationNotFoundException; |
|
14
|
|
|
use Shopware\Core\System\SystemConfig\Util\ConfigReader; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
16
|
|
|
|
|
17
|
|
|
class ConfigurationService |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $bundles; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ConfigReader |
|
26
|
|
|
*/ |
|
27
|
|
|
private $configReader; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var AbstractAppLoader |
|
31
|
|
|
*/ |
|
32
|
|
|
private $appLoader; |
|
33
|
4 |
|
|
|
34
|
|
|
/** |
|
35
|
4 |
|
* @var EntityRepositoryInterface |
|
36
|
4 |
|
*/ |
|
37
|
4 |
|
private $appRepository; |
|
38
|
4 |
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param BundleInterface[] $bundles |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
3 |
|
iterable $bundles, |
|
44
|
|
|
ConfigReader $configReader, |
|
45
|
3 |
|
AbstractAppLoader $appLoader, |
|
46
|
|
|
EntityRepositoryInterface $appRepository |
|
47
|
3 |
|
) { |
|
48
|
1 |
|
$this->bundles = $bundles; |
|
|
|
|
|
|
49
|
|
|
$this->configReader = $configReader; |
|
50
|
|
|
$this->appLoader = $appLoader; |
|
51
|
2 |
|
$this->appRepository = $appRepository; |
|
52
|
|
|
} |
|
53
|
1 |
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws ConfigurationNotFoundException |
|
56
|
2 |
|
* @throws \InvalidArgumentException |
|
57
|
|
|
* @throws BundleConfigNotFoundException |
|
58
|
2 |
|
* |
|
59
|
|
|
* @deprecated tag:v6.4.0 $context param will be required |
|
60
|
2 |
|
*/ |
|
61
|
|
|
public function getConfiguration(string $domain, ?Context $context = null): array |
|
62
|
2 |
|
{ |
|
63
|
1 |
|
if (!$context) { |
|
64
|
|
|
$context = Context::createDefaultContext(); |
|
65
|
|
|
} |
|
66
|
2 |
|
|
|
67
|
1 |
|
$validDomain = preg_match('/^([\w-]+)\.?([\w-]*)$/', $domain, $match); |
|
68
|
|
|
|
|
69
|
|
|
if (!$validDomain) { |
|
70
|
1 |
|
throw new \InvalidArgumentException('Expected domain'); |
|
71
|
1 |
|
} |
|
72
|
1 |
|
|
|
73
|
1 |
|
$scope = $match[1]; |
|
74
|
|
|
$configName = $match[2] !== '' ? $match[2] : null; |
|
75
|
|
|
|
|
76
|
1 |
|
$config = $this->fetchConfiguration($scope === 'core' ? 'System' : $scope, $configName, $context); |
|
77
|
|
|
if (!$config) { |
|
78
|
1 |
|
throw new ConfigurationNotFoundException($scope); |
|
79
|
|
|
} |
|
80
|
1 |
|
|
|
81
|
|
|
$domain = rtrim($domain, '.') . '.'; |
|
82
|
|
|
|
|
83
|
2 |
|
foreach ($config as $i => $card) { |
|
84
|
|
|
foreach ($card['elements'] as $j => $field) { |
|
85
|
2 |
|
$newField = ['name' => $domain . $field['name']]; |
|
86
|
|
|
|
|
87
|
2 |
|
if (\array_key_exists('flag', $field)) { |
|
88
|
2 |
|
try { |
|
89
|
2 |
|
if (!Feature::isActive($field['flag'])) { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
} catch (\RuntimeException $e) { |
|
93
|
2 |
|
continue; |
|
94
|
|
|
} |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (array_key_exists('type', $field)) { |
|
98
|
3 |
|
$newField['type'] = $field['type']; |
|
99
|
|
|
} |
|
100
|
3 |
|
|
|
101
|
3 |
|
unset($field['type'], $field['name']); |
|
102
|
2 |
|
if ($field === []) { |
|
103
|
|
|
$field = new \stdClass(); |
|
104
|
|
|
} |
|
105
|
|
|
$newField['config'] = $field; |
|
106
|
1 |
|
$card['elements'][$j] = $newField; |
|
107
|
|
|
} |
|
108
|
|
|
$config[$i] = $card; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $config; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @deprecated tag:v6.4.0 $context param will be required |
|
116
|
|
|
*/ |
|
117
|
|
|
public function checkConfiguration(string $domain, ?Context $context = null): bool |
|
118
|
|
|
{ |
|
119
|
|
|
try { |
|
120
|
|
|
$this->getConfiguration($domain, $context); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
return true; |
|
123
|
|
|
} catch (\InvalidArgumentException | ConfigurationNotFoundException | BundleConfigNotFoundException $e) { |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function fetchConfiguration(string $scope, ?string $configName, Context $context): ?array |
|
129
|
|
|
{ |
|
130
|
|
|
$technicalName = array_slice(explode('\\', $scope), -1)[0]; |
|
131
|
|
|
foreach ($this->bundles as $bundle) { |
|
132
|
|
|
if ($bundle->getName() === $technicalName) { |
|
133
|
|
|
return $this->configReader->getConfigFromBundle($bundle, $configName); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$app = $this->getAppByName($technicalName, $context); |
|
138
|
|
|
if ($app) { |
|
139
|
|
|
return $this->appLoader->getConfiguration($app); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
private function getAppByName(string $name, Context $context): ?AppEntity |
|
146
|
|
|
{ |
|
147
|
|
|
$criteria = new Criteria(); |
|
148
|
|
|
$criteria->addFilter(new EqualsFilter('name', $name)); |
|
149
|
|
|
|
|
150
|
|
|
/** @var AppEntity|null $result */ |
|
151
|
|
|
$result = $this->appRepository->search($criteria, $context)->first(); |
|
152
|
|
|
|
|
153
|
|
|
return $result; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..