-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AddDependencyCallsCompilerPass.php
500 lines (414 loc) · 20.5 KB
/
AddDependencyCallsCompilerPass.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\DependencyInjection\Compiler;
use Doctrine\Inflector\InflectorFactory;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Datagrid\Pager;
use Sonata\AdminBundle\DependencyInjection\Admin\TaggedAdminInterface;
use Sonata\AdminBundle\Templating\TemplateRegistry;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
* Add all dependencies to the Admin class, this avoid to write too many lines
* in the configuration files.
*
* @final since sonata-project/admin-bundle 3.52
*
* NEXT_MAJOR: Remove the "since" part of the internal annotation.
*
* @internal since sonata-project/admin-bundle version 4.0
*
* @author Thomas Rabaix <[email protected]>
*/
class AddDependencyCallsCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has('sonata.admin.pool')) {
return;
}
// check if translator service exist
if (!$container->has('translator')) {
throw new \RuntimeException('The "translator" service is not yet enabled.
It\'s required by SonataAdmin to display all labels properly.
To learn how to enable the translator service please visit:
http://symfony.com/doc/current/translation.html#configuration
');
}
$parameterBag = $container->getParameterBag();
$groupDefaults = $admins = $classes = [];
$pool = $container->getDefinition('sonata.admin.pool');
$defaultController = $container->getParameter('sonata.admin.configuration.default_controller');
$defaultValues = [
'group' => $container->getParameter('sonata.admin.configuration.default_group'),
'label_catalogue' => $container->getParameter('sonata.admin.configuration.default_label_catalogue'),
'icon' => $container->getParameter('sonata.admin.configuration.default_icon'),
];
foreach ($container->findTaggedServiceIds(TaggedAdminInterface::ADMIN_TAG) as $id => $tags) {
foreach ($tags as $attributes) {
$definition = $container->getDefinition($id);
$parentDefinition = null;
// Temporary fix until we can support service locators
$definition->setPublic(true);
if ($definition instanceof ChildDefinition) {
$parentDefinition = $container->getDefinition($definition->getParent());
}
$this->replaceDefaultArguments([
0 => $id,
2 => $defaultController,
], $definition, $parentDefinition);
$this->applyConfigurationFromAttribute($definition, $attributes);
$this->applyDefaults($container, $id, $attributes);
$arguments = $parentDefinition ?
array_merge($parentDefinition->getArguments(), $definition->getArguments()) :
$definition->getArguments();
$admins[] = $id;
if (!isset($classes[$arguments[1]])) {
$classes[$arguments[1]] = [];
}
$default = (bool) (isset($attributes['default']) ? $parameterBag->resolveValue($attributes['default']) : false);
if ($default) {
if (isset($classes[$arguments[1]][Pool::DEFAULT_ADMIN_KEY])) {
throw new \RuntimeException(sprintf(
'The class %s has two admins %s and %s with the "default" attribute set to true. Only one is allowed.',
$arguments[1],
$classes[$arguments[1]][Pool::DEFAULT_ADMIN_KEY],
$id
));
}
$classes[$arguments[1]][Pool::DEFAULT_ADMIN_KEY] = $id;
} else {
$classes[$arguments[1]][] = $id;
}
$showInDashboard = (bool) (isset($attributes['show_in_dashboard']) ? $parameterBag->resolveValue($attributes['show_in_dashboard']) : true);
if (!$showInDashboard) {
continue;
}
$resolvedGroupName = isset($attributes['group']) ?
$parameterBag->resolveValue($attributes['group']) :
$defaultValues['group'];
\assert(\is_string($resolvedGroupName));
$labelCatalogue = $attributes['label_catalogue'] ?? $defaultValues['label_catalogue'];
$icon = $attributes['icon'] ?? $defaultValues['icon'];
$onTop = $attributes['on_top'] ?? false;
$keepOpen = $attributes['keep_open'] ?? false;
if (!isset($groupDefaults[$resolvedGroupName])) {
$groupDefaults[$resolvedGroupName] = [
'label' => $resolvedGroupName,
'label_catalogue' => $labelCatalogue,
'icon' => $icon,
'roles' => [],
'on_top' => false,
'keep_open' => false,
];
}
$groupDefaults[$resolvedGroupName]['items'][] = [
'admin' => $id,
'label' => !empty($attributes['label']) ? $attributes['label'] : '',
'route' => '',
'route_params' => [],
'route_absolute' => false,
];
if (isset($groupDefaults[$resolvedGroupName]['on_top']) && $groupDefaults[$resolvedGroupName]['on_top']
|| $onTop && (\count($groupDefaults[$resolvedGroupName]['items']) > 1)) {
throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.');
}
$groupDefaults[$resolvedGroupName]['on_top'] = $onTop;
$groupDefaults[$resolvedGroupName]['keep_open'] = $keepOpen;
}
}
$dashboardGroupsSettings = $container->getParameter('sonata.admin.configuration.dashboard_groups');
\assert(\is_array($dashboardGroupsSettings));
if (!empty($dashboardGroupsSettings)) {
$groups = $dashboardGroupsSettings;
foreach ($dashboardGroupsSettings as $groupName => $group) {
$resolvedGroupName = $parameterBag->resolveValue($groupName);
\assert(\is_string($resolvedGroupName));
if (!isset($groupDefaults[$resolvedGroupName])) {
$groupDefaults[$resolvedGroupName] = [
'items' => [],
'label' => $resolvedGroupName,
'label_catalogue' => $defaultValues['label_catalogue'],
'icon' => $defaultValues['icon'],
'roles' => [],
'on_top' => false,
'keep_open' => false,
];
}
if (empty($group['items'])) {
$groups[$resolvedGroupName]['items'] = $groupDefaults[$resolvedGroupName]['items'];
}
if (empty($group['label'])) {
$groups[$resolvedGroupName]['label'] = $groupDefaults[$resolvedGroupName]['label'];
}
if (empty($group['label_catalogue'])) {
$groups[$resolvedGroupName]['label_catalogue'] = $groupDefaults[$resolvedGroupName]['label_catalogue'];
}
if (empty($group['icon'])) {
$groups[$resolvedGroupName]['icon'] = $groupDefaults[$resolvedGroupName]['icon'];
}
if (!empty($group['item_adds'])) {
$groups[$resolvedGroupName]['items'] = array_merge($groups[$resolvedGroupName]['items'], $group['item_adds']);
}
if (empty($group['roles'])) {
$groups[$resolvedGroupName]['roles'] = $groupDefaults[$resolvedGroupName]['roles'];
}
if (isset($groups[$resolvedGroupName]['on_top']) && !empty($group['on_top']) && $group['on_top']
&& (\count($groups[$resolvedGroupName]['items']) > 1)) {
throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.');
}
if (empty($group['on_top'])) {
$groups[$resolvedGroupName]['on_top'] = $groupDefaults[$resolvedGroupName]['on_top'];
}
if (empty($group['keep_open'])) {
$groups[$resolvedGroupName]['keep_open'] = $groupDefaults[$resolvedGroupName]['keep_open'];
}
}
} elseif ($container->getParameter('sonata.admin.configuration.sort_admins')) {
$groups = $groupDefaults;
$elementSort = static function (array &$element): void {
usort(
$element['items'],
static function (array $a, array $b): int {
$a = !empty($a['label']) ? $a['label'] : $a['admin'];
$b = !empty($b['label']) ? $b['label'] : $b['admin'];
return $a <=> $b;
}
);
};
/*
* 1) sort the groups by their index
* 2) sort the elements within each group by label/admin
*/
ksort($groups);
array_walk($groups, $elementSort);
} else {
$groups = $groupDefaults;
}
// NEXT_MAJOR: Remove the following 3 lines
$pool->addMethodCall('setAdminServiceIds', [$admins, 'sonata_deprecation_mute']);
$pool->addMethodCall('setAdminGroups', [$groups, 'sonata_deprecation_mute']);
$pool->addMethodCall('setAdminClasses', [$classes, 'sonata_deprecation_mute']);
$pool->replaceArgument(1, $admins);
$pool->replaceArgument(2, $groups);
$pool->replaceArgument(3, $classes);
}
/**
* NEXT_MAJOR: Change visibility to private.
*
* This method read the attribute keys and configure admin class to use the related dependency.
*/
public function applyConfigurationFromAttribute(Definition $definition, array $attributes)
{
$keys = [
'model_manager',
'data_source',
'field_description_factory',
'form_contractor',
'show_builder',
'list_builder',
'datagrid_builder',
'translator',
'configuration_pool',
'route_generator',
'validator', // NEXT_MAJOR: Remove this line
'security_handler',
'menu_factory',
'route_builder',
'label_translator_strategy',
];
foreach ($keys as $key) {
$method = $this->generateSetterMethodName($key);
if (!isset($attributes[$key]) || $definition->hasMethodCall($method)) {
continue;
}
$definition->addMethodCall($method, [new Reference($attributes[$key])]);
}
}
/**
* NEXT_MAJOR: Change visibility to private.
*
* Apply the default values required by the AdminInterface to the Admin service definition.
*
* @param string $serviceId
*
* @return Definition
*/
public function applyDefaults(ContainerBuilder $container, $serviceId, array $attributes = [])
{
$definition = $container->getDefinition($serviceId);
// NEXT_MAJOR: Remove this line.
$settings = $container->getParameter('sonata.admin.configuration.admin_services');
$definition->setShared(false);
$managerType = $attributes['manager_type'];
// NEXT_MAJOR: Remove this line and uncomment the following
$overwriteAdminConfiguration = $settings[$serviceId] ?? $container->getParameter('sonata.admin.configuration.default_admin_services');
// $overwriteAdminConfiguration = $container->getParameter('sonata.admin.configuration.default_admin_services');
$defaultAddServices = [
'model_manager' => sprintf('sonata.admin.manager.%s', $managerType),
'data_source' => sprintf('sonata.admin.data_source.%s', $managerType),
'field_description_factory' => sprintf('sonata.admin.field_description_factory.%s', $managerType),
'form_contractor' => sprintf('sonata.admin.builder.%s_form', $managerType),
'show_builder' => sprintf('sonata.admin.builder.%s_show', $managerType),
'list_builder' => sprintf('sonata.admin.builder.%s_list', $managerType),
'datagrid_builder' => sprintf('sonata.admin.builder.%s_datagrid', $managerType),
'translator' => 'translator',
'configuration_pool' => 'sonata.admin.pool',
'route_generator' => 'sonata.admin.route.default_generator',
'validator' => 'validator', // NEXT_MAJOR: Remove this line
'security_handler' => 'sonata.admin.security.handler',
'menu_factory' => 'knp_menu.factory',
'route_builder' => 'sonata.admin.route.path_info'.
(('doctrine_phpcr' === $managerType) ? '_slashes' : ''),
'label_translator_strategy' => 'sonata.admin.label.strategy.native',
];
$definition->addMethodCall('setManagerType', [$managerType]);
foreach ($defaultAddServices as $attr => $addServiceId) {
// NEXT_MAJOR: Remove this check
if ('data_source' === $attr && !$container->has($addServiceId)) {
continue;
}
// NEXT_MAJOR: Remove this check
if ('field_description_factory' === $attr && !$container->has($addServiceId)) {
continue;
}
$method = $this->generateSetterMethodName($attr);
if (!$definition->hasMethodCall($method)) {
$args = [new Reference($overwriteAdminConfiguration[$attr] ?? $addServiceId)];
if ('translator' === $attr) {
$args[] = false;
}
$definition->addMethodCall($method, $args);
}
}
$pagerType = $overwriteAdminConfiguration['pager_type'] ?? $attributes['pager_type'] ?? Pager::TYPE_DEFAULT;
$definition->addMethodCall('setPagerType', [$pagerType]);
// NEXT_MAJOR: Default to null
$label = $overwriteAdminConfiguration['label'] ?? $attributes['label'] ?? '-';
$definition->addMethodCall('setLabel', [$label]);
$persistFilters = $attributes['persist_filters']
?? $container->getParameter('sonata.admin.configuration.filters.persist');
$filtersPersister = $attributes['filter_persister']
?? $container->getParameter('sonata.admin.configuration.filters.persister');
// configure filters persistence, if configured to
if ($persistFilters) {
$definition->addMethodCall('setFilterPersister', [new Reference($filtersPersister)]);
}
$showMosaicButton = $overwriteAdminConfiguration['show_mosaic_button']
?? $attributes['show_mosaic_button']
?? $container->getParameter('sonata.admin.configuration.show.mosaic.button');
$listModes = TaggedAdminInterface::DEFAULT_LIST_MODES;
if (!$showMosaicButton) {
unset($listModes['mosaic']);
}
$definition->addMethodCall('setListModes', [$listModes]);
$this->fixTemplates(
$serviceId,
$container,
$definition,
$overwriteAdminConfiguration['templates'] ?? ['view' => []]
);
if ($container->hasParameter('sonata.admin.configuration.security.information') && !$definition->hasMethodCall('setSecurityInformation')) {
$definition->addMethodCall('setSecurityInformation', ['%sonata.admin.configuration.security.information%']);
}
$defaultTemplates = $container->getParameter('sonata.admin.configuration.templates');
\assert(\is_array($defaultTemplates));
if (!$definition->hasMethodCall('setFormTheme')) {
// NEXT_MAJOR: Remove this line and uncomment the following one.
$formTheme = $overwriteAdminConfiguration['templates']['form'] ?? $defaultTemplates['form_theme'] ?? [];
// $formTheme = $defaultTemplates['form_theme'] ?? [];
$definition->addMethodCall('setFormTheme', [$formTheme]);
}
if (!$definition->hasMethodCall('setFilterTheme')) {
// NEXT_MAJOR: Remove this line and uncomment the following one.
$filterTheme = $overwriteAdminConfiguration['templates']['filter'] ?? $defaultTemplates['filter_theme'] ?? [];
// $filterTheme = $defaultTemplates['filter_theme'] ?? [];
$definition->addMethodCall('setFilterTheme', [$filterTheme]);
}
return $definition;
}
/**
* NEXT_MAJOR: Change visibility to private.
*
* @param string $serviceId
*/
public function fixTemplates(
$serviceId,
ContainerBuilder $container,
Definition $definition,
array $overwrittenTemplates = []
) {
$definedTemplates = $container->getParameter('sonata.admin.configuration.templates');
$methods = [];
$pos = 0;
foreach ($definition->getMethodCalls() as [$method, $args]) {
if ('setTemplates' === $method) {
$definedTemplates = array_merge($definedTemplates, $args[0]);
continue;
}
if ('setTemplate' === $method) {
$definedTemplates[$args[0]] = $args[1];
continue;
}
// set template for simple pager if it is not already overwritten
if ('setPagerType' === $method
&& Pager::TYPE_SIMPLE === $args[0]
&& (
!isset($definedTemplates['pager_results'])
|| '@SonataAdmin/Pager/results.html.twig' === $definedTemplates['pager_results']
)
) {
$definedTemplates['pager_results'] = '@SonataAdmin/Pager/simple_pager_results.html.twig';
}
$methods[$pos] = [$method, $args];
++$pos;
}
$definition->setMethodCalls($methods);
$definedTemplates = $overwrittenTemplates['view'] + $definedTemplates;
$templateRegistryId = sprintf('%s.template_registry', $serviceId);
$templateRegistryDefinition = $container
->register($templateRegistryId, TemplateRegistry::class)
->addTag('sonata.admin.template_registry')
->setPublic(true); // Temporary fix until we can support service locators
if ($container->getParameter('sonata.admin.configuration.templates') !== $definedTemplates) {
$templateRegistryDefinition->addArgument($definedTemplates);
} else {
$templateRegistryDefinition->addArgument('%sonata.admin.configuration.templates%');
}
$definition->addMethodCall('setTemplateRegistry', [new Reference($templateRegistryId)]);
}
/**
* Replace the empty arguments required by the Admin service definition.
*/
private function replaceDefaultArguments(
array $defaultArguments,
Definition $definition,
?Definition $parentDefinition = null
): void {
$arguments = $definition->getArguments();
$parentArguments = $parentDefinition ? $parentDefinition->getArguments() : [];
foreach ($defaultArguments as $index => $value) {
$declaredInParent = $parentDefinition && \array_key_exists($index, $parentArguments);
$argumentValue = $declaredInParent ? $parentArguments[$index] : $arguments[$index];
if (null === $argumentValue || '' === $argumentValue) {
$arguments[$declaredInParent ? sprintf('index_%s', $index) : $index] = $value;
}
}
$definition->setArguments($arguments);
}
private function generateSetterMethodName(string $key): string
{
return 'set'.InflectorFactory::create()->build()->classify($key);
}
}