1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Blast Project package. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
7
|
|
|
* |
8
|
|
|
* This file is licenced under the GNU LGPL v3. |
9
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Blast\CoreBundle\Admin; |
14
|
|
|
|
15
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
16
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
17
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
18
|
|
|
use Sonata\AdminBundle\Mapper\BaseMapper; |
19
|
|
|
use Sonata\AdminBundle\Show\ShowMapper; |
20
|
|
|
use Sonata\AdminBundle\Route\RouteCollection; |
21
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin as SonataAdmin; |
22
|
|
|
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription; |
23
|
|
|
use Blast\CoreBundle\Tools\Reflection\ClassAnalyzer; |
24
|
|
|
use Blast\CoreBundle\Admin\Traits\CollectionsManager; |
25
|
|
|
use Blast\CoreBundle\Admin\Traits\Mapper; |
26
|
|
|
use Blast\CoreBundle\Admin\Traits\Templates; |
27
|
|
|
use Blast\CoreBundle\Admin\Traits\PreEvents; |
28
|
|
|
use Blast\CoreBundle\Admin\Traits\ManyToManyManager; |
29
|
|
|
use Blast\CoreBundle\Admin\Traits\Actions; |
30
|
|
|
use Blast\CoreBundle\Admin\Traits\ListActions; |
31
|
|
|
use Blast\CoreBundle\CodeGenerator\CodeGeneratorRegistry; |
32
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
33
|
|
|
|
34
|
|
|
abstract class CoreAdmin extends SonataAdmin implements \JsonSerializable |
35
|
|
|
{ |
36
|
|
|
use CollectionsManager, |
37
|
|
|
ManyToManyManager, |
38
|
|
|
Mapper, |
39
|
|
|
Templates, |
40
|
|
|
PreEvents, |
41
|
|
|
Actions, |
42
|
|
|
ListActions |
43
|
|
|
; |
44
|
|
|
|
45
|
|
|
protected $extraTemplates = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getLabelTranslatorStrategy() |
51
|
|
|
{ |
52
|
|
|
$this->labelTranslatorStrategy->setAdmin($this); |
|
|
|
|
53
|
|
|
|
54
|
|
|
return $this->labelTranslatorStrategy; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Configure routes for list actions. |
59
|
|
|
* |
60
|
|
|
* @param RouteCollection $collection |
61
|
|
|
*/ |
62
|
|
|
protected function configureRoutes(RouteCollection $collection) |
63
|
|
|
{ |
64
|
|
|
parent::configureRoutes($collection); |
65
|
|
|
$collection->add('duplicate', $this->getRouterIdParameter() . '/duplicate'); |
66
|
|
|
$collection->add('generateEntityCode'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function getBaseRouteName() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$configuredBaseRoute = $this->getBaseRouteMapping(); |
72
|
|
|
|
73
|
|
|
if (count($configuredBaseRoute) > 0) { |
74
|
|
|
$this->cachedBaseRouteName = null; |
|
|
|
|
75
|
|
|
if (isset($configuredBaseRoute['name']) && $this->baseRouteName === null) { |
76
|
|
|
$this->baseRouteName = $configuredBaseRoute['name']; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return parent::getBaseRouteName(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function getBaseRoutePattern() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$configuredBaseRoute = $this->getBaseRouteMapping(); |
86
|
|
|
|
87
|
|
|
if (count($configuredBaseRoute) > 0) { |
88
|
|
|
$this->cachedBaseRoutePattern = null; |
|
|
|
|
89
|
|
|
if (isset($configuredBaseRoute['pattern']) && $this->baseRoutePattern === null) { |
90
|
|
|
$this->baseRoutePattern = $configuredBaseRoute['pattern']; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return parent::getBaseRoutePattern(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getFormTheme() |
98
|
|
|
{ |
99
|
|
|
return array_merge($this->formTheme, $this->getFormThemeMapping()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param DatagridMapper $mapper |
104
|
|
|
*/ |
105
|
|
|
protected function configureDatagridFilters(DatagridMapper $mapper) |
106
|
|
|
{ |
107
|
|
|
if (!$this->configureMapper($mapper)) { |
108
|
|
|
$this->fallbackConfiguration($mapper, __FUNCTION__); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param ListMapper $mapper |
114
|
|
|
*/ |
115
|
|
|
protected function configureListFields(ListMapper $mapper) |
116
|
|
|
{ |
117
|
|
|
if (!$this->configureMapper($mapper)) { |
118
|
|
|
$this->fallbackConfiguration($mapper, __FUNCTION__); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param FormMapper $mapper |
124
|
|
|
*/ |
125
|
|
|
protected function configureFormFields(FormMapper $mapper) |
126
|
|
|
{ |
127
|
|
|
if (!$this->configureMapper($mapper)) { |
128
|
|
|
$this->fallbackConfiguration($mapper, __FUNCTION__); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param ShowMapper $mapper |
134
|
|
|
*/ |
135
|
|
|
protected function configureShowFields(ShowMapper $mapper) |
136
|
|
|
{ |
137
|
|
|
if (!$this->configureMapper($mapper)) { |
138
|
|
|
$this->fallbackConfiguration($mapper, __FUNCTION__); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param BaseMapper $mapper |
144
|
|
|
*/ |
145
|
|
|
protected function fixShowRoutes(BaseMapper $mapper) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
foreach (['getShow', 'getList'] as $fct) { |
148
|
|
|
foreach ($this->$fct()->getElements() as $field) { |
149
|
|
|
if ($field instanceof FieldDescription) { |
150
|
|
|
$options = $field->getOptions(); |
151
|
|
|
if ($options['route']['name'] != 'edit') { |
152
|
|
|
continue; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$options['route']['name'] = 'show'; |
156
|
|
|
$field->setOptions($options); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function getCurrentComposition() |
165
|
|
|
{ |
166
|
|
|
// traits of the current Entity |
167
|
|
|
$classes = ClassAnalyzer::getTraits($this->getClass()); |
168
|
|
|
// inheritance of the current Entity |
169
|
|
|
foreach (array_reverse([$this->getClass()] + class_parents($this->getClass())) as $class) { |
170
|
|
|
$classes[] = $class; |
171
|
|
|
} |
172
|
|
|
// inheritance of the current Admin |
173
|
|
|
foreach (array_reverse([$this->getOriginalClass()] + $this->getParentClasses()) as $admin) { |
174
|
|
|
$classes[] = $admin; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $classes; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function fallbackConfiguration(BaseMapper $mapper, $function) |
181
|
|
|
{ |
182
|
|
|
// fallback |
183
|
|
|
$rm = new \ReflectionMethod($this->getParentClass(), $function); |
184
|
|
|
if ($rm->class == $this->getParentClass()) { |
185
|
|
|
$this->configureFields($function, $mapper, $this->getParentClass()); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the level of depth of an array. |
191
|
|
|
* |
192
|
|
|
* @param array $array |
193
|
|
|
* @param int $level : do not use, just used for recursivity |
194
|
|
|
* |
195
|
|
|
* @return int : depth |
196
|
|
|
*/ |
197
|
|
|
private static function arrayDepth($array, $level = 0) |
198
|
|
|
{ |
199
|
|
|
if (!$array) { |
|
|
|
|
200
|
|
|
return $level; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (!is_array($array)) { |
204
|
|
|
return $level; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
++$level; |
208
|
|
|
foreach ($array as $key => $value) { |
209
|
|
|
if (is_array($value)) { |
210
|
|
|
$level = $level < self::arrayDepth($value, $level) ? self::arrayDepth($value, $level) : $level; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $level; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
protected function getOriginalClass() |
218
|
|
|
{ |
219
|
|
|
return get_called_class(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
protected function getParentClasses() |
223
|
|
|
{ |
224
|
|
|
return class_parents($this->getOriginalClass()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
protected function getParentClass() |
228
|
|
|
{ |
229
|
|
|
return get_parent_class($this->getOriginalClass()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
protected function getGrandParentClass() |
233
|
|
|
{ |
234
|
|
|
return get_parent_class(get_parent_class($this->getOriginalClass())); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $view 'list', 'show', 'form', etc |
239
|
|
|
* @param string $template template name |
240
|
|
|
*/ |
241
|
|
|
public function addExtraTemplate($view, $template) |
242
|
|
|
{ |
243
|
|
|
if (empty($this->extraTemplates[$view])) { |
244
|
|
|
$this->extraTemplates[$view] = []; |
245
|
|
|
} |
246
|
|
|
if (!in_array($template, $this->extraTemplates[$view])) { |
247
|
|
|
$this->extraTemplates[$view][] = $template; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $view 'list', 'show', 'form', etc |
253
|
|
|
* |
254
|
|
|
* @return array array of template names |
255
|
|
|
*/ |
256
|
|
|
public function getExtraTemplates($view) |
257
|
|
|
{ |
258
|
|
|
if (empty($this->extraTemplates[$view])) { |
259
|
|
|
$this->extraTemplates[$view] = []; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $this->extraTemplates[$view]; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param string $view 'list', 'show', 'form', etc |
267
|
|
|
* @param array $link link (array keys should be: 'label', 'url', 'class', 'title') |
268
|
|
|
*/ |
269
|
|
|
public function addHelperLink($view, $link) |
270
|
|
|
{ |
271
|
|
|
if (empty($this->helperLinks[$view])) { |
272
|
|
|
$this->helperLinks[$view] = []; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// Do not add links without URL |
276
|
|
|
if (empty($link['url'])) { |
277
|
|
|
return; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// Do not add two links with the same URL |
281
|
|
|
foreach ($this->helperLinks[$view] as $l) { |
282
|
|
|
if ($l['url'] == $link['url']) { |
283
|
|
|
return; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$this->helperLinks[$view][] = $link; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $view 'list', 'show', 'form', etc |
292
|
|
|
* |
293
|
|
|
* @return array array of links (each link is an array with keys 'label', 'url', 'class' and 'title') |
294
|
|
|
*/ |
295
|
|
|
public function getHelperLinks($view) |
296
|
|
|
{ |
297
|
|
|
if (empty($this->helperLinks[$view])) { |
298
|
|
|
$this->helperLinks[$view] = []; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $this->helperLinks[$view]; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Checks if a Bundle is installed. |
306
|
|
|
* |
307
|
|
|
* @param string $bundle Bundle name or class FQN |
308
|
|
|
*/ |
309
|
|
|
public function bundleExists($bundle) |
310
|
|
|
{ |
311
|
|
|
$kernelBundles = $this->getConfigurationPool()->getContainer()->getParameter('kernel.bundles'); |
312
|
|
|
if (array_key_exists($bundle, $kernelBundles)) { |
313
|
|
|
return true; |
314
|
|
|
} |
315
|
|
|
if (in_array($bundle, $kernelBundles)) { |
316
|
|
|
return true; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return false; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Rename a form tab after form fields have been configured. |
324
|
|
|
* |
325
|
|
|
* TODO: groups of the renamed tab are still prefixed with the old tab name |
326
|
|
|
* |
327
|
|
|
* @param type $tabName the name of the tab to be renamed |
328
|
|
|
* @param type $newTabName the new name for the tab |
329
|
|
|
*/ |
330
|
|
View Code Duplication |
public function renameFormTab($tabName, $newTabName, $keepOrder = true) |
|
|
|
|
331
|
|
|
{ |
332
|
|
|
$tabs = $this->getFormTabs(); |
333
|
|
|
|
334
|
|
|
if (!$tabs) { |
335
|
|
|
return; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
if (!isset($tabs[$tabName])) { |
339
|
|
|
throw new \Exception(sprintf('Tab %s does not exist.', $tabName)); |
340
|
|
|
} |
341
|
|
|
if (isset($tabs[$newTabName])) { |
342
|
|
|
return; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
if ($keepOrder) { |
346
|
|
|
$keys = array_keys($tabs); |
347
|
|
|
$keys[array_search($tabName, $keys)] = $newTabName; |
348
|
|
|
$tabs = array_combine($keys, $tabs); |
349
|
|
|
} else { |
350
|
|
|
$tabs[$newTabName] = $tabs[$tabName]; |
351
|
|
|
unset($tabs[$tabName]); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$this->setFormTabs($tabs); |
|
|
|
|
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Rename a show tab after show fields have been configured. |
359
|
|
|
* |
360
|
|
|
* TODO: groups of the renamed tab are still prefixed with the old tab name |
361
|
|
|
* |
362
|
|
|
* @param type $tabName the name of the tab to be renamed |
363
|
|
|
* @param type $newTabName the new name for the tab |
364
|
|
|
*/ |
365
|
|
View Code Duplication |
public function renameShowTab($tabName, $newTabName, $keepOrder = true) |
|
|
|
|
366
|
|
|
{ |
367
|
|
|
$tabs = $this->getShowTabs(); |
368
|
|
|
|
369
|
|
|
if (!$tabs) { |
370
|
|
|
return; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if (!isset($tabs[$tabName])) { |
374
|
|
|
throw new \Exception(sprintf('Tab %s does not exist.', $tabName)); |
375
|
|
|
} |
376
|
|
|
if (isset($tabs[$newTabName])) { |
377
|
|
|
return; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
if ($keepOrder) { |
381
|
|
|
$keys = array_keys($tabs); |
382
|
|
|
$keys[array_search($tabName, $keys)] = $newTabName; |
383
|
|
|
$tabs = array_combine($keys, $tabs); |
384
|
|
|
} else { |
385
|
|
|
$tabs[$newTabName] = $tabs[$tabName]; |
386
|
|
|
unset($tabs[$tabName]); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
$this->setShowTabs($tabs); |
|
|
|
|
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Rename a form group. |
394
|
|
|
* |
395
|
|
|
* @param string $group the old group name |
396
|
|
|
* @param string $tab the tab the group belongs to |
397
|
|
|
* @param string $newGroupName the new group name |
398
|
|
|
* |
399
|
|
|
* @return self |
400
|
|
|
*/ |
401
|
|
|
public function renameFormGroup($group, $tab, $newGroupName) |
402
|
|
|
{ |
403
|
|
|
$groups = $this->getFormGroups(); |
404
|
|
|
|
405
|
|
|
// When the default tab is used, the tabname is not prepended to the index in the group array |
406
|
|
|
if ($tab !== 'default') { |
407
|
|
|
$group = $tab . '.' . $group; |
408
|
|
|
} |
409
|
|
|
$newGroup = ($tab !== 'default') ? $tab . '.' . $newGroupName : $newGroupName; |
410
|
|
|
|
411
|
|
|
if (isset($groups[$newGroup])) { |
412
|
|
|
throw new \Exception(sprintf('%s form group already exists.', $newGroup)); |
413
|
|
|
} |
414
|
|
|
if (!array_key_exists($group, $groups)) { |
415
|
|
|
throw new \Exception(sprintf('form group « %s » doesn\'t exist.', $group)); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
$groups[$newGroup] = $groups[$group]; |
419
|
|
|
$groups[$newGroup]['name'] = $newGroupName; |
420
|
|
|
unset($groups[$group]); |
421
|
|
|
|
422
|
|
|
$tabs = $this->getFormTabs(); |
423
|
|
|
$key = array_search($group, $tabs[$tab]['groups']); |
424
|
|
|
|
425
|
|
|
if (false !== $key) { |
426
|
|
|
$tabs[$tab]['groups'][$key] = $newGroup; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$this->setFormTabs($tabs); |
|
|
|
|
430
|
|
|
$this->setFormGroups($groups); |
|
|
|
|
431
|
|
|
|
432
|
|
|
return $this; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Removes tab in current form Mapper. |
437
|
|
|
* |
438
|
|
|
* @param string|array $tabNames name or array of names of tabs to be removed |
439
|
|
|
* @param FormMapper $mapper Sonata Admin form mapper |
440
|
|
|
*/ |
441
|
|
|
public function removeTab($tabNames, $mapper) |
442
|
|
|
{ |
443
|
|
|
$currentTabs = $this->getFormTabs(); |
444
|
|
|
foreach ($currentTabs as $k => $item) { |
|
|
|
|
445
|
|
|
if (is_array($tabNames) && in_array($item['name'], $tabNames) || !is_array($tabNames) && $item['name'] === $tabNames) { |
446
|
|
|
foreach ($item['groups'] as $groupName) { |
447
|
|
|
$this->removeAllFieldsFromFormGroup($groupName, $mapper); |
448
|
|
|
} |
449
|
|
|
unset($currentTabs[$k]); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
$this->setFormTabs($currentTabs); |
|
|
|
|
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Removes all fields from form groups and remove them from mapper. |
457
|
|
|
* |
458
|
|
|
* @param string $groupName Name of the group to remove |
459
|
|
|
* @param FormMapper $mapper Sonata Admin form mapper |
460
|
|
|
*/ |
461
|
|
|
public function removeAllFieldsFromFormGroup($groupName, $mapper) |
462
|
|
|
{ |
463
|
|
|
$formGroups = $this->getFormGroups(); |
464
|
|
|
foreach ($formGroups as $name => $formGroup) { |
|
|
|
|
465
|
|
|
if ($name === $groupName) { |
466
|
|
|
foreach ($formGroups[$name]['fields'] as $key => $field) { |
467
|
|
|
$mapper->remove($key); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
public function jsonSerialize() |
474
|
|
|
{ |
475
|
|
|
$propertiesToShow = [ |
476
|
|
|
'baseRouteName', |
477
|
|
|
'baseRoutePattern', |
478
|
|
|
'extraTemplates', |
479
|
|
|
'listFieldDescriptions', |
480
|
|
|
'showFieldDescriptions', |
481
|
|
|
'formFieldDescriptions', |
482
|
|
|
'filterFieldDescriptions', |
483
|
|
|
'maxPerPage', |
484
|
|
|
'maxPageLinks', |
485
|
|
|
'classnameLabel', |
486
|
|
|
'translationDomain', |
487
|
|
|
'formOptions', |
488
|
|
|
'datagridValues', |
489
|
|
|
'perPageOptions', |
490
|
|
|
'pagerType', |
491
|
|
|
'code', |
492
|
|
|
'label', |
493
|
|
|
'routes', |
494
|
|
|
'subject', |
495
|
|
|
'children', |
496
|
|
|
'parent', |
497
|
|
|
'baseCodeRoute', |
498
|
|
|
'uniqid', |
499
|
|
|
'extensions', |
500
|
|
|
'class', |
501
|
|
|
'subClasses', |
502
|
|
|
'list', |
503
|
|
|
'show', |
504
|
|
|
'form', |
505
|
|
|
'filter', |
506
|
|
|
'formGroups', |
507
|
|
|
'formTabs', |
508
|
|
|
'showGroups', |
509
|
|
|
'showTabs', |
510
|
|
|
'managedCollections', |
511
|
|
|
'helperLinks', |
512
|
|
|
'titles', |
513
|
|
|
]; |
514
|
|
|
|
515
|
|
|
$properties = []; |
516
|
|
|
foreach ($this as $key => $value) { |
|
|
|
|
517
|
|
|
if (in_array($key, $propertiesToShow)) { |
518
|
|
|
$properties[$key] = $value; |
519
|
|
|
} |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
return $properties; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* {@inheritdoc} |
527
|
|
|
*/ |
528
|
|
|
public function prePersist($object) |
529
|
|
|
{ |
530
|
|
|
parent::prePersist($object); |
531
|
|
|
|
532
|
|
|
$hasCodeGenerator = CodeGeneratorRegistry::hasGeneratorForClass(get_class($object)); |
533
|
|
|
if ($hasCodeGenerator) { |
534
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
535
|
|
|
foreach (CodeGeneratorRegistry::getCodeGenerators(get_class($object)) as $name => $generator) { |
536
|
|
|
$accessor->setValue($object, $name, $generator->generate($object)); |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
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: