Skip to content

Commit 9a62ab3

Browse files
committed
provide a compiler pass for doctrine to register namespace aliases and not only mapping drivers
1 parent 09f252a commit 9a62ab3

File tree

1 file changed

+109
-27
lines changed

1 file changed

+109
-27
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterMappingsPass.php

Lines changed: 109 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,66 @@ abstract class RegisterMappingsPass implements CompilerPassInterface
6969
protected $enabledParameter;
7070

7171
/**
72-
* @param Definition|Reference $driver driver DI definition or reference
73-
* @param string[] $namespaces list of namespaces handled by $driver
74-
* @param string[] $managerParameters list of container parameters
75-
* that could hold the manager name
76-
* @param string $driverPattern pattern to get the metadata driver service names
77-
* @param string|false $enabledParameter service container parameter that must be
78-
* present to enable the mapping. Set to false
79-
* to not do any check, optional.
72+
* Naming pattern for the configuration service id, for example
73+
* 'doctrine.orm.%s_configuration'
74+
* @var string
8075
*/
81-
public function __construct($driver, array $namespaces, array $managerParameters, $driverPattern, $enabledParameter = false)
82-
{
76+
private $configurationPattern;
77+
78+
/**
79+
* Method name to call on the configuration service. This depends on the
80+
* Doctrine implementation. For example addEntityNamespace
81+
* @var string
82+
*/
83+
private $registerAliasMethodName;
84+
85+
/**
86+
* Map of alias to namespace.
87+
* @var string[]
88+
*/
89+
private $aliasMap;
90+
91+
/**
92+
* Constructor.
93+
*
94+
* The $managerParameters is an ordered list of container parameters that could provide the
95+
* name of the manager to register these namespaces and alias on. The first non-empty name
96+
* is used, the others skipped.
97+
*
98+
* The $aliasMap parameter can be used to define bundle namespace shortcuts like the
99+
* DoctrineBundle provides automatically for objects in the default Entity/Document folder.
100+
*
101+
* @param Definition|Reference $driver Driver DI definition or reference.
102+
* @param string[] $namespaces List of namespaces handled by $driver.
103+
* @param string[] $managerParameters List of container parameters that could
104+
* hold the manager name.
105+
* @param string $driverPattern Pattern for the metadata driver service name.
106+
* @param string $enabledParameter Service container parameter that must be
107+
* present to enable the mapping. Set to false
108+
* to not do any check, optional.
109+
* @param string $configurationPattern Pattern for the Configuration service name.
110+
* @param string $registerAliasMethodName Name of Configuration class method to
111+
* register alias.
112+
* @param string[] $aliasMap Map of alias to namespace.
113+
*
114+
* @since Support for bundle alias was added in Symfony 2.6
115+
*/
116+
public function __construct($driver, array $namespaces, array $managerParameters, $driverPattern, $enabledParameter = false, $configurationPattern = '', $registerAliasMethodName = '', array $aliasMap = array()) {
83117
$this->driver = $driver;
84118
$this->namespaces = $namespaces;
85119
$this->managerParameters = $managerParameters;
86120
$this->driverPattern = $driverPattern;
87121
$this->enabledParameter = $enabledParameter;
122+
if (count($aliasMap) && (!$configurationPattern || !$registerAliasMethodName)) {
123+
throw new \InvalidArgumentException('configurationPattern and registerAliasMethodName are required to register namespace alias');
124+
}
125+
$this->configurationPattern = $configurationPattern;
126+
$this->registerAliasMethodName = $registerAliasMethodName;
127+
$this->aliasMap = $aliasMap;
88128
}
89129

90130
/**
91-
* Register mappings with the metadata drivers.
131+
* Register mappings and alias with the metadata drivers.
92132
*
93133
* @param ContainerBuilder $container
94134
*/
@@ -99,39 +139,39 @@ public function process(ContainerBuilder $container)
99139
}
100140

