1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata project. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\DoctrinePHPCRAdminBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Sonata\AdminBundle\DependencyInjection\AbstractSonataAdminExtension; |
17
|
|
|
|
18
|
|
|
use Symfony\Component\Config\FileLocator; |
19
|
|
|
use Symfony\Component\Config\Definition\Processor; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SonataAdminBundleExtension |
23
|
|
|
* |
24
|
|
|
* @author Thomas Rabaix <[email protected]> |
25
|
|
|
* @author Michael Williams <[email protected]> |
26
|
|
|
* @author Nacho Martín <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class SonataDoctrinePHPCRAdminExtension extends AbstractSonataAdminExtension |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param array $configs An array of configuration settings |
32
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
33
|
|
|
*/ |
34
|
|
|
public function load(array $configs, ContainerBuilder $container) |
35
|
|
|
{ |
36
|
|
|
$defaultConfig = array( |
37
|
|
|
'templates' => array( |
38
|
|
|
'types' => array( |
39
|
|
|
'list' => array( |
40
|
|
|
'node' => 'SonataDoctrinePHPCRAdminBundle:CRUD:list_node.html.twig', |
41
|
|
|
), |
42
|
|
|
'show' => array( |
43
|
|
|
'doctrine_phpcr_many_to_many' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_many_to_many.html.twig', |
44
|
|
|
'doctrine_phpcr_many_to_one' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_many_to_one.html.twig', |
45
|
|
|
'doctrine_phpcr_one_to_many' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_one_to_many.html.twig', |
46
|
|
|
'doctrine_phpcr_one_to_one' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_one_to_one.html.twig', |
47
|
|
|
) |
48
|
|
|
) |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$configs = $this->fixTemplatesConfiguration($configs, $container, $defaultConfig); |
53
|
|
|
|
54
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
55
|
|
|
$loader->load('doctrine_phpcr.xml'); |
56
|
|
|
$loader->load('doctrine_phpcr_filter_types.xml'); |
57
|
|
|
$loader->load('doctrine_phpcr_form_types.xml'); |
58
|
|
|
$loader->load('form.xml'); |
59
|
|
|
$loader->load('route.xml'); |
60
|
|
|
$loader->load('twig.xml'); |
61
|
|
|
$loader->load('block.xml'); |
62
|
|
|
$loader->load('tree.xml'); |
63
|
|
|
|
64
|
|
|
$configuration = new Configuration(); |
65
|
|
|
$processor = new Processor(); |
66
|
|
|
$config = $processor->processConfiguration($configuration, $configs); |
67
|
|
|
|
68
|
|
|
$pool = $container->getDefinition('sonata.admin.manager.doctrine_phpcr'); |
69
|
|
|
$pool->addMethodCall('__hack_doctrine_phpcr__', $config); |
70
|
|
|
|
71
|
|
|
$container->getDefinition('sonata.admin.builder.doctrine_phpcr_list') |
72
|
|
|
->replaceArgument(1, $config['templates']['types']['list']); |
73
|
|
|
|
74
|
|
|
$container->getDefinition('sonata.admin.builder.doctrine_phpcr_show') |
75
|
|
|
->replaceArgument(1, $config['templates']['types']['show']); |
76
|
|
|
|
77
|
|
|
$this->loadTreeTypes($config, $container); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the tree type mapping configuration in the services |
82
|
|
|
* |
83
|
|
|
* @param array $config |
84
|
|
|
* @param ContainerBuilder $container |
85
|
|
|
*/ |
86
|
|
|
private function loadTreeTypes($config, ContainerBuilder $container) |
87
|
|
|
{ |
88
|
|
|
$options = $config['document_tree_options']; |
89
|
|
|
$container->setParameter('sonata_admin_doctrine_phpcr.tree_block.defaults', $config['document_tree_defaults']); |
90
|
|
|
$container->setParameter('sonata_admin_doctrine_phpcr.tree_confirm_move', $options['confirm_move']); |
91
|
|
|
unset($options['confirm_move']); |
92
|
|
|
$container->getDefinition('sonata.admin.doctrine_phpcr.phpcr_odm_tree') |
93
|
|
|
->replaceArgument(5, $this->processDocumentTreeConfig($config['document_tree'])); |
94
|
|
|
$container->getDefinition('sonata.admin.doctrine_phpcr.phpcr_odm_tree') |
95
|
|
|
->replaceArgument(6, $options); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Process the document tree config |
100
|
|
|
* Expand references to 'all' to an array of all types |
101
|
|
|
* Validate document types |
102
|
|
|
* |
103
|
|
|
* @param array $documentTree |
104
|
|
|
*/ |
105
|
|
|
private function processDocumentTreeConfig(array $documentTree) |
106
|
|
|
{ |
107
|
|
|
$docClasses = $this->findAllDocumentClasses($documentTree); |
108
|
|
|
|
109
|
|
|
// Validate all document classes |
110
|
|
|
$invalidClasses = array_filter( |
111
|
|
|
$docClasses, |
112
|
|
|
function ($class) { |
113
|
|
|
return false === class_exists($class); |
114
|
|
|
} |
115
|
|
|
); |
116
|
|
|
if (count($invalidClasses)) { |
117
|
|
|
throw new \InvalidArgumentException(sprintf( |
118
|
|
|
'The following document types provided in valid_children are invalid: %s '. |
119
|
|
|
'The class names provided could not be loaded.', |
120
|
|
|
implode(', ', array_unique($invalidClasses)) |
121
|
|
|
)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Process the config |
125
|
|
|
$processed = array(); |
126
|
|
|
foreach ($documentTree as $docClass => $config) { |
127
|
|
|
// Expand 'all' |
128
|
|
|
if (false !== array_search('all', $config['valid_children'])) { |
129
|
|
|
$config['valid_children'] = $docClasses; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$processed[$docClass] = $config; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $processed; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Find all document classes within a document tree |
140
|
|
|
* |
141
|
|
|
* @param array $documentTree |
142
|
|
|
*/ |
143
|
|
|
private function findAllDocumentClasses(array $documentTree) |
144
|
|
|
{ |
145
|
|
|
$documentClasses = array_unique(array_reduce( |
146
|
|
|
$documentTree, |
147
|
|
|
function ($result, $config) { |
148
|
|
|
return array_merge($result, $config['valid_children']); |
149
|
|
|
}, |
150
|
|
|
array_keys($documentTree) |
151
|
|
|
)); |
152
|
|
|
|
153
|
|
|
if (false !== ($allIndex = array_search('all', $documentClasses))) { |
154
|
|
|
unset($documentClasses[$allIndex]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $documentClasses; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getNamespace() |
161
|
|
|
{ |
162
|
|
|
return 'http://sonata-project.org/schema/dic/doctrine_phpcr_admin'; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|