forked from symfony-cmf/routing-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmfRoutingBundle.php
157 lines (142 loc) · 6.31 KB
/
CmfRoutingBundle.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
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\RoutingBundle;
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRoutersPass;
use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\SetRouterPass;
/**
* Bundle class.
*/
class CmfRoutingBundle extends Bundle
{
/**
* @param ContainerBuilder $container
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new RegisterRoutersPass());
$container->addCompilerPass(new RegisterRouteEnhancersPass());
$container->addCompilerPass(new SetRouterPass());
$this->buildPhpcrCompilerPass($container);
$this->buildOrmCompilerPass($container);
}
/**
* Creates and registers compiler passes for PHPCR-ODM mapping if both the
* phpcr-odm and the phpcr-bundle are present.
*
* @param ContainerBuilder $container
*/
private function buildPhpcrCompilerPass(ContainerBuilder $container)
{
if (!class_exists('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass')
|| !class_exists('Doctrine\ODM\PHPCR\Version')
) {
return;
}
$container->addCompilerPass(
$this->buildBaseCompilerPass('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass', 'Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver', 'phpcr')
);
$container->addCompilerPass(
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
array(
realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
realpath(__DIR__.'/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr',
),
array('cmf_routing.dynamic.persistence.phpcr.manager_name'),
'cmf_routing.backend_type_phpcr',
array('CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr')
)
);
}
/**
* Creates and registers compiler passes for ORM mappings if both doctrine
* ORM and a suitable compiler pass implementation are available.
*
* @param ContainerBuilder $container
*/
private function buildOrmCompilerPass(ContainerBuilder $container)
{
if (!class_exists('Doctrine\ORM\Version')) {
return;
}
$doctrineOrmCompiler = $this->findDoctrineOrmCompiler();
if (!$doctrineOrmCompiler) {
return;
}
$container->addCompilerPass(
$this->buildBaseCompilerPass($doctrineOrmCompiler, 'Doctrine\ORM\Mapping\Driver\XmlDriver', 'orm')
);
$bundles = $container->getParameter('kernel.bundles');
$mappingDirs = array(
realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
realpath(__DIR__ . '/Resources/config/doctrine-orm-base') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Base',
);
if (!isset($bundles['CmfRoutingAutoBundle'])) {
$mappingDirs[realpath(__DIR__ . '/Resources/config/doctrine-orm-entity')] = 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm';
}
$container->addCompilerPass(
$doctrineOrmCompiler::createXmlMappingDriver(
$mappingDirs,
array('cmf_routing.dynamic.persistence.orm.manager_name'),
'cmf_routing.backend_type_orm',
array('CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm')
)
);
}
/**
* Looks for a mapping compiler pass. If available, use the one from
* DoctrineBundle (available only since DoctrineBundle 2.4 and Symfony 2.3)
* Otherwise use the standalone one from CmfCoreBundle.
*
* @return bool|string the compiler pass to use or false if no suitable
* one was found
*/
private function findDoctrineOrmCompiler()
{
if (class_exists('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass')
&& class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')
) {
return 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
}
if (class_exists('Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
return 'Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
}
return false;
}
/**
* Builds the compiler pass for the symfony core routing component. The
* compiler pass factory method uses the SymfonyFileLocator which does
* magic with the namespace and thus does not work here.
*
* @param string $compilerClass the compiler class to instantiate
* @param string $driverClass the xml driver class for this backend
* @param string $type the backend type name
*
* @return CompilerPassInterface
*/
private function buildBaseCompilerPass($compilerClass, $driverClass, $type)
{
$arguments = array(array(realpath(__DIR__.'/Resources/config/doctrine-base')), sprintf('.%s.xml', $type));
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator', $arguments);
$driver = new Definition($driverClass, array($locator));
return new $compilerClass(
$driver,
array('Symfony\Component\Routing'),
array(sprintf('cmf_routing.dynamic.persistence.%s.manager_name', $type)),
sprintf('cmf_routing.backend_type_%s', $type)
);
}
}