Skip to content

Commit f07fdb2

Browse files
committed
Merge pull request #140 from symfony-cmf/cleanup-mapping-compiler-passes
only load mapper compiler pass if the orm resp phpcr-odm is available
2 parents 38c0738 + 3d4fd56 commit f07fdb2

File tree

6 files changed

+112
-78
lines changed

6 files changed

+112
-78
lines changed

CmfRoutingBundle.php

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
66
use Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
78
use Symfony\Component\DependencyInjection\Definition;
89
use Symfony\Component\HttpKernel\Bundle\Bundle;
910
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -26,46 +27,84 @@ public function build(ContainerBuilder $container)
2627
$container->addCompilerPass(new RegisterRouteEnhancersPass());
2728
$container->addCompilerPass(new SetRouterPass());
2829

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;
4146
}
4247

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+
}
4474

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;
5778
}
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+
);
5893
}
5994

6095
/**
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
63102
*/
64103
private function findDoctrineOrmCompiler()
65104
{
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+
) {
69108
return 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
70109
}
71110

@@ -76,41 +115,29 @@ private function findDoctrineOrmCompiler()
76115
return false;
77116
}
78117

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-
}
95118

96119
/**
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
100127
*
101-
* @return DoctrinePhpcrMappingsPass
128+
* @return CompilerPassInterface
102129
*/
103-
private function buildBasePhpcrCompilerPass()
130+
private function buildBaseCompilerPass($compilerClass, $driverClass, $type)
104131
{
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));
106133
$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));
108135

109-
return new DoctrinePhpcrMappingsPass(
136+
return new $compilerClass(
110137
$driver,
111138
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)
114141
);
115142
}
116143
}

Resources/translations/CmfRoutingBundle.de.xliff

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
<source>filter.label_name</source>
5151
<target>Name</target>
5252
</trans-unit>
53-
<trans-unit id="list.label_path">
54-
<source>list.label_path</source>
55-
<target>Pfad</target>
53+
<trans-unit id="list.label_id">
54+
<source>list.label_id</source>
55+
<target>Id</target>
5656
</trans-unit>
5757
<trans-unit id="form.group_general">
5858
<source>form.group_general</source>
@@ -70,8 +70,8 @@
7070
<source>form.label_variable_pattern</source>
7171
<target>Variablenmuster</target>
7272
</trans-unit>
73-
<trans-unit id="form.label_route_content">
74-
<source>form.label_route_content</source>
73+
<trans-unit id="form.label_content">
74+
<source>form.label_content</source>
7575
<target>Inhalt</target>
7676
</trans-unit>
7777
<trans-unit id="form.label_defaults">

Resources/translations/CmfRoutingBundle.en.xliff

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
<source>filter.label_name</source>
5151
<target>Name</target>
5252
</trans-unit>
53-
<trans-unit id="list.label_path">
54-
<source>list.label_path</source>
55-
<target>Path</target>
53+
<trans-unit id="list.label_id">
54+
<source>list.label_id</source>
55+
<target>Id</target>
5656
</trans-unit>
5757
<trans-unit id="form.group_general">
5858
<source>form.group_general</source>
@@ -70,8 +70,8 @@
7070
<source>form.label_variable_pattern</source>
7171
<target>Variable pattern</target>
7272
</trans-unit>
73-
<trans-unit id="form.label_route_content">
74-
<source>form.label_route_content</source>
73+
<trans-unit id="form.label_content">
74+
<source>form.label_content</source>
7575
<target>Content</target>
7676
</trans-unit>
7777
<trans-unit id="form.label_defaults">

Resources/translations/CmfRoutingBundle.fr.xliff

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
<source>filter.label_name</source>
5151
<target>Nom</target>
5252
</trans-unit>
53-
<trans-unit id="list.label_path">
54-
<source>list.label_path</source>
55-
<target>Chemin</target>
53+
<trans-unit id="list.label_id">
54+
<source>list.label_id</source>
55+
<target>Id</target>
5656
</trans-unit>
5757
<trans-unit id="form.group_general">
5858
<source>form.group_general</source>
@@ -70,8 +70,8 @@
7070
<source>form.label_variable_pattern</source>
7171
<target>Motif variable</target>
7272
</trans-unit>
73-
<trans-unit id="form.label_route_content">
74-
<source>form.label_route_content</source>
73+
<trans-unit id="form.label_content">
74+
<source>form.label_content</source>
7575
<target>Contenu</target>
7676
</trans-unit>
7777
<trans-unit id="form.label_defaults">

Resources/translations/CmfRoutingBundle.pl.xliff

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@
5050
<source>filter.label_name</source>
5151
<target>Nazwa</target>
5252
</trans-unit>
53-
<trans-unit id="list.label_path">
54-
<source>list.label_path</source>
55-
<target>Ścieżka</target>
56-
</trans-unit>
5753
<trans-unit id="list.label_id">
5854
<source>list.label_id</source>
5955
<target>Ścieżka</target>
@@ -74,8 +70,8 @@
7470
<source>form.label_variable_pattern</source>
7571
<target>Wzór zmiennych</target>
7672
</trans-unit>
77-
<trans-unit id="form.label_route_content">
78-
<source>form.label_route_content</source>
73+
<trans-unit id="form.label_content">
74+
<source>form.label_content</source>
7975
<target>Zawartość</target>
8076
</trans-unit>
8177
<trans-unit id="form.label_defaults">

Tests/Functional/Admin/RouteAdminTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ protected function setUp()
3535
public function testCorrectControllerPath()
3636
{
3737
$route = new Route('/', array('_controller' => 'FrameworkBundle:Redirect:redirect'));
38+
39+
$this->errorElement->expects($this->never())
40+
->method('with')
41+
;
42+
3843
$this->routeAdmin->validate($this->errorElement, $route);
3944
}
4045

@@ -74,7 +79,13 @@ public function testTemplateViolation()
7479

7580
public function testCorrectTemplate()
7681
{
77-
$route = new Route('/', array('_template' => 'TwigBundle::layout.html.twig'));
82+
// the template 'TwigBundle::layout.html.twig' is not found in the test setup...
83+
$mockTemplate = $this->getMockBuilder('Twig_Template')->disableOriginalConstructor()->getMockForAbstractClass();
84+
$route = new Route('/', array('_template' => $mockTemplate));
85+
$this->errorElement->expects($this->never())
86+
->method('with')
87+
;
88+
7889
$this->routeAdmin->validate($this->errorElement, $route);
7990
}
8091
}

0 commit comments

Comments
 (0)