22
22
/**
23
23
* @author Philippo de Santis
24
24
* @author David Buchmann
25
+ * @author Wouter de Jong <[email protected] >
25
26
*/
26
27
class CmfRoutingExtension extends Extension
27
28
{
@@ -33,32 +34,35 @@ public function load(array $configs, ContainerBuilder $container)
33
34
$ config = $ this ->processConfiguration (new Configuration (), $ configs );
34
35
$ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ .'/../Resources/config ' ));
35
36
36
- if ($ config ['dynamic ' ]['enabled ' ]) {
37
- // load this even if no explicit enabled value but some configuration
37
+ if ($ this ->isConfigEnabled ($ container , $ config ['dynamic ' ])) {
38
38
$ this ->setupDynamicRouter ($ config ['dynamic ' ], $ container , $ loader );
39
39
}
40
40
41
- /* set up the chain router */
41
+ $ this ->setupChainRouter ($ config , $ container , $ loader );
42
+ $ this ->setupFormTypes ($ config , $ container , $ loader );
43
+
44
+ $ loader ->load ('validators.xml ' );
45
+ }
46
+
47
+ private function setupChainRouter (array $ config , ContainerBuilder $ container , LoaderInterface $ loader )
48
+ {
42
49
$ loader ->load ('routing-chain.xml ' );
43
- $ container ->setParameter ($ this ->getAlias () . '.replace_symfony_router ' , $ config ['chain ' ]['replace_symfony_router ' ]);
50
+
51
+ $ container ->setParameter ('cmf_routing.replace_symfony_router ' , $ config ['chain ' ]['replace_symfony_router ' ]);
44
52
45
53
// add the routers defined in the configuration mapping
46
- $ router = $ container ->getDefinition ($ this -> getAlias () . ' .router ' );
54
+ $ router = $ container ->getDefinition (' cmf_routing .router ' );
47
55
foreach ($ config ['chain ' ]['routers_by_id ' ] as $ id => $ priority ) {
48
56
$ router ->addMethodCall ('add ' , array (new Reference ($ id ), trim ($ priority )));
49
57
}
50
-
51
- $ this ->setupFormTypes ($ config , $ container , $ loader );
52
-
53
- $ loader ->load ('validators.xml ' );
54
58
}
55
59
56
- public function setupFormTypes (array $ config , ContainerBuilder $ container , LoaderInterface $ loader )
60
+ private function setupFormTypes (array $ config , ContainerBuilder $ container , LoaderInterface $ loader )
57
61
{
58
62
$ loader ->load ('form-type.xml ' );
59
63
60
64
if (isset ($ config ['dynamic ' ])) {
61
- $ routeTypeTypeDefinition = $ container ->getDefinition ($ this -> getAlias () . ' .route_type_form_type ' );
65
+ $ routeTypeTypeDefinition = $ container ->getDefinition (' cmf_routing .route_type_form_type ' );
62
66
63
67
foreach (array_keys ($ config ['dynamic ' ]['controllers_by_type ' ]) as $ routeType ) {
64
68
$ routeTypeTypeDefinition ->addMethodCall ('addRouteType ' , array ($ routeType ));
@@ -75,102 +79,81 @@ public function setupFormTypes(array $config, ContainerBuilder $container, Loade
75
79
*/
76
80
private function setupDynamicRouter (array $ config , ContainerBuilder $ container , LoaderInterface $ loader )
77
81
{
82
+ $ loader ->load ('routing-dynamic.xml ' );
83
+
78
84
// strip whitespace (XML support)
79
85
foreach (array ('controllers_by_type ' , 'controllers_by_class ' , 'templates_by_class ' , 'route_filters_by_id ' ) as $ option ) {
80
- $ config [$ option ] = array_map (function ($ value ) {
81
- return trim ($ value );
82
- }, $ config [$ option ]);
86
+ $ config [$ option ] = array_map ('trim ' , $ config [$ option ]);
83
87
}
84
88
85
89
$ defaultController = $ config ['default_controller ' ];
86
90
if (null === $ defaultController ) {
87
91
$ defaultController = $ config ['generic_controller ' ];
88
92
}
89
- $ container ->setParameter ($ this ->getAlias () . '.default_controller ' , $ defaultController );
90
- $ container ->setParameter ($ this ->getAlias () . '.generic_controller ' , $ config ['generic_controller ' ]);
91
- $ container ->setParameter ($ this ->getAlias () . '.controllers_by_type ' , $ config ['controllers_by_type ' ]);
92
- $ container ->setParameter ($ this ->getAlias () . '.controllers_by_class ' , $ config ['controllers_by_class ' ]);
93
- $ container ->setParameter ($ this ->getAlias () . '.templates_by_class ' , $ config ['templates_by_class ' ]);
94
- $ container ->setParameter ($ this ->getAlias () . '.uri_filter_regexp ' , $ config ['uri_filter_regexp ' ]);
95
- $ container ->setParameter ($ this ->getAlias () . '.route_collection_limit ' , $ config ['route_collection_limit ' ]);
96
-
97
- $ locales = empty ($ config ['locales ' ]) ? array () : $ config ['locales ' ];
98
- $ container ->setParameter ($ this ->getAlias () . '.dynamic.locales ' , $ locales );
99
- if (count ($ locales ) === 0 && $ config ['auto_locale_pattern ' ]) {
93
+ $ container ->setParameter ('cmf_routing.default_controller ' , $ defaultController );
94
+
95
+ $ locales = $ config ['locales ' ];
96
+ if (0 === count ($ locales ) && $ config ['auto_locale_pattern ' ]) {
100
97
throw new InvalidConfigurationException ('It makes no sense to activate auto_locale_pattern when no locales are configured. ' );
101
98
}
102
- $ container ->setParameter ($ this ->getAlias () . '.dynamic.auto_locale_pattern ' , $ config ['auto_locale_pattern ' ]);
103
-
104
- $ container ->setParameter ($ this ->getAlias () . '.dynamic.limit_candidates ' , $ config ['limit_candidates ' ]);
105
99
106
- $ loader ->load ('routing-dynamic.xml ' );
100
+ $ this ->configureParameters ($ container , $ config , array (
101
+ 'generic_controller ' => 'generic_controller ' ,
102
+ 'controllers_by_type ' => 'controllers_by_type ' ,
103
+ 'controllers_by_class ' => 'controllers_by_class ' ,
104
+ 'templates_by_class ' => 'templates_by_class ' ,
105
+ 'uri_filter_regexp ' => 'uri_filter_regexp ' ,
106
+ 'route_collection_limit ' => 'route_collection_limit ' ,
107
+ 'limit_candidates ' => 'dynamic.limit_candidates ' ,
108
+ 'locales ' => 'dynamic.locales ' ,
109
+ 'auto_locale_pattern ' => 'dynamic.auto_locale_pattern ' ,
110
+ ));
107
111
108
112
$ hasProvider = false ;
109
113
$ hasContentRepository = false ;
110
- if ($ config ['persistence ' ]['phpcr ' ]['enabled ' ] && $ config ['persistence ' ]['orm ' ]['enabled ' ]) {
111
- throw new InvalidConfigurationException ('You can only enable either phpcr or orm, not both. ' );
112
- }
113
-
114
114
if ($ config ['persistence ' ]['phpcr ' ]['enabled ' ]) {
115
115
$ this ->loadPhpcrProvider ($ config ['persistence ' ]['phpcr ' ], $ loader , $ container , $ locales , $ config ['match_implicit_locale ' ]);
116
- $ hasProvider = true ;
117
- $ hasContentRepository = true ;
116
+ $ hasProvider = $ hasContentRepository = true ;
118
117
}
119
118
120
119
if ($ config ['persistence ' ]['orm ' ]['enabled ' ]) {
121
120
$ this ->loadOrmProvider ($ config ['persistence ' ]['orm ' ], $ loader , $ container , $ locales , $ config ['match_implicit_locale ' ]);
122
- $ hasProvider = true ;
123
- $ hasContentRepository = true ;
121
+ $ hasProvider = $ hasContentRepository = true ;
124
122
}
125
123
126
124
if (isset ($ config ['route_provider_service_id ' ])) {
127
- $ container ->setAlias ($ this -> getAlias () . ' .route_provider ' , $ config ['route_provider_service_id ' ]);
125
+ $ container ->setAlias (' cmf_routing .route_provider ' , $ config ['route_provider_service_id ' ]);
128
126
$ hasProvider = true ;
129
127
}
128
+
130
129
if (!$ hasProvider ) {
131
130
throw new InvalidConfigurationException ('When the dynamic router is enabled, you need to either enable one of the persistence layers or set the cmf_routing.dynamic.route_provider_service_id option ' );
132
131
}
132
+
133
133
if (isset ($ config ['content_repository_service_id ' ])) {
134
- $ container ->setAlias ($ this -> getAlias () . ' .content_repository ' , $ config ['content_repository_service_id ' ]);
134
+ $ container ->setAlias (' cmf_routing .content_repository ' , $ config ['content_repository_service_id ' ]);
135
135
$ hasContentRepository = true ;
136
136
}
137
+
137
138
// content repository is optional
138
139
if ($ hasContentRepository ) {
139
- $ generator = $ container ->getDefinition ($ this ->getAlias () . '.generator ' );
140
- $ generator ->addMethodCall ('setContentRepository ' , array (
141
- new Reference ($ this ->getAlias () . '.content_repository ' ),
142
- ));
140
+ $ generator = $ container ->getDefinition ('cmf_routing.generator ' );
141
+ $ generator ->addMethodCall ('setContentRepository ' , array (new Reference ('cmf_routing.content_repository ' )));
143
142
}
144
143
145
- $ dynamic = $ container ->getDefinition ($ this -> getAlias () . ' .dynamic_router ' );
144
+ $ dynamic = $ container ->getDefinition (' cmf_routing .dynamic_router ' );
146
145
147
146
// if any mappings are defined, set the respective route enhancer
148
- if (!empty ($ config ['controllers_by_type ' ])) {
149
- $ dynamic ->addMethodCall (
150
- 'addRouteEnhancer ' ,
151
- array (
152
- new Reference ($ this ->getAlias () . '.enhancer.controllers_by_type ' ),
153
- 60
154
- )
155
- );
147
+ if (count ($ config ['controllers_by_type ' ]) > 0 ) {
148
+ $ dynamic ->addMethodCall ('addRouteEnhancer ' , array (new Reference ('cmf_routing.enhancer.controllers_by_type ' ), 60 ));
156
149
}
157
- if (!empty ($ config ['controllers_by_class ' ])) {
158
- $ dynamic ->addMethodCall (
159
- 'addRouteEnhancer ' ,
160
- array (
161
- new Reference ($ this ->getAlias () . '.enhancer.controllers_by_class ' ),
162
- 50
163
- )
164
- );
150
+
151
+ if (count ($ config ['controllers_by_class ' ]) > 0 ) {
152
+ $ dynamic ->addMethodCall ('addRouteEnhancer ' , array (new Reference ('cmf_routing.enhancer.controllers_by_class ' ), 50 ));
165
153
}
166
- if (!empty ($ config ['templates_by_class ' ])) {
167
- $ dynamic ->addMethodCall (
168
- 'addRouteEnhancer ' ,
169
- array (
170
- new Reference ($ this ->getAlias () . '.enhancer.templates_by_class ' ),
171
- 40
172
- )
173
- );
154
+
155
+ if (count ($ config ['templates_by_class ' ]) > 0 ) {
156
+ $ dynamic ->addMethodCall ('addRouteEnhancer ' , array (new Reference ('cmf_routing.enhancer.templates_by_class ' ), 40 ));
174
157
175
158
/*
176
159
* The CoreBundle prepends the controller from ContentBundle if the
@@ -184,46 +167,29 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
184
167
throw new InvalidConfigurationException ('If you want to configure templates_by_class, you need to configure the generic_controller option. ' );
185
168
}
186
169
187
- if (is_string ($ config ['generic_controller ' ])) {
188
- // if the content class defines the template, we also need to make sure we use the generic controller for those routes
189
- $ controllerForTemplates = array ();
190
- foreach ($ config ['templates_by_class ' ] as $ key => $ value ) {
191
- $ controllerForTemplates [$ key ] = $ config ['generic_controller ' ];
192
- }
193
-
194
- $ definition = $ container ->getDefinition ($ this ->getAlias () . '.enhancer.controller_for_templates_by_class ' );
195
- $ definition ->replaceArgument (2 , $ controllerForTemplates );
196
-
197
- $ dynamic ->addMethodCall (
198
- 'addRouteEnhancer ' ,
199
- array (
200
- new Reference ($ this ->getAlias () . '.enhancer.controller_for_templates_by_class ' ),
201
- 30
202
- )
203
- );
170
+ // if the content class defines the template, we also need to make sure we use the generic controller for those routes
171
+ $ controllerForTemplates = array ();
172
+ foreach ($ config ['templates_by_class ' ] as $ key => $ value ) {
173
+ $ controllerForTemplates [$ key ] = $ config ['generic_controller ' ];
204
174
}
175
+
176
+ $ definition = $ container ->getDefinition ('cmf_routing.enhancer.controller_for_templates_by_class ' );
177
+ $ definition ->replaceArgument (2 , $ controllerForTemplates );
178
+
179
+ $ dynamic ->addMethodCall ('addRouteEnhancer ' , array (new Reference ('cmf_routing.enhancer.controller_for_templates_by_class ' ), 30 ));
205
180
}
206
- if (!empty ($ config ['generic_controller ' ]) && $ config ['generic_controller ' ] !== $ defaultController ) {
207
- $ dynamic ->addMethodCall (
208
- 'addRouteEnhancer ' ,
209
- array (
210
- new Reference ($ this ->getAlias () . '.enhancer.explicit_template ' ),
211
- 10
212
- )
213
- );
181
+
182
+ if (null !== $ config ['generic_controller ' ] && $ defaultController !== $ config ['generic_controller ' ]) {
183
+ $ dynamic ->addMethodCall ('addRouteEnhancer ' , array (new Reference ('cmf_routing.enhancer.explicit_template ' ), 10 ));
214
184
}
215
- if ($ defaultController ) {
216
- $ dynamic ->addMethodCall (
217
- 'addRouteEnhancer ' ,
218
- array (
219
- new Reference ($ this ->getAlias () . '.enhancer.default_controller ' ),
220
- -100
221
- )
222
- );
185
+
186
+ if (null !== $ defaultController ) {
187
+ $ dynamic ->addMethodCall ('addRouteEnhancer ' , array (new Reference ('cmf_routing.enhancer.default_controller ' ), -100 ));
223
188
}
224
189
225
- if (!empty ($ config ['route_filters_by_id ' ])) {
226
- $ matcher = $ container ->getDefinition ($ this ->getAlias () . '.nested_matcher ' );
190
+ if (count ($ config ['route_filters_by_id ' ]) > 0 ) {
191
+ $ matcher = $ container ->getDefinition ('cmf_routing.nested_matcher ' );
192
+
227
193
foreach ($ config ['route_filters_by_id ' ] as $ id => $ priority ) {
228
194
$ matcher ->addMethodCall ('addRouteFilter ' , array (new Reference ($ id ), $ priority ));
229
195
}
@@ -232,49 +198,25 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
232
198
$ dynamic ->replaceArgument (2 , new Reference ($ config ['url_generator ' ]));
233
199
}
234
200
235
- public function loadPhpcrProvider ($ config , XmlFileLoader $ loader , ContainerBuilder $ container , $ locales , $ matchImplicitLocale )
201
+ private function loadPhpcrProvider ($ config , XmlFileLoader $ loader , ContainerBuilder $ container , array $ locales , $ matchImplicitLocale )
236
202
{
237
203
$ loader ->load ('provider-phpcr.xml ' );
238
204
239
- $ container ->setParameter ($ this ->getAlias () . '.backend_type_phpcr ' , true );
240
-
241
- $ container ->setParameter (
242
- $ this ->getAlias () . '.dynamic.persistence.phpcr.route_basepaths ' ,
243
- array_values (array_unique ($ config ['route_basepaths ' ]))
244
- );
205
+ $ container ->setParameter ('cmf_routing.backend_type_phpcr ' , true );
206
+ $ container ->setParameter ('cmf_routing.dynamic.persistence.phpcr.route_basepaths ' , array_values (array_unique ($ config ['route_basepaths ' ])));
245
207
246
208
/**
247
209
* @deprecated The cmf_routing.dynamic.persistence.phpcr.route_basepath parameter is deprecated as of version 1.4 and will be removed in 2.0. Use the cmf_routing.dynamic.persistence.phpcr.route_basepaths parameter instead.
248
210
*/
249
- $ container ->setParameter (
250
- $ this ->getAlias () . '.dynamic.persistence.phpcr.route_basepath ' ,
251
- reset ($ config ['route_basepaths ' ])
252
- );
253
-
254
- $ container ->setParameter (
255
- $ this ->getAlias () . '.dynamic.persistence.phpcr.content_basepath ' ,
256
- $ config ['content_basepath ' ]
257
- );
258
-
259
- $ container ->setParameter (
260
- $ this ->getAlias () . '.dynamic.persistence.phpcr.manager_name ' ,
261
- $ config ['manager_name ' ]
262
- );
263
-
264
- $ container ->setAlias (
265
- $ this ->getAlias () . '.route_provider ' ,
266
- $ this ->getAlias () . '.phpcr_route_provider '
267
- );
268
- $ container ->setAlias (
269
- $ this ->getAlias () . '.content_repository ' ,
270
- $ this ->getAlias () . '.phpcr_content_repository '
271
- );
272
-
273
- if (!$ locales ) {
274
- $ container ->removeDefinition ($ this ->getAlias () . '.phpcrodm_route_locale_listener ' );
211
+ $ container ->setParameter ('cmf_routing.dynamic.persistence.phpcr.route_basepath ' , reset ($ config ['route_basepaths ' ]));
212
+ $ container ->setParameter ('cmf_routing.dynamic.persistence.phpcr.content_basepath ' , $ config ['content_basepath ' ]);
213
+ $ container ->setParameter ('cmf_routing.dynamic.persistence.phpcr.manager_name ' , $ config ['manager_name ' ]);
214
+
215
+ if (0 === count ($ locales )) {
216
+ $ container ->removeDefinition ('cmf_routing.phpcrodm_route_locale_listener ' );
275
217
} elseif (!$ matchImplicitLocale ) {
276
218
// remove all but the prefixes configuration from the service definition.
277
- $ definition = $ container ->getDefinition ($ this -> getAlias () . ' .phpcr_candidates_prefix ' );
219
+ $ definition = $ container ->getDefinition (' cmf_routing .phpcr_candidates_prefix ' );
278
220
$ definition ->setArguments (array ($ definition ->getArgument (0 )));
279
221
}
280
222
@@ -283,38 +225,46 @@ public function loadPhpcrProvider($config, XmlFileLoader $loader, ContainerBuild
283
225
}
284
226
}
285
227
286
- public function loadSonataPhpcrAdmin ($ config , XmlFileLoader $ loader , ContainerBuilder $ container )
228
+ private function loadSonataPhpcrAdmin ($ config , XmlFileLoader $ loader , ContainerBuilder $ container )
287
229
{
288
230
$ bundles = $ container ->getParameter ('kernel.bundles ' );
289
231
if ('auto ' === $ config ['use_sonata_admin ' ] && !isset ($ bundles ['SonataDoctrinePHPCRAdminBundle ' ])) {
290
232
return ;
291
233
}
292
234
293
- $ basePath = $ config ['admin_basepath ' ] ?: reset ($ config ['route_basepaths ' ]);
294
- $ container ->setParameter ($ this ->getAlias () . '.dynamic.persistence.phpcr.admin_basepath ' , $ basePath );
295
-
296
235
$ loader ->load ('admin-phpcr.xml ' );
297
236
237
+ $ basePath = $ config ['admin_basepath ' ] ?: reset ($ config ['route_basepaths ' ]);
238
+ $ container ->setParameter ('cmf_routing.dynamic.persistence.phpcr.admin_basepath ' , $ basePath );
239
+
298
240
if ($ config ['enable_initializer ' ]) {
299
241
$ loader ->load ('initializer-phpcr.xml ' );
300
242
}
301
243
}
302
244
303
- public function loadOrmProvider ($ config , XmlFileLoader $ loader , ContainerBuilder $ container , $ matchImplicitLocale )
245
+ private function loadOrmProvider ($ config , XmlFileLoader $ loader , ContainerBuilder $ container , $ matchImplicitLocale )
304
246
{
305
- $ container ->setParameter ($ this ->getAlias () . '.dynamic.persistence.orm.manager_name ' , $ config ['manager_name ' ]);
306
- $ container ->setParameter ($ this ->getAlias () . '.backend_type_orm ' , true );
307
247
$ loader ->load ('provider-orm.xml ' );
308
248
249
+ $ container ->setParameter ('cmf_routing.backend_type_orm ' , true );
250
+ $ container ->setParameter ('cmf_routing.dynamic.persistence.orm.manager_name ' , $ config ['manager_name ' ]);
251
+
309
252
if (!$ matchImplicitLocale ) {
310
253
// remove the locales argument from the candidates
311
- $ definition = $ container ->getDefinition ($ this ->getAlias () . '.orm_candidates ' );
312
- $ definition ->setArguments (array ());
254
+ $ container ->getDefinition ('cmf_routing.orm_candidates ' )->setArguments (array ());
255
+ }
256
+ }
257
+
258
+ /**
259
+ * @param ContainerBuilder $container The container builder
260
+ * @param array $config The config array
261
+ * @param array $settingToParameter An array with setting to parameter mappings (key = setting, value = parameter name without alias prefix)
262
+ */
263
+ private function configureParameters (ContainerBuilder $ container , array $ config , array $ settingToParameter )
264
+ {
265
+ foreach ($ settingToParameter as $ setting => $ parameter ) {
266
+ $ container ->setParameter ('cmf_routing. ' .$ parameter , $ config [$ setting ]);
313
267
}
314
- $ container ->setAlias (
315
- $ this ->getAlias () . '.content_repository ' ,
316
- $ this ->getAlias () . '.orm_content_repository '
317
- );
318
268
}
319
269
320
270
/**
0 commit comments