8000 [Doctrine-Bridge] add a base compiler pass class to register doctrine… · symfony/symfony@099fd9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 099fd9f

Browse files
dbufabpot
authored andcommitted
[Doctrine-Bridge] add a base compiler pass class to register doctrine mappings
1 parent 83e078a commit 099fd9f

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;
13+
14+
use Symfony\Component\HttpKernel\Kernel;
15+
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20+
21+
/**
22+
* Base class for the doctrine bundles to provide a compiler pass class that
23+
* helps to register doctrine mappings. For concrete implementations that are
24+
* easy to use, see the RegisterXyMappingsPass classes in the DoctrineBundle
25+
* resp. DoctrineMongodbBundle, DoctrineCouchdbBundle and DoctrinePhpcrBundle.
26+
*
27+
* @author David Buchmann <david@liip.ch>
28+
*/
29+
abstract class RegisterMappingsPass implements CompilerPassInterface
30+
{
31+
/**
32+
* DI object for the driver to use, either a service definition for a
33+
* private service or a reference for a public service.
34+
* @var Definition|Reference
35+
*/
36+
protected $driver;
37+
38+
/**
39+
* List of namespaces handled by the driver
40+
* @var string[]
41+
*/
42+
protected $namespaces;
43+
44+
/**
45+
* Parameter name of the entity managers list in the service container.
46+
* For example 'doctrine.entity_managers'
47+
* @var string
48+
*/
49+
protected $managersParameter;
50+
51+
/**
52+
* Naming pattern of the metadata service ids, for example 'doctrine.orm.%s_metadata_driver'
53+
* @var string
54+
*/
55+
protected $driverPattern;
56+
57+
/**
58+
* A name for a parameter in the container. If set, this compiler pass will
59+
* only do anything if the parameter is present. (But regardless of the
60+
* value of that parameter.
61+
* @var string
62+
*/
63+
protected $enabledParameter;
64+
65+
/**
66+
* @param Definition|Reference $driver driver DI definition or reference
67+
* @param string[] $namespaces list of namespaces handled by $driver
68+
* @param string $managersParameter service container parameter name for the managers list
69+
* @param string $driverPattern pattern to get the metadata driver service names
70+
* @param string $enableParameter service container parameter that must be
71+
* present to enable the mapping. Set to false to not do any check, optional.
72+
*/
73+
public function __construct($driver, array $namespaces, $managersParameter, $driverPattern, $enableParameter = false)
74+
{
75+
$this->driver = $driver;
76+
$this->namespaces = $namespaces;
77+
$this->managersParameter = $managersParameter;
78+
$this->driverPattern = $driverPattern;
79+
$this->enabledParameter = $enableParameter;
80+
}
81+
82+
/**
83+
* Register mappings with the metadata drivers.
84+
*
85+
* @param ContainerBuilder $container
86+
*/
87+
public function process(ContainerBuilder $container)
88+
{
89+
if (!$this->enabled($container)) {
90+
return;
91+
}
92+
93+
$mappingDriverDef = $this->getDriver($container);
94+
foreach ($container->getParameter($this->managersParameter) as $name => $manager) {
95+
$chainDriverDefService = sprintf($this->driverPattern, $name);
96+
$chainDriverDef = $container->getDefinition($chainDriverDefService);
97+
foreach ($this->namespaces as $namespace) {
98+
$chainDriverDef->addMethodCall('addDriver', array($mappingDriverDef, $namespace));
99+
}
100+
}
101+
}
102+
103+
/**
104+
* Create the service definition for the metadata driver.
105+
*
106+
* @param ContainerBuilder $container passed on in case an extending class
107+
* needs access to the container.
108+
*
109+
* @return Definition|Reference the metadata driver to add to all chain drivers
110+
*/
111+
protected function getDriver(ContainerBuilder $container)
112+
{
113+
return $this->driver;
114+
}
115+
116+
/**
117+
* Determine whether this mapping should be activated or not. This allows
118+
* to take this decision with the container builder available.
119+
*
120+
* This default implementation checks if the class has the enabledParameter
121+
* configured and if so if that parameter is present in the container.
122+
*
123+
* @param ContainerBuilder $container
124+
*
125+
* @return boolean whether this compiler pass really should register the mappings
126+
*/
127+
protected function enabled(ContainerBuilder $container)
128+
{
129+
return !$this->enabledParameter || $container->hasParameter($this->enabledParameter);
130+
}
131+
}

0 commit comments

Comments
 (0)
0