4
4
5
5
use Doctrine \Bundle \PHPCRBundle \DependencyInjection \Compiler \DoctrinePhpcrMappingsPass ;
6
6
use Symfony \Cmf \Bundle \CoreBundle \DependencyInjection \Compiler \DoctrineOrmMappingsPass ;
7
+ use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
7
8
use Symfony \Component \DependencyInjection \Definition ;
8
9
use Symfony \Component \HttpKernel \Bundle \Bundle ;
9
10
use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -26,46 +27,84 @@ public function build(ContainerBuilder $container)
26
27
$ container ->addCompilerPass (new RegisterRouteEnhancersPass ());
27
28
$ container ->addCompilerPass (new SetRouterPass ());
28
29
29
- if (class_exists ('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass ' )) {
30
- $ container ->addCompilerPass ($ this ->buildBasePhpcrCompilerPass ());
31
- $ container ->addCompilerPass (
32
- DoctrinePhpcrMappingsPass::createXmlMappingDriver (
33
- array (
34
- realpath (__DIR__ . '/Resources/config/doctrine-model ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Model ' ,
35
- realpath (__DIR__ . '/Resources/config/doctrine-phpcr ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr ' ,
36
- ),
37
- array ('cmf_routing.dynamic.persistence.phpcr.manager_name ' ),
38
- 'cmf_routing.backend_type_phpcr '
39
- )
40
- );
30
+ $ this ->buildPhpcrCompilerPass ($ container );
31
+ $ this ->buildOrmCompilerPass ($ container );
32
+ }
33
+
34
+ /**
35
+ * Creates and registers compiler passes for PHPCR-ODM mapping if both the
36
+ * phpcr-odm and the phpcr-bundle are present.
37
+ *
38
+ * @param ContainerBuilder $container
39
+ */
40
+ private function buildPhpcrCompilerPass (ContainerBuilder $ container )
41
+ {
42
+ if (!class_exists ('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass ' )
43
+ || !class_exists ('Doctrine\ODM\PHPCR\Version ' )
44
+ ) {
45
+ return ;
41
46
}
42
47
43
- $ doctrineOrmCompiler = $ this ->findDoctrineOrmCompiler ();
48
+ $ container ->addCompilerPass (
49
+ $ this ->buildBaseCompilerPass ('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass ' , 'Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver ' , 'phpcr ' )
50
+ );
51
+ $ container ->addCompilerPass (
52
+ DoctrinePhpcrMappingsPass::createXmlMappingDriver (
53
+ array (
54
+ realpath (__DIR__ .'/Resources/config/doctrine-model ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Model ' ,
55
+ realpath (__DIR__ .'/Resources/config/doctrine-phpcr ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr ' ,
56
+ ),
57
+ array ('cmf_routing.dynamic.persistence.phpcr.manager_name ' ),
58
+ 'cmf_routing.backend_type_phpcr '
59
+ )
60
+ );
61
+ }
62
+
63
+ /**
64
+ * Creates and registers compiler passes for ORM mappings if both doctrine
65
+ * ORM and a suitable compiler pass implementation are available.
66
+ *
67
+ * @param ContainerBuilder $container
68
+ */
69
+ private function buildOrmCompilerPass (ContainerBuilder $ container )
70
+ {
71
+ if (!class_exists ('Doctrine\ORM\Version ' )) {
72
+ return ;
73
+ }
44
74
45
- if ($ doctrineOrmCompiler ) {
46
- $ container ->addCompilerPass ($ this ->buildBaseOrmCompilerPass ($ doctrineOrmCompiler ));
47
- $ container ->addCompilerPass (
48
- $ doctrineOrmCompiler ::createXmlMappingDriver (
49
- array (
50
- realpath (__DIR__ . '/Resources/config/doctrine-model ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Model ' ,
51
- realpath (__DIR__ . '/Resources/config/doctrine-orm ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm ' ,
52
- ),
53
- array ('cmf_routing.dynamic.persistence.orm.manager_name ' ),
54
- 'cmf_routing.backend_type_orm '
55
- )
56
- );
75
+ $ doctrineOrmCompiler = $ this ->findDoctrineOrmCompiler ();
76
+ if (!$ doctrineOrmCompiler ) {
77
+ return ;
57
78
}
79
+
80
+ $ container ->addCompilerPass (
81
+ $ this ->buildBaseCompilerPass ($ doctrineOrmCompiler , 'Doctrine\ORM\Mapping\Driver\XmlDriver ' , 'orm ' )
82
+ );
83
+ $ container ->addCompilerPass (
84
+ $ doctrineOrmCompiler ::createXmlMappingDriver (
85
+ array (
86
+ realpath (__DIR__ . '/Resources/config/doctrine-model ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Model ' ,
87
+ realpath (__DIR__ . '/Resources/config/doctrine-orm ' ) => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm ' ,
88
+ ),
89
+ array ('cmf_routing.dynamic.persistence.orm.manager_name ' ),
90
+ 'cmf_routing.backend_type_orm '
91
+ )
92
+ );
58
93
}
59
94
60
95
/**
61
- * Searches a mapping compiler (doctrine bridge compiler is missing in symfony < 2.3).
62
- * Use Cmf\CoreBundle in that case.
96
+ * Looks for a mapping compiler pass. If available, use the one from
97
+ * DoctrineBundle (available only since DoctrineBundle 2.4 and Symfony 2.3)
98
+ * Otherwise use the standalone one from CmfCoreBundle.
99
+ *
100
+ * @return boolean|string the compiler pass to use or false if no suitable
101
+ * one was found
63
102
*/
64
103
private function findDoctrineOrmCompiler ()
65
104
{
66
- $ symfonyVersion = class_exists ('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass ' );
67
-
68
- if ( $ symfonyVersion && class_exists ( ' Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass ' ) ) {
105
+ if ( class_exists ('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass ' )
106
+ && class_exists ( ' Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass ' )
107
+ ) {
69
108
return 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass ' ;
70
109
}
71
110
@@ -76,41 +115,29 @@ private function findDoctrineOrmCompiler()
76
115
return false ;
77
116
}
78
117
79
- /**
80
- * Instantiate compiler now because of SymfonyFileLocator namespace issue (see phpcr method comment).
81
- */
82
- private function buildBaseOrmCompilerPass ($ doctrineOrmCompiler )
83
- {
84
- $ arguments = array (array (realpath (__DIR__ . '/Resources/config/doctrine-base ' )), '.orm.xml ' );
85
- $ locator = new Definition ('Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator ' , $ arguments );
86
- $ driver = new Definition ('Doctrine\ORM\Mapping\Driver\XmlDriver ' , array ($ locator ));
87
-
88
- return new $ doctrineOrmCompiler (
89
- $ driver ,
90
- array ('Symfony\Component\Routing ' ),
91
- array ('cmf_routing.dynamic.persistence.orm.manager_name ' ),
92
- 'cmf_routing.backend_type_orm '
93
- );
94
- }
95
118
96
119
/**
97
- * Build the compiler pass for the symfony core routing component. The
98
- * factory method uses the SymfonyFileLocator which will look at the
99
- * namespace and thus not work here.
120
+ * Builds the compiler pass for the symfony core routing component. The
121
+ * compiler pass factory method uses the SymfonyFileLocator which does
122
+ * magic with the namespace and thus does not work here.
123
+ *
124
+ * @param string $compilerClass the compiler class to instantiate
125
+ * @param string $driverClass the xml driver class for this backend
126
+ * @param string $type the backend type name
100
127
*
101
- * @return DoctrinePhpcrMappingsPass
128
+ * @return CompilerPassInterface
102
129
*/
103
- private function buildBasePhpcrCompilerPass ( )
130
+ private function buildBaseCompilerPass ( $ compilerClass , $ driverClass , $ type )
104
131
{
105
- $ arguments = array (array (realpath (__DIR__ . '/Resources/config/doctrine-base ' )), ' .phpcr .xml ' );
132
+ $ arguments = array (array (realpath (__DIR__ . '/Resources/config/doctrine-base ' )), sprintf ( ' .%s .xml ', $ type ) );
106
133
$ locator = new Definition ('Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator ' , $ arguments );
107
- $ driver = new Definition (' Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver ' , array ($ locator ));
134
+ $ driver = new Definition ($ driverClass , array ($ locator ));
108
135
109
- return new DoctrinePhpcrMappingsPass (
136
+ return new $ compilerClass (
110
137
$ driver ,
111
138
array ('Symfony\Component\Routing ' ),
112
- array ('cmf_routing.dynamic.persistence.phpcr .manager_name ' ),
113
- 'cmf_routing.backend_type_phpcr '
139
+ array (sprintf ( 'cmf_routing.dynamic.persistence.%s .manager_name ' , $ type ) ),
140
+ sprintf ( 'cmf_routing.backend_type_%s ' , $ type )
114
141
);
115
142
}
116
143
}
0 commit comments