|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Zicht\Bundle\PageBundle\DependencyInjection\CompilerPass; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
11
|
|
|
use Zicht\Bundle\PageBundle\Model\ContentItemContainer; |
|
12
|
|
|
use Zicht\Util\Str; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Generates admin services for all pages and content items, if the admin classes match the entity namespace structure. |
|
16
|
|
|
*/ |
|
17
|
|
|
class GenerateAdminServicesCompilerPass implements CompilerPassInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @{inheritDoc} |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function process(ContainerBuilder $container) |
|
23
|
|
|
{ |
|
24
|
2 |
|
$config = $container->getParameter('zicht_page.config'); |
|
25
|
2 |
|
if (empty($config['admin'])) { |
|
26
|
1 |
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
$config = $container->getParameter('zicht_page.config'); |
|
30
|
|
|
|
|
31
|
1 |
|
$baseIds = $config['admin']['base']; |
|
32
|
|
|
|
|
33
|
1 |
|
$serviceDefinitions = []; |
|
34
|
1 |
|
$pageManagerDef = $container->getDefinition('zicht_page.page_manager'); |
|
35
|
1 |
|
$types = []; |
|
36
|
|
|
|
|
37
|
1 |
|
foreach ($pageManagerDef->getMethodCalls() as $call) { |
|
38
|
1 |
|
switch ($call[0]) { |
|
39
|
1 |
|
case 'setPageTypes': |
|
40
|
1 |
|
$types['page'] = $call[1][0]; |
|
41
|
1 |
|
break; |
|
42
|
1 |
|
case 'setContentItemTypes': |
|
43
|
1 |
|
$types['contentItem'] = $call[1][0]; |
|
44
|
1 |
|
break; |
|
45
|
1 |
|
} |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
foreach (['page', 'contentItem'] as $type) { |
|
49
|
1 |
|
$serviceDefinitions[$type] = []; |
|
50
|
|
|
|
|
51
|
1 |
|
$def = $container->getDefinition($baseIds[$type]); |
|
52
|
|
|
|
|
53
|
1 |
|
foreach ($types[$type] as $entityClassName) { |
|
54
|
1 |
|
$adminClassName = $this->renderAdminClassName($entityClassName); |
|
55
|
1 |
|
if (!class_exists($adminClassName)) { |
|
56
|
|
|
throw new \Exception( |
|
57
|
|
|
sprintf( |
|
58
|
|
|
'The PageBundle was unable to create a service definition for %s because the associated class %s was not found', |
|
59
|
|
|
$entityClassName, |
|
60
|
|
|
$adminClassName |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
$adminService = clone $def; |
|
66
|
1 |
|
$adminService->setClass($adminClassName); |
|
67
|
|
|
|
|
68
|
1 |
|
$id = Str::systemize($adminClassName, '.'); |
|
69
|
|
|
|
|
70
|
1 |
|
$tags = $adminService->getTags(); |
|
71
|
1 |
|
$tags['sonata.admin'][0]['show_in_dashboard'] = 0; |
|
72
|
1 |
|
$tags['sonata.admin'][0]['label'] = $id; |
|
73
|
1 |
|
$adminService->setTags($tags); |
|
74
|
|
|
|
|
75
|
1 |
|
$adminService->replaceArgument(0, $id); |
|
76
|
1 |
|
$adminService->replaceArgument(1, $entityClassName); |
|
77
|
|
|
|
|
78
|
1 |
|
$container->setDefinition($id, $adminService); |
|
79
|
|
|
|
|
80
|
1 |
|
$serviceDefinitions[$type][] = $id; |
|
81
|
1 |
|
} |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
foreach ($serviceDefinitions['page'] as $pageServiceId) { |
|
85
|
1 |
|
$pageDef = $container->getDefinition($pageServiceId); |
|
86
|
1 |
|
$pageClassName = $pageDef->getArgument(1); |
|
87
|
|
|
|
|
88
|
1 |
|
if (!is_a($pageClassName, 'Zicht\Bundle\PageBundle\Model\ContentItemContainer', true)) { |
|
89
|
1 |
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** @var ContentItemContainer $instance */ |
|
93
|
|
|
$instance = new $pageClassName; |
|
94
|
|
|
|
|
95
|
|
|
if (null === $matrix = $instance->getContentItemMatrix()) { |
|
96
|
|
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
foreach ($serviceDefinitions['contentItem'] as $contentItemServiceId) { |
|
100
|
|
|
$contentItemDefinition = $container->getDefinition($contentItemServiceId); |
|
101
|
|
|
|
|
102
|
|
|
if (in_array($contentItemDefinition->getArgument(1), $matrix->getTypes())) { |
|
103
|
|
|
$pageDef->addMethodCall('addChild', [new Reference($contentItemServiceId)]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
1 |
|
} |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $fullyQualifiedClassName |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
1 |
|
private function renderAdminClassName($fullyQualifiedClassName) |
|
115
|
|
|
{ |
|
116
|
1 |
|
return sprintf( |
|
117
|
1 |
|
'%s%s', |
|
118
|
1 |
|
str_replace('\\Entity\\', '\\Admin\\', $fullyQualifiedClassName), |
|
119
|
|
|
'Admin' |
|
120
|
1 |
|
); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|