@@ -69,26 +69,66 @@ abstract class RegisterMappingsPass implements CompilerPassInterface
69
69
protected $ enabledParameter ;
70
70
71
71
/**
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
80
75
*/
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 ()) {
83
117
$ this ->driver = $ driver ;
84
118
$ this ->namespaces = $ namespaces ;
85
119
$ this ->managerParameters = $ managerParameters ;
86
120
$ this ->driverPattern = $ driverPattern ;
87
121
$ 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 ;
88
128
}
89
129
90
130
/**
91
- * Register mappings with the metadata drivers.
131
+ * Register mappings and alias with the metadata drivers.
92
132
*
93
133
* @param ContainerBuilder $container
94
134
*/
@@ -99,39 +139,39 @@ public function process(ContainerBuilder $container)
99
139
}
100
140
101
141
$ mappingDriverDef = $ this ->getDriver ($ container );
102
-
103
142
$ chainDriverDefService = $ this ->getChainDriverServiceName ($ container );
143
+ // Definition for a Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain
104
144
$ chainDriverDef = $ container ->getDefinition ($ chainDriverDefService );
105
145
foreach ($ this ->namespaces as $ namespace ) {
106
146
$ chainDriverDef ->addMethodCall ('addDriver ' , array ($ mappingDriverDef , $ namespace ));
107
147
}
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
+ }
108
159
}
109
160
110
161
/**
111
162
* 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.
115
164
*
116
165
* @param ContainerBuilder $container
117
166
*
118
- * @return string a service definition name
167
+ * @return string The name of the chain driver service
119
168
*
120
169
* @throws ParameterNotFoundException if non of the managerParameters has a
121
170
* non-empty value.
122
171
*/
123
172
protected function getChainDriverServiceName (ContainerBuilder $ container )
124
173
{
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 ));
135
175
}
136
176
137
177
/**
@@ -147,6 +187,48 @@ protected function getDriver(ContainerBuilder $container)
147
187
return $ this ->driver ;
148
188
}
149
189
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
+
150
232
/**
151
233
* Determine whether this mapping should be activated or not. This allows
152
234
* to take this decision with the container builder available.
@@ -156,7 +238,7 @@ protected function getDriver(ContainerBuilder $container)
156
238
*
157
239
* @param ContainerBuilder $container
158
240
*
159
- * @return bool whether this compiler pass really should register the mappings
241
+ * @return bool whether this compiler pass really should register the mappings
160
242
*/
161
243
protected function enabled (ContainerBuilder $ container )
162
244
{
0 commit comments