forked from EasyCorp/EasyAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalizerConfigPass.php
More file actions
204 lines (177 loc) · 7.5 KB
/
NormalizerConfigPass.php
File metadata and controls
204 lines (177 loc) · 7.5 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
<?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;
/**
* Normalizes the different configuration formats available for entities, views,
* actions and properties.
*
* @author Javier Eguiluz <[email protected]>
*/
class NormalizerConfigPass implements ConfigPassInterface
{
public function process(array $backendConfig)
{
$backendConfig = $this->normalizeEntityConfig($backendConfig);
$backendConfig = $this->normalizeFormViewConfig($backendConfig);
$backendConfig = $this->normalizeViewConfig($backendConfig);
$backendConfig = $this->normalizePropertyConfig($backendConfig);
$backendConfig = $this->normalizeActionConfig($backendConfig);
return $backendConfig;
}
/**
* By default the entity name is used as its label (showed in buttons, the
* main menu, etc.) unless the entity config defines the 'label' option:
*
* easy_admin:
* entities:
* User:
* class: AppBundle\Entity\User
* label: 'Clients'
*
* @param array $backendConfig
*
* @return array
*/
private function normalizeEntityConfig(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
if (!isset($entityConfig['label'])) {
$backendConfig['entities'][$entityName]['label'] = $entityName;
}
}
return $backendConfig;
}
/**
* Process the configuration of the 'form' view (if any) to complete the
* configuration of the 'edit' and 'new' views.
*
* @param array $backendConfig [description]
*
* @return array
*/
private function normalizeFormViewConfig(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
if (isset($entityConfig['form'])) {
$entityConfig['new'] = isset($entityConfig['new']) ? array_replace($entityConfig['form'], $entityConfig['new']) : $entityConfig['form'];
$entityConfig['edit'] = isset($entityConfig['edit']) ? array_replace($entityConfig['form'], $entityConfig['edit']) : $entityConfig['form'];
}
$backendConfig['entities'][$entityName] = $entityConfig;
}
return $backendConfig;
}
/**
* Normalizes the view configuration when some of them doesn't define any
* configuration.
*
* @param array $backendConfig
*
* @return array
*/
private function normalizeViewConfig(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
foreach (array('edit', 'list', 'new', 'search', 'show') as $view) {
if (!isset($entityConfig[$view])) {
$entityConfig[$view] = array('fields' => array());
}
if (!isset($entityConfig[$view]['fields'])) {
$entityConfig[$view]['fields'] = array();
}
if (in_array($view, array('edit', 'new')) && !isset($entityConfig[$view]['form_options'])) {
$entityConfig[$view]['form_options'] = array();
}
}
$backendConfig['entities'][$entityName] = $entityConfig;
}
return $backendConfig;
}
/**
* Fields can be defined using two different formats:.
*
* # Config format #1: simple configuration
* easy_admin:
* Client:
* # ...
* list:
* fields: ['id', 'name', 'email']
*
* # Config format #2: extended configuration
* easy_admin:
* Client:
* # ...
* list:
* fields: ['id', 'name', { property: 'email', label: 'Contact' }]
*
* This method processes both formats to produce a common form field configuration
* format used in the rest of the application.
*
* @param array $backendConfig
*
* @return array
*/
private function normalizePropertyConfig(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
foreach (array('edit', 'list', 'new', 'search', 'show') as $view) {
$fields = array();
foreach ($entityConfig[$view]['fields'] as $field) {
if (!is_string($field) && !is_array($field)) {
throw new \RuntimeException(sprintf('The values of the "fields" option for the "%s" view of the "%s" entity can only be strings or arrays.', $view, $entityConfig['class']));
}
if (is_string($field)) {
// Config format #1: field is just a string representing the entity property
$fieldConfig = array('property' => $field);
} else {
// Config format #1: field is an array that defines one or more
// options. Check that the mandatory 'property' option is set
if (!array_key_exists('property', $field)) {
throw new \RuntimeException(sprintf('One of the values of the "fields" option for the "%s" view of the "%s" entity does not define the "property" option.', $view, $entityConfig['class']));
}
$fieldConfig = $field;
}
// for 'image' type fields, if the entity defines an 'image_base_path'
// option, but the field does not, use the value defined by the entity
if (isset($fieldConfig['type']) && 'image' === $fieldConfig['type']) {
if (!isset($fieldConfig['base_path']) && isset($entityConfig['image_base_path'])) {
$fieldConfig['base_path'] = $entityConfig['image_base_path'];
}
}
$fieldName = $fieldConfig['property'];
$fields[$fieldName] = $fieldConfig;
}
$backendConfig['entities'][$entityName][$view]['fields'] = $fields;
}
}
return $backendConfig;
}
private function normalizeActionConfig(array $backendConfig)
{
$views = array('edit', 'list', 'new', 'show');
foreach ($views as $view) {
if (!isset($backendConfig[$view]['actions'])) {
$backendConfig[$view]['actions'] = array();
}
// there is no need to check if the "actions" option for the global
// view is an array because it's done by the Configuration definition
}
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
foreach ($views as $view) {
if (!isset($entityConfig[$view]['actions'])) {
$backendConfig['entities'][$entityName][$view]['actions'] = array();
}
if (!is_array($backendConfig['entities'][$entityName][$view]['actions'])) {
throw new \InvalidArgumentException(sprintf('The "actions" configuration for the "%s" view of the "%s" entity must be an array (a string was provided).', $view, $entityName));
}
}
}
return $backendConfig;
}
}