Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Contextualizer.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware;
8
9 use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 class Contextualizer implements ContextualizerInterface
13 {
15 private $container;
16
18 private $namespace;
19
25 private $siteAccessNodeName;
26
28 private $availableSiteAccesses;
29
31 private $groupsBySiteAccess;
32
33 public function __construct(
34 ContainerInterface $containerBuilder,
35 $namespace,
36 $siteAccessNodeName,
37 array $availableSiteAccesses,
38 array $groupsBySiteAccess
39 ) {
40 $this->container = $containerBuilder;
41 $this->namespace = $namespace;
42 $this->siteAccessNodeName = $siteAccessNodeName;
43 $this->availableSiteAccesses = $availableSiteAccesses;
44 $this->groupsBySiteAccess = $groupsBySiteAccess;
45 }
46
47 public function setContextualParameter($parameterName, $scope, $value)
48 {
49 $this->container->setParameter("$this->namespace.$scope.$parameterName", $value);
50 }
51
52 public function mapSetting($id, array $config)
53 {
54 foreach ($config[$this->siteAccessNodeName] as $currentScope => $scopeSettings) {
55 if (isset($scopeSettings[$id])) {
56 $this->setContextualParameter($id, $currentScope, $scopeSettings[$id]);
57 }
58 }
59 }
60
61 public function mapConfigArray($id, array $config, $options = 0)
62 {
63 $this->mapReservedScopeArray($id, $config, ConfigResolver::SCOPE_DEFAULT);
64 $this->mapReservedScopeArray($id, $config, ConfigResolver::SCOPE_GLOBAL);
65 $defaultSettings = $this->getContainerParameter(
66 $this->namespace . '.' . ConfigResolver::SCOPE_DEFAULT . '.' . $id,
67 []
68 );
69 $globalSettings = $this->getContainerParameter(
70 $this->namespace . '.' . ConfigResolver::SCOPE_GLOBAL . '.' . $id,
71 []
72 );
73
74 foreach ($this->availableSiteAccesses as $scope) {
75 // for a siteaccess, we have to merge the default value,
76 // the group value(s), the siteaccess value and the global
77 // value of the settings.
78 $groupsSettings = [];
79 if (isset($this->groupsBySiteAccess[$scope]) && is_array($this->groupsBySiteAccess[$scope])) {
80 $groupsSettings = $this->groupsArraySetting(
81 $this->groupsBySiteAccess[$scope],
82 $id,
83 $config,
84 $options & static::MERGE_FROM_SECOND_LEVEL
85 );
86 }
87
88 $scopeSettings = [];
89 if (isset($config[$this->siteAccessNodeName][$scope][$id])) {
90 $scopeSettings = $config[$this->siteAccessNodeName][$scope][$id];
91 }
92
93 if (empty($groupsSettings) && empty($scopeSettings)) {
94 continue;
95 }
96
97 if ($options & static::MERGE_FROM_SECOND_LEVEL) {
98 // array_merge() has to be used because we don't
99 // know whether we have a hash or a plain array
100 $keys1 = array_unique(
101 array_merge(
102 array_keys($defaultSettings),
103 array_keys($groupsSettings),
104 array_keys($scopeSettings),
105 array_keys($globalSettings)
106 )
107 );
108 $mergedSettings = [];
109 foreach ($keys1 as $key) {
110 // Only merge if actual setting is an array.
111 // We assume default setting to be a clear reference for this.
112 // If the setting is not an array, we copy the right value, in respect to the precedence:
113 // 1. global
114 // 2. SiteAccess
115 // 3. Group
116 // 4. default
117 if (array_key_exists($key, $defaultSettings) && !is_array($defaultSettings[$key])) {
118 if (array_key_exists($key, $globalSettings)) {
119 $mergedSettings[$key] = $globalSettings[$key];
120 } elseif (array_key_exists($key, $scopeSettings)) {
121 $mergedSettings[$key] = $scopeSettings[$key];
122 } elseif (array_key_exists($key, $groupsSettings)) {
123 $mergedSettings[$key] = $groupsSettings[$key];
124 } else {
125 $mergedSettings[$key] = $defaultSettings[$key];
126 }
127 } else {
128 $mergedSettings[$key] = array_merge(
129 isset($defaultSettings[$key]) ? $defaultSettings[$key] : [],
130 isset($groupsSettings[$key]) ? $groupsSettings[$key] : [],
131 isset($scopeSettings[$key]) ? $scopeSettings[$key] : [],
132 isset($globalSettings[$key]) ? $globalSettings[$key] : []
133 );
134 }
135 }
136 } else {
137 $mergedSettings = array_merge(
138 $defaultSettings,
139 $groupsSettings,
140 $scopeSettings,
141 $globalSettings
142 );
143 }
144
145 if ($options & static::UNIQUE) {
146 $mergedSettings = array_values(
147 array_unique($mergedSettings)
148 );
149 }
150
151 $this->container->setParameter("$this->namespace.$scope.$id", $mergedSettings);
152 }
153 }
154
164 protected function getContainerParameter($id, $default = null)
165 {
166 if ($this->container->hasParameter($id)) {
167 return $this->container->getParameter($id);
168 }
169
170 return $default;
171 }
172
183 private function groupsArraySetting(array $groups, $id, array $config, $options = 0)
184 {
185 $groupsSettings = [];
186 sort($groups);
187 foreach ($groups as $group) {
188 if (isset($config[$this->siteAccessNodeName][$group][$id])) {
189 if ($options & static::MERGE_FROM_SECOND_LEVEL) {
190 foreach (array_keys($config[$this->siteAccessNodeName][$group][$id]) as $key) {
191 if (!isset($groupsSettings[$key])) {
192 $groupsSettings[$key] = $config[$this->siteAccessNodeName][$group][$id][$key];
193 } else {
194 // array_merge() has to be used because we don't
195 // know whether we have a hash or a plain array
196 $groupsSettings[$key] = array_merge(
197 $groupsSettings[$key],
198 $config[$this->siteAccessNodeName][$group][$id][$key]
199 );
200 }
201 }
202 } else {
203 // array_merge() has to be used because we don't
204 // know whether we have a hash or a plain array
205 $groupsSettings = array_merge(
206 $groupsSettings,
207 $config[$this->siteAccessNodeName][$group][$id]
208 );
209 }
210 }
211 }
212
213 return $groupsSettings;
214 }
215
224 private function mapReservedScopeArray($id, array $config, $scope)
225 {
226 if (
227 isset($config[$this->siteAccessNodeName][$scope][$id])
228 && !empty($config[$this->siteAccessNodeName][$scope][$id])
229 ) {
230 $key = "$this->namespace.$scope.$id";
231 $value = $config[$this->siteAccessNodeName][$scope][$id];
232 if ($this->container->hasParameter($key)) {
233 $value = array_merge(
234 $this->container->getParameter($key),
235 $value
236 );
237 }
238 $this->container->setParameter($key, $value);
239 }
240 }
241
242 public function setContainer(ContainerInterface $container)
243 {
244 $this->container = $container;
245 }
246
247 public function getContainer()
248 {
249 return $this->container;
250 }
251
252 public function setSiteAccessNodeName($scopeNodeName)
253 {
254 $this->siteAccessNodeName = $scopeNodeName;
255 }
256
257 public function getSiteAccessNodeName()
258 {
259 return $this->siteAccessNodeName;
260 }
261
262 public function setNamespace($namespace)
263 {
264 $this->namespace = $namespace;
265 }
266
267 public function getNamespace()
268 {
269 return $this->namespace;
270 }
271
272 public function setAvailableSiteAccesses(array $availableSiteAccesses)
273 {
274 $this->availableSiteAccesses = $availableSiteAccesses;
275 }
276
277 public function getAvailableSiteAccesses()
278 {
279 return $this->availableSiteAccesses;
280 }
281
282 public function setGroupsBySiteAccess(array $groupsBySiteAccess)
283 {
284 $this->groupsBySiteAccess = $groupsBySiteAccess;
285 }
286
287 public function getGroupsBySiteAccess()
288 {
289 return $this->groupsBySiteAccess;
290 }
291 }
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver
This class will help you get settings for a specific scope.
Definition: ConfigResolver.php:35
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver\SCOPE_DEFAULT
const SCOPE_DEFAULT
Definition: ConfigResolver.php:39
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver\SCOPE_GLOBAL
const SCOPE_GLOBAL
Definition: ConfigResolver.php:38
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer
Definition: Contextualizer.php:13
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\setContextualParameter
setContextualParameter($parameterName, $scope, $value)
Defines a contextual parameter in the container for given scope in current namespace.
Definition: Contextualizer.php:47
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\setSiteAccessNodeName
setSiteAccessNodeName($scopeNodeName)
Injects the name of the node under which scope based (semantic) configuration takes place.
Definition: Contextualizer.php:252
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\groupsArraySetting
groupsArraySetting(array $groups, $id, array $config, $options=0)
Merges setting array for a set of groups.
Definition: Contextualizer.php:183
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\__construct
__construct(ContainerInterface $containerBuilder, $namespace, $siteAccessNodeName, array $availableSiteAccesses, array $groupsBySiteAccess)
Definition: Contextualizer.php:33
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\mapConfigArray
mapConfigArray($id, array $config, $options=0)
Maps semantic array settings to internal format, and merges them between scopes.
Definition: Contextualizer.php:61
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\$availableSiteAccesses
$availableSiteAccesses
Definition: Contextualizer.php:28
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\getContainerParameter
getContainerParameter($id, $default=null)
Returns the value under the $id in the $container.
Definition: Contextualizer.php:164
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\getContainer
getContainer()
Definition: Contextualizer.php:247
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\getSiteAccessNodeName
getSiteAccessNodeName()
Definition: Contextualizer.php:257
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\$namespace
$namespace
Definition: Contextualizer.php:18
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\setGroupsBySiteAccess
setGroupsBySiteAccess(array $groupsBySiteAccess)
Injects names of registered SiteAccess groups, indexed by SiteAccess.
Definition: Contextualizer.php:282
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\setAvailableSiteAccesses
setAvailableSiteAccesses(array $availableSiteAccesses)
Injects registered SiteAccesses (i.e.
Definition: Contextualizer.php:272
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\$groupsBySiteAccess
$groupsBySiteAccess
Definition: Contextualizer.php:31
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\getAvailableSiteAccesses
getAvailableSiteAccesses()
Definition: Contextualizer.php:277
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\$container
$container
Definition: Contextualizer.php:15
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\$siteAccessNodeName
$siteAccessNodeName
Definition: Contextualizer.php:25
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\setContainer
setContainer(ContainerInterface $container)
Definition: Contextualizer.php:242
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\mapReservedScopeArray
mapReservedScopeArray($id, array $config, $scope)
Ensures settings array defined in a given "reserved scope" are registered properly.
Definition: Contextualizer.php:224
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\getNamespace
getNamespace()
Definition: Contextualizer.php:267
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\mapSetting
mapSetting($id, array $config)
Maps a semantic setting to internal format for all declared scopes.
Definition: Contextualizer.php:52
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\setNamespace
setNamespace($namespace)
Injects namespace for internal settings.
Definition: Contextualizer.php:262
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Contextualizer\getGroupsBySiteAccess
getGroupsBySiteAccess()
Definition: Contextualizer.php:287
$containerBuilder
if(!isset($installDir)) $containerBuilder
Definition: containerBuilder.php:19
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface
Definition: ContextualizerInterface.php:12
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware
Definition: EzPublishCoreBundle/DependencyInjection/Configuration/SiteAccessAware/Configuration.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.