forked from EasyCorp/EasyAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.php
More file actions
210 lines (184 loc) · 6.39 KB
/
ConfigManager.php
File metadata and controls
210 lines (184 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JavierEguiluz\Bundle\EasyAdminBundle\Configuration;
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\UndefinedEntityException;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Manages the loading and processing of backend configuration and it provides
* useful methods to get the configuration for the entire backend, for a single
* entity, for a single action, etc.
*
* @author Javier Eguiluz <[email protected]>
*/
class ConfigManager
{
/** @var array */
private $backendConfig;
/** @var ContainerInterface */
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* Returns the entire backend configuration or just the configuration for
* the optional property path. Example: getBackendConfig('design.menu').
*
* @param string|null $propertyPath
*
* @return array
*/
public function getBackendConfig($propertyPath = null)
{
if (null === $this->backendConfig) {
$this->backendConfig = $this->processConfig();
}
if (empty($propertyPath)) {
return $this->backendConfig;
}
// turns 'design.menu' into '[design][menu]', the format required by PropertyAccess
$propertyPath = '['.str_replace('.', '][', $propertyPath).']';
return $this->container->get('property_accessor')->getValue($this->backendConfig, $propertyPath);
}
/**
* Returns the configuration for the given entity name.
*
* @param string $entityName
*
* @deprecated Use getEntityConfig()
*
* @return array The full entity configuration
*
* @throws \InvalidArgumentException when the entity isn't managed by EasyAdmin
*/
public function getEntityConfiguration($entityName)
{
return $this->getEntityConfig($entityName);
}
/**
* Returns the configuration for the given entity name.
*
* @param string $entityName
*
* @return array The full entity configuration
*
* @throws \InvalidArgumentException
*/
public function getEntityConfig($entityName)
{
$backendConfig = $this->getBackendConfig();
if (!isset($backendConfig['entities'][$entityName])) {
throw new UndefinedEntityException(array('entity_name' => $entityName));
}
return $backendConfig['entities'][$entityName];
}
/**
* Returns the full entity config for the given entity class.
*
* @param string $fqcn The full qualified class name of the entity
*
* @return array|null The full entity configuration
*/
public function getEntityConfigByClass($fqcn)
{
$backendConfig = $this->getBackendConfig();
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
if ($entityConfig['class'] === $fqcn) {
return $entityConfig;
}
}
}
/**
* Returns the full action configuration for the given 'entity' and 'view'.
*
* @param string $entityName
* @param string $view
* @param string $action
*
* @return array
*/
public function getActionConfig($entityName, $view, $action)
{
try {
$entityConfig = $this->getEntityConfig($entityName);
} catch (\Exception $e) {
$entityConfig = array();
}
return isset($entityConfig[$view]['actions'][$action]) ? $entityConfig[$view]['actions'][$action] : array();
}
/**
* Checks whether the given 'action' is enabled for the given 'entity' and
* 'view'.
*
* @param string $entityName
* @param string $view
* @param string $action
*
* @return bool
*/
public function isActionEnabled($entityName, $view, $action)
{
$entityConfig = $this->getEntityConfig($entityName);
return !in_array($action, $entityConfig['disabled_actions']) && array_key_exists($action, $entityConfig[$view]['actions']);
}
/**
* It processes the original backend configuration defined by the end-users
* to generate the full configuration used by the application. Depending on
* the environment, the configuration is processed every time or once and
* the result cached for later reuse.
*
* @return array
*/
private function processConfig()
{
$originalBackendConfig = $this->container->getParameter('easyadmin.config');
if (true === $this->container->getParameter('kernel.debug')) {
return $this->doProcessConfig($originalBackendConfig);
}
$cache = $this->container->get('easyadmin.cache.manager');
if ($cache->hasItem('processed_config')) {
return $cache->getItem('processed_config');
}
$backendConfig = $this->doProcessConfig($originalBackendConfig);
$cache->save('processed_config', $backendConfig);
return $backendConfig;
}
/**
* It processes the given backend configuration to generate the fully
* processed configuration used in the application.
*
* @param string $backendConfig
*
* @return array
*/
private function doProcessConfig($backendConfig)
{
if ($this->container->hasParameter('locale')) {
$locale = $this->container->getParameter('locale');
} else {
$locale = $this->container->getParameter('kernel.default_locale');
}
$configPasses = array(
new NormalizerConfigPass($this->container),
new DesignConfigPass($this->container->get('twig'), $this->container->getParameter('kernel.debug'), $locale),
new MenuConfigPass(),
new ActionConfigPass(),
new MetadataConfigPass($this->container->get('doctrine')),
new PropertyConfigPass(),
new ViewConfigPass(),
new TemplateConfigPass($this->container->getParameter('kernel.root_dir').'/Resources/views'),
new DefaultConfigPass(),
);
foreach ($configPasses as $configPass) {
$backendConfig = $configPass->process($backendConfig);
}
return $backendConfig;
}
}