1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
7
|
|
|
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
9
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
10
|
|
|
|
11
|
|
|
abstract class AbstractResourceConfiguration implements ConfigurationInterface |
12
|
|
|
{ |
13
|
|
|
const DEFAULT_KEY = 'default'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $resources |
17
|
|
|
* |
18
|
|
|
* @return ArrayNodeDefinition |
19
|
|
|
*/ |
20
|
|
|
protected function createResourcesSection(array $resources = array()) |
21
|
|
|
{ |
22
|
|
|
$builder = new TreeBuilder(); |
23
|
|
|
$node = $builder->root('resources'); |
24
|
|
|
$node->addDefaultsIfNotSet(); |
25
|
|
|
$resourceNodes = $node->children(); |
26
|
|
|
|
27
|
|
|
foreach ($resources as $resource => $defaults) { |
28
|
|
|
$resourceNode = $resourceNodes |
29
|
|
|
->arrayNode($resource) |
30
|
|
|
->addDefaultsIfNotSet() |
31
|
|
|
; |
32
|
|
|
|
33
|
|
|
$this->addClassesSection($resourceNode, $defaults['classes']); |
34
|
|
|
|
35
|
|
|
if (!isset($defaults['validation_groups'])) { |
36
|
|
|
$defaults['validation_groups'] = array( |
37
|
|
|
'default' => array() |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->addValidationGroupsSection($resourceNode, $defaults['validation_groups']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $node; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ArrayNodeDefinition $node |
49
|
|
|
* @param array $defaults |
50
|
|
|
* |
51
|
|
|
* @return ArrayNodeDefinition |
52
|
|
|
*/ |
53
|
|
|
protected function addClassesSection(ArrayNodeDefinition $node, array $defaults = array()) |
54
|
|
|
{ |
55
|
|
|
$node |
56
|
|
|
->children() |
57
|
|
|
->arrayNode('classes') |
58
|
|
|
->addDefaultsIfNotSet() |
59
|
|
|
->children() |
60
|
|
|
->scalarNode('model') |
61
|
|
|
->cannotBeEmpty() |
62
|
|
|
->defaultValue(isset($defaults['model']) ? $defaults['model'] : null) |
63
|
|
|
->end() |
64
|
|
|
|
65
|
|
|
->scalarNode('factory') |
66
|
|
|
->cannotBeEmpty() |
67
|
|
|
->defaultValue(isset($defaults['factory']) ? $defaults['factory'] : 'DoS\ResourceBundle\Factory\Factory') |
68
|
|
|
->end() |
69
|
|
|
|
70
|
|
|
->scalarNode('controller') |
71
|
|
|
->defaultValue(isset($defaults['controller']) ? $defaults['controller'] : 'DoS\ResourceBundle\Controller\ResourceController') |
72
|
|
|
->end() |
73
|
|
|
|
74
|
|
|
->scalarNode('repository') |
75
|
|
|
->cannotBeEmpty() |
76
|
|
|
->defaultValue(isset($defaults['repository']) ? $defaults['repository'] : 'DoS\ResourceBundle\Doctrine\ORM\EntityRepository') |
77
|
|
|
->end() |
78
|
|
|
|
79
|
|
|
->append($this->createInterfaceNode(isset($defaults['interface']) ? $defaults['interface'] : null)) |
80
|
|
|
->append($this->createFormsNode(isset($defaults['form']) ? $defaults['form'] : null)) |
81
|
|
|
->end() |
82
|
|
|
->end() |
83
|
|
|
->end() |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
return $node; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $default |
91
|
|
|
* |
92
|
|
|
* @return ScalarNodeDefinition |
93
|
|
|
*/ |
94
|
|
|
protected function createDriverNode($default = null) |
95
|
|
|
{ |
96
|
|
|
$builder = new TreeBuilder(); |
97
|
|
|
$node = $builder->root('driver', 'enum'); |
98
|
|
|
|
99
|
|
|
if ($default) { |
|
|
|
|
100
|
|
|
$node->defaultValue($default); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$node |
104
|
|
|
->values(array( |
105
|
|
|
SyliusResourceBundle::DRIVER_DOCTRINE_ORM, |
106
|
|
|
SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM, |
107
|
|
|
SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM, |
108
|
|
|
)) |
109
|
|
|
->cannotBeEmpty() |
110
|
|
|
->info(sprintf( |
111
|
|
|
'Database driver (%s, %s or %s)', |
112
|
|
|
SyliusResourceBundle::DRIVER_DOCTRINE_ORM, |
113
|
|
|
SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM, |
114
|
|
|
SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM |
115
|
|
|
)) |
116
|
|
|
->end() |
117
|
|
|
; |
118
|
|
|
|
119
|
|
|
return $node; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $default |
124
|
|
|
* |
125
|
|
|
* @return ScalarNodeDefinition |
126
|
|
|
*/ |
127
|
|
|
protected function createInterfaceNode($default = null) |
128
|
|
|
{ |
129
|
|
|
$builder = new TreeBuilder(); |
130
|
|
|
$node = $builder->root('interface', 'scalar'); |
131
|
|
|
|
132
|
|
|
if ($default) { |
|
|
|
|
133
|
|
|
$node->defaultValue($default); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$node |
137
|
|
|
->cannotBeEmpty() |
138
|
|
|
->info('Name of model interface') |
139
|
|
|
->end() |
140
|
|
|
; |
141
|
|
|
|
142
|
|
|
return $node; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param ArrayNodeDefinition $node |
147
|
|
|
* @param array $validationGroups |
148
|
|
|
*/ |
149
|
|
|
protected function addValidationGroupsSection(ArrayNodeDefinition $node, array $validationGroups = array()) |
150
|
|
|
{ |
151
|
|
|
$child = $node |
152
|
|
|
->children() |
153
|
|
|
->arrayNode('validation_groups') |
154
|
|
|
->addDefaultsIfNotSet() |
155
|
|
|
->children() |
156
|
|
|
; |
157
|
|
|
|
158
|
|
|
foreach ($validationGroups as $name => $groups) { |
159
|
|
|
$child |
160
|
|
|
->arrayNode($name) |
161
|
|
|
->prototype('scalar')->end() |
162
|
|
|
->defaultValue($groups) |
163
|
|
|
->end(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$child |
167
|
|
|
->end() |
168
|
|
|
->end() |
169
|
|
|
->end() |
170
|
|
|
; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array|string $classes |
175
|
|
|
* |
176
|
|
|
* @return ArrayNodeDefinition |
177
|
|
|
*/ |
178
|
|
|
protected function createFormsNode($classes) |
179
|
|
|
{ |
180
|
|
|
$builder = new TreeBuilder(); |
181
|
|
|
$node = $builder->root('form'); |
182
|
|
|
|
183
|
|
|
if (is_string($classes)) { |
184
|
|
|
$classes = array(self::DEFAULT_KEY => $classes); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!isset($classes['choice'])) { |
188
|
|
|
$classes['choice'] = 'DoS\ResourceBundle\Form\Type\ResourceChoiceType'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$node |
|
|
|
|
192
|
|
|
->defaultValue($classes) |
193
|
|
|
->useAttributeAsKey('name') |
194
|
|
|
->prototype('scalar') |
195
|
|
|
->end() |
196
|
|
|
->beforeNormalization() |
197
|
|
|
->ifString() |
198
|
|
|
->then(function ($v) { |
199
|
|
|
return array( |
200
|
|
|
AbstractResourceConfiguration::DEFAULT_KEY => $v, |
201
|
|
|
); |
202
|
|
|
}) |
203
|
|
|
->end() |
204
|
|
|
; |
205
|
|
|
|
206
|
|
|
return $node; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
protected function setDefaults(ArrayNodeDefinition $node, array $configs = array()) |
210
|
|
|
{ |
211
|
|
|
$configs = array_replace_recursive(array( |
212
|
|
|
'driver' => SyliusResourceBundle::DRIVER_DOCTRINE_ORM, |
213
|
|
|
'resources' => array(), |
214
|
|
|
), $configs); |
215
|
|
|
|
216
|
|
|
$node->append($this->createDriverNode($configs['driver'])); |
217
|
|
|
$node->append($this->createResourcesSection($configs['resources'])); |
218
|
|
|
|
219
|
|
|
if (isset($configs['validation_groups'])) { |
220
|
|
|
$this->addValidationGroupsSection($node, $configs['validation_groups']); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: