Skip to content

Commit 6b27e12

Browse files
committed
merged branch dbu/doctrine-register-mappings-pass (PR symfony#7599)
This PR was squashed before being merged into the master branch (closes symfony#7599). Discussion ---------- [Doctrine-Bridge] add a base compiler pass class to register doctrine mappings | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | not on code, but defining best practices | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#2507 Reusable bundles providing model classes should not rely on the automapping provided by the doctrine bundles. What is more, the same model can often be mapped to Doctrine ORM, MongoODM, CouchODM and PHPCR-ODM. This pull request adds a base class for a compiler pass that the concrete doctrine bundles extend to provide a default compiler pass that makes it trivial for bundles to register their mappings. See doctrine/DoctrineBundle#177 The FOSUserBundle shows how this would look in practice. See FriendsOfSymfony/FOSUserBundle#1081 I will create the documentation pull request as well as pull requests for the mongo, couch and phpcr bundles once we agree how exactly to do this. Commits ------- 099fd9f [Doctrine-Bridge] add a base compiler pass class to register doctrine mappings
2 parents fef7990 + 099fd9f commit 6b27e12

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 <[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 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 <[email protected]>
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)