101141
$mappingDriverDef = $this->getDriver($container);
102-
103142
$chainDriverDefService = $this->getChainDriverServiceName($container);
143+
// Definition for a Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain
104144
$chainDriverDef = $container->getDefinition($chainDriverDefService);
105145
foreach ($this->namespaces as $namespace) {
106146
$chainDriverDef->addMethodCall('addDriver', array($mappingDriverDef, $namespace));
107147
}
148+
149+
if (!count($this->aliasMap)) {
150+
return;
151+
}
152+
153+
$configurationServiceName = $this->getConfigurationServiceName($container);
154+
// Definition of the Doctrine\...\Configuration class specific to the Doctrine flavour.
155+
$configurationServiceDefinition = $container->getDefinition($configurationServiceName);
156+
foreach ($this->aliasMap as $alias => $namespace) {
157+
$configurationServiceDefinition->addMethodCall($this->registerAliasMethodName, array($alias, $namespace));
158+
}
108159
}
109160

110161
/**
111162
* Get the service name of the metadata chain driver that the mappings
112-
* should be registered with. The default implementation loops over the
113-
* managerParameters and applies the first non-empty parameter it finds to
114-
* the driverPattern.
163+
* should be registered with.
115164
*
116165
* @param ContainerBuilder $container
117166
*
118-
* @return string a service definition name
167+
* @return string The name of the chain driver service
119168
*
120169
* @throws ParameterNotFoundException if non of the managerParameters has a
121170
* non-empty value.
122171
*/
123172
protected function getChainDriverServiceName(ContainerBuilder $container)
124173
{
125-
foreach ($this->managerParameters as $param) {
126-
if ($container->hasParameter($param)) {
127-
$name = $container->getParameter($param);
128-
if ($name) {
129-
return sprintf($this->driverPattern, $name);
130-
}
131-
}
132-
}
133-
134-
throw new ParameterNotFoundException('None of the managerParameters resulted in a valid name');
174+
return sprintf($this->driverPattern, $this->getManagerName($container));
135175
}
136176

137177
/**
@@ -147,6 +187,48 @@ protected function getDriver(ContainerBuilder $container)
147187
return $this->driver;
148188
}
149189

190+
/**
191+
* Get the service name from the pattern and the configured manager name.
192+
*
193+
* @param ContainerBuilder $container
194+
*
195+
* @return string a service definition name
196+
*
197+
* @throws ParameterNotFoundException if none of the managerParameters has a
198+
* non-empty value.
199+
*/
200+
private function getConfigurationServiceName(ContainerBuilder $container)
201+
{
202+
return sprintf($this->configurationPattern, $this->getManagerName($container));
203+
}
204+
205+
/**
206+
* Determine the manager name.
207+
*
208+
* The default implementation loops over the managerParameters and returns
209+
* the first non-empty parameter.
210+
*
211+
* @param ContainerBuilder $container
212+
*
213+
* @return string The name of the active manager.
214+
*
215+
* @throws ParameterNotFoundException If none of the managerParameters is found in the container.
216+
*/
217+
private function getManagerName(ContainerBuilder $container)
218+
{
219+
foreach ($this->managerParameters as $param) {
220+
if ($container->hasParameter($param)) {
221+
$name = $container->getParameter($param);
222+
if ($name) {
223+
return $name;
224+
}
225+
}
226+
}
227+
228+
throw new ParameterNotFoundException('None of the managerParameters resulted in a valid name');
229+
230+
}
231+
150232
/**
151233
* Determine whether this mapping should be activated or not. This allows
152234
* to take this decision with the container builder available.
@@ -156,7 +238,7 @@ protected function getDriver(ContainerBuilder $container)
156238
*
157239
* @param ContainerBuilder $container
158240
*
159-
* @return bool whether this compiler pass really should register the mappings
241+
* @return bool whether this compiler pass really should register the mappings
160242
*/
161243
protected function enabled(ContainerBuilder $container)
162244
{

0 commit comments

Comments
 (0)