|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Admingenerator\GeneratorBundle\Builder; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Cedric LOMBARDOT |
|
7
|
|
|
* @author Piotr Gołębiewski <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
10
|
|
|
use TwigGenerator\Builder\Generator as TwigGeneratorGenerator; |
|
11
|
|
|
use TwigGenerator\Builder\BuilderInterface; |
|
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
13
|
|
|
|
|
14
|
|
|
class Generator extends TwigGeneratorGenerator |
|
15
|
|
|
{ |
|
16
|
|
|
const TEMP_DIR_PREFIX = 'Admingenerator'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array $yaml The yaml array. |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $yaml; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string $baseController The base controller. |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $baseController; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string $columnClass The column class. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $columnClass = 'Admingenerator\GeneratorBundle\Generator\Column'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string $baseAdminTemplate The base admin template. |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $baseAdminTemplate = 'AdmingeneratoroldThemeBundle::base.html.twig'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string $baseGeneratorName The base generator name. |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $baseGeneratorName; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array $bundleConfig Generator bundle config. |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $bundleConfig; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $router; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Init a new generator and automatically define the base of tempDir |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $cacheDir |
|
57
|
|
|
* @param Filepath $yaml |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct($cacheDir, $yaml) |
|
60
|
|
|
{ |
|
61
|
|
|
parent::__construct($cacheDir); |
|
62
|
|
|
$this->setYamlConfig(Yaml::parse(file_get_contents($yaml))); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string The base admin template. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getBaseAdminTemplate() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->baseAdminTemplate; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $baseAdminTemplate The base admin template. |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setBaseAdminTemplate($baseAdminTemplate) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->baseAdminTemplate = $baseAdminTemplate; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Add a builder |
|
84
|
|
|
* @param BuilderInterface $builder |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function addBuilder(BuilderInterface $builder) |
|
89
|
|
|
{ |
|
90
|
|
|
parent::addBuilder($builder); |
|
91
|
|
|
|
|
92
|
|
|
$params = $this->getFromYaml('params', array()); |
|
93
|
|
|
$params = $this->applyActionsBuilderDefaults($params); |
|
94
|
|
|
|
|
95
|
|
|
$params = $this->mergeParameters( |
|
96
|
|
|
$params, |
|
97
|
|
|
$this->getFromYaml(sprintf('builders.%s.params', $builder->getYamlKey()), array()) |
|
|
|
|
|
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$builder->setVariables($params); |
|
101
|
|
|
$builder->setColumnClass($this->getColumnClass()); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Merge parameters from global definition with builder definition |
|
106
|
|
|
* Fields and actions have special behaviors: |
|
107
|
|
|
* - fields are merged and all global fields are still available |
|
108
|
|
|
* from a builder |
|
109
|
|
|
* - actions depend of builder. List of available actions come |
|
110
|
|
|
* from builder, configuration is a merge between builder configuration |
|
111
|
|
|
* and global configuration |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $global |
|
114
|
|
|
* @param array $builder |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function mergeParameters(array $global, array $builder) |
|
119
|
|
|
{ |
|
120
|
|
|
foreach ($global as $param => &$value) { |
|
121
|
|
|
if (!array_key_exists($param, $builder)) { |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (!in_array($param, array('fields', 'actions', 'object_actions', 'batch_actions'))) { |
|
126
|
|
|
if (is_array($value)) { |
|
127
|
|
|
$value = $this->recursiveReplace($value, $builder[$param]); |
|
128
|
|
|
} else { |
|
129
|
|
|
$value = $builder[$param]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Grab builder configuration only if defined |
|
136
|
|
|
if (is_null($builder[$param])) { |
|
137
|
|
|
continue; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$configurations = array(); |
|
141
|
|
|
foreach ($builder[$param] as $name => $configuration) { |
|
142
|
|
|
if (!is_array($configuration) && !is_null($configuration)) { |
|
143
|
|
|
throw new \InvalidArgumentException( |
|
144
|
|
|
sprintf('Invalid %s "%s" builder definition for %s', $param, $name, $this->getFromYaml('params.model')) |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (!is_null($value) && array_key_exists($name, $value)) { |
|
149
|
|
|
$configurations[$name] = $configuration |
|
150
|
|
|
? $this->mergeConfiguration($value[$name], $configuration) // Override definition |
|
151
|
|
|
: $value[$name]; // Configuration is null => use global definition |
|
152
|
|
|
} else { |
|
153
|
|
|
// New definition (new field, new action) from builder |
|
154
|
|
|
$configurations[$name] = $configuration; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if (in_array($param, array('actions', 'object_actions', 'batch_actions'))) { |
|
159
|
|
|
// Actions list comes from builder |
|
160
|
|
|
$value = $configurations; |
|
161
|
|
|
} else { |
|
162
|
|
|
// All fields are still available in a builder |
|
163
|
|
|
$value = array_merge($value ?:array(), $configurations); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
// If builder doesn't have actions/object_actions/batch_actions remove it from merge. |
|
168
|
|
|
$global['actions'] = array_key_exists('actions', $builder) ? $global['actions'] : array(); |
|
169
|
|
|
$global['object_actions'] = array_key_exists('object_actions', $builder) ? $global['object_actions'] : array(); |
|
170
|
|
|
$global['batch_actions'] = array_key_exists('batch_actions', $builder) ? $global['batch_actions'] : array(); |
|
171
|
|
|
|
|
172
|
|
|
return array_merge($global, array_diff_key($builder, $global)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Merge configuration on a single level |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $global |
|
179
|
|
|
* @param array $builder |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function mergeConfiguration(array $global, array $builder) |
|
184
|
|
|
{ |
|
185
|
|
|
foreach ($global as $name => &$value) { |
|
186
|
|
|
if (!array_key_exists($name, $builder) || is_null($builder[$name])) { |
|
187
|
|
|
continue; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if (!is_array($value)) { |
|
191
|
|
|
$value = $builder[$name]; |
|
192
|
|
|
|
|
193
|
|
|
continue; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if (!is_array($builder[$name])) { |
|
197
|
|
|
throw new \InvalidArgumentException('Invalid generator'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$value = array_replace($value, $builder[$name]); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return array_merge($global, array_diff_key($builder, $global)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Recursively replaces Base array values with Replacement array values |
|
208
|
|
|
* while keeping indexes of Replacement array |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $base Base array |
|
211
|
|
|
* @param array $replacement Replacement array |
|
212
|
|
|
* |
|
213
|
|
|
* @return array |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function recursiveReplace($base, $replacement) |
|
216
|
|
|
{ |
|
217
|
|
|
$replace_values_recursive = function (array $array, array $order) use (&$replace_values_recursive) { |
|
218
|
|
|
$array = array_replace($order, array_replace($array, $order)); |
|
219
|
|
|
|
|
220
|
|
|
foreach ($array as $key => &$value) { |
|
221
|
|
|
if (is_array($value)) { |
|
222
|
|
|
$value = (array_key_exists($key, $order) && is_array($order[$key])) |
|
223
|
|
|
? $replace_values_recursive($value, $order[$key]) |
|
224
|
|
|
: $value |
|
225
|
|
|
; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $array; |
|
230
|
|
|
}; |
|
231
|
|
|
|
|
232
|
|
|
return $replace_values_recursive($base, $replacement); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Inject default batch and object actions settings |
|
237
|
|
|
* |
|
238
|
|
|
* @return array |
|
239
|
|
|
*/ |
|
240
|
|
|
protected function applyActionsBuilderDefaults(array $params) |
|
241
|
|
|
{ |
|
242
|
|
|
if (!array_key_exists('namespace_prefix', $params) || !array_key_exists('bundle_name', $params)) { |
|
243
|
|
|
return $params; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$routeBase = $params['namespace_prefix'].'_'.$params['bundle_name'].'_'.$this->getBaseGeneratorName(); |
|
247
|
|
|
|
|
248
|
|
|
if (array_key_exists('object_actions', $params) && is_array($params['object_actions'])) { |
|
249
|
|
|
foreach ($params['object_actions'] as $name => $config) { |
|
250
|
|
|
$baseKey = 'object_actions.'.$name; |
|
251
|
|
|
|
|
252
|
|
|
if (is_array($config) && array_key_exists('route', $config) && $config['route'] === 'inject_object_defaults') { |
|
253
|
|
|
$customized = $params['object_actions'][$name]; |
|
254
|
|
|
unset($customized['route']); |
|
255
|
|
|
|
|
256
|
|
|
$params['object_actions'][$name] = $this->recursiveReplace(array( |
|
257
|
|
|
'route' => $routeBase.'_object', |
|
258
|
|
|
'label' => $baseKey.'.label', |
|
259
|
|
|
'csrfProtected' => true, |
|
260
|
|
|
'params' => array( |
|
261
|
|
|
'pk' => '{{ '.$this->getBaseGeneratorName().'.id }}', |
|
262
|
|
|
'action' => $name, |
|
263
|
|
|
), |
|
264
|
|
|
'options' => array( |
|
265
|
|
|
'title' => $baseKey.'.title', |
|
266
|
|
|
'success' => $baseKey.'.success', |
|
267
|
|
|
'error' => $baseKey.'.error', |
|
268
|
|
|
), |
|
269
|
|
|
), $customized); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
if (array_key_exists('batch_actions', $params) && is_array($params['batch_actions'])) { |
|
275
|
|
|
foreach ($params['batch_actions'] as $name => $config) { |
|
276
|
|
|
$baseKey = 'batch_actions.'.$name; |
|
277
|
|
|
|
|
278
|
|
|
if (is_array($config) && array_key_exists('route', $config) && $config['route'] === 'inject_batch_defaults') { |
|
279
|
|
|
$params['batch_actions'][$name] = $this->recursiveReplace($params['batch_actions'][$name], array( |
|
280
|
|
|
'route' => $routeBase.'_batch', |
|
281
|
|
|
'label' => $baseKey.'.label', |
|
282
|
|
|
'csrfProtected' => true, |
|
283
|
|
|
'params' => array( |
|
284
|
|
|
'action' => $name, |
|
285
|
|
|
), |
|
286
|
|
|
'options' => array( |
|
287
|
|
|
'title' => $baseKey.'.title', |
|
288
|
|
|
'success' => $baseKey.'.success', |
|
289
|
|
|
'error' => $baseKey.'.error', |
|
290
|
|
|
'notfound' => $baseKey.'.notfound', |
|
291
|
|
|
), |
|
292
|
|
|
)); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return $params; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @param string $columnClass |
|
302
|
|
|
* @return void |
|
303
|
|
|
*/ |
|
304
|
|
|
public function setColumnClass($columnClass) |
|
305
|
|
|
{ |
|
306
|
|
|
$this->columnClass = $columnClass; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* @return string The column class. |
|
311
|
|
|
*/ |
|
312
|
|
|
protected function getColumnClass() |
|
313
|
|
|
{ |
|
314
|
|
|
return $this->columnClass; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Set the yaml to pass all the vars to the builders |
|
319
|
|
|
* |
|
320
|
|
|
* @param array $yaml |
|
321
|
|
|
* @return void |
|
322
|
|
|
*/ |
|
323
|
|
|
protected function setYamlConfig(array $yaml) |
|
324
|
|
|
{ |
|
325
|
|
|
$this->yaml = array_replace_recursive( |
|
326
|
|
|
Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/default.yml')), |
|
327
|
|
|
$yaml |
|
328
|
|
|
); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @param string $yamlPath Path string with point for levels. |
|
333
|
|
|
* @param mixed $default Value to default to, path key not found. |
|
334
|
|
|
* @return mixed |
|
335
|
|
|
*/ |
|
336
|
|
|
public function getFromYaml($yamlPath, $default = null) |
|
337
|
|
|
{ |
|
338
|
|
|
return $this->getFromArray( |
|
339
|
|
|
$this->yaml, |
|
340
|
|
|
$yamlPath, |
|
341
|
|
|
$default |
|
342
|
|
|
); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @param object $fieldGuesser The fieldguesser. |
|
347
|
|
|
* @return void |
|
348
|
|
|
*/ |
|
349
|
|
|
public function setFieldGuesser($fieldGuesser) |
|
350
|
|
|
{ |
|
351
|
|
|
$this->fieldGuesser = $fieldGuesser; |
|
|
|
|
|
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* @return object The fieldguesser. |
|
356
|
|
|
*/ |
|
357
|
|
|
public function getFieldGuesser() |
|
358
|
|
|
{ |
|
359
|
|
|
return $this->fieldGuesser; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* @param string $baseController |
|
364
|
|
|
*/ |
|
365
|
|
|
public function setBaseController($baseController) |
|
366
|
|
|
{ |
|
367
|
|
|
$this->baseController = $baseController; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @return string Base controller. |
|
372
|
|
|
*/ |
|
373
|
|
|
public function getBaseController() |
|
374
|
|
|
{ |
|
375
|
|
|
return $this->baseController; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* @param string $baseGeneratorName |
|
380
|
|
|
*/ |
|
381
|
|
|
public function setBaseGeneratorName($baseGeneratorName) |
|
382
|
|
|
{ |
|
383
|
|
|
$this->baseGeneratorName = $baseGeneratorName; |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* @return string Base generator name. |
|
388
|
|
|
*/ |
|
389
|
|
|
public function getBaseGeneratorName() |
|
390
|
|
|
{ |
|
391
|
|
|
return $this->baseGeneratorName; |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* @return string Generated controller directory. |
|
396
|
|
|
*/ |
|
397
|
|
|
public function getGeneratedControllerFolder() |
|
398
|
|
|
{ |
|
399
|
|
|
return 'Base'.$this->baseGeneratorName.'Controller'; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $router |
|
404
|
|
|
* @return void |
|
405
|
|
|
*/ |
|
406
|
|
|
public function setRouter(RouterInterface $router) |
|
407
|
|
|
{ |
|
408
|
|
|
$this->router = $router; |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* @return \Symfony\Component\Routing\RouterInterface $router |
|
413
|
|
|
*/ |
|
414
|
|
|
public function getRouter() |
|
415
|
|
|
{ |
|
416
|
|
|
return $this->router; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* @param array $bundleConfig |
|
421
|
|
|
*/ |
|
422
|
|
|
public function setBundleConfig(array $bundleConfig) |
|
423
|
|
|
{ |
|
424
|
|
|
$this->bundleConfig = $bundleConfig; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* @return array |
|
429
|
|
|
*/ |
|
430
|
|
|
public function getBundleConfig() |
|
431
|
|
|
{ |
|
432
|
|
|
return $this->bundleConfig; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* @param string $configPath Path string with point for levels. |
|
437
|
|
|
* @param mixed $default Value to default to, path key not found. |
|
438
|
|
|
* @return mixed |
|
439
|
|
|
*/ |
|
440
|
|
|
public function getFromBundleConfig($configPath, $default = null) |
|
441
|
|
|
{ |
|
442
|
|
|
return $this->getFromArray( |
|
443
|
|
|
$this->bundleConfig, |
|
444
|
|
|
$configPath, |
|
445
|
|
|
$default |
|
446
|
|
|
); |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* @param array $array The array to traverse. |
|
451
|
|
|
* @param string $path Path string with point for levels. |
|
452
|
|
|
* @param mixed $default Value to default to, path key not found. |
|
453
|
|
|
* @return mixed |
|
454
|
|
|
*/ |
|
455
|
|
View Code Duplication |
protected function getFromArray($array, $path, $default = null) |
|
|
|
|
|
|
456
|
|
|
{ |
|
457
|
|
|
$search_in = $array; |
|
458
|
|
|
$path = explode('.', $path); |
|
459
|
|
|
foreach ($path as $key) { |
|
460
|
|
|
if (!isset($search_in[$key])) { |
|
461
|
|
|
return $default; |
|
462
|
|
|
} |
|
463
|
|
|
$search_in = $search_in[$key]; |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
return $search_in; |
|
467
|
|
|
} |
|
468
|
|
|
} |
|
469
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: