Skip to content

Commit 7606b70

Browse files
committed
fix locale handling in routing
1 parent 741ff58 commit 7606b70

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

DependencyInjection/CmfRoutingExtension.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,18 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
8383
}
8484
$container->setParameter($this->getAlias() . '.defined_templates_class', $controllerForTemplates);
8585
$container->setParameter($this->getAlias() . '.uri_filter_regexp', $config['uri_filter_regexp']);
86+
$locales = false;
87+
if (isset($config['locales'])) {
88+
$locales = $config['locales'];
89+
$container->setParameter($this->getAlias() . '.dynamic.locales', $locales);
90+
}
8691

8792
$loader->load('routing-dynamic.xml');
8893

8994
$hasProvider = false;
9095
$hasContentRepository = false;
9196
if (!empty($config['persistence']['phpcr']['enabled'])) {
92-
$this->loadPhpcrProvider($config['persistence']['phpcr'], $loader, $container);
97+
$this->loadPhpcrProvider($config['persistence']['phpcr'], $loader, $container, $locales);
9398
$hasProvider = true;
9499
$hasContentRepository = true;
95100
}
@@ -142,7 +147,7 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
142147
}
143148
}
144149

145-
public function loadPhpcrProvider($config, XmlFileLoader $loader, ContainerBuilder $container)
150+
public function loadPhpcrProvider($config, XmlFileLoader $loader, ContainerBuilder $container, $locales)
146151
{
147152
$loader->load('provider-phpcr.xml');
148153

@@ -156,9 +161,7 @@ public function loadPhpcrProvider($config, XmlFileLoader $loader, ContainerBuild
156161
$container->setAlias($this->getAlias() . '.route_provider', $this->getAlias() . '.phpcr_route_provider');
157162
$container->setAlias($this->getAlias() . '.content_repository', $this->getAlias() . '.phpcr_content_repository');
158163

159-
if (isset($config['locales']) && $config['locales']) {
160-
$container->setParameter($this->getAlias() . '.locales', $config['locales']);
161-
} else {
164+
if (!$locales) {
162165
$container->removeDefinition('cmf_routing.phpcrodm_route_locale_listener');
163166
}
164167

Resources/config/provider-phpcr.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<service id="cmf_routing.phpcrodm_route_locale_listener" class="%cmf_routing.phpcrodm_route_locale_listener_class%">
3434
<argument>%cmf_routing.dynamic.persistence.phpcr.route_basepath%</argument>
35-
<argument>%cmf_routing.locales%</argument>
35+
<argument>%cmf_routing.dynamic.locales%</argument>
3636
<tag name="doctrine_phpcr.event_listener" event="postLoad" />
3737
<tag name="doctrine_phpcr.event_listener" event="postPersist" />
3838
<tag name="doctrine_phpcr.event_listener" event="postMove" />

Tests/Functional/Routing/DynamicRouterTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,49 @@ public function testNoMatchingFormat()
163163
$this->router->matchRequest(Request::create('/format/48.xml'));
164164
}
165165

166+
public function testMatchLocale()
167+
{
168+
$route = new Route;
169+
$route->setPosition($this->getDm()->find(null, self::ROUTE_ROOT), 'de');
170+
$route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
171+
$this->getDm()->persist($route);
172+
$childroute = new Route;
173+
$childroute->setPosition($route, 'testroute');
174+
$this->getDm()->persist($childroute);
175+
$nolocale = new Route;
176+
$nolocale->setPosition($this->getDm()->find(null, self::ROUTE_ROOT), 'es');
177+
$nolocale->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
178+
$this->getDm()->persist($nolocale);
179+
$this->getDm()->flush();
180+
181+
$expected = array(
182+
'_controller' => 'testController',
183+
'_locale' => 'de',
184+
'_route' => self::ROUTE_ROOT . '/de'
185+
);
186+
$this->assertEquals(
187+
$expected,
188+
$this->router->match('/de')
189+
);
190+
$expected = array(
191+
'_locale' => 'de',
192+
'_route' => self::ROUTE_ROOT . '/de/testroute'
193+
);
194+
$this->assertEquals(
195+
$expected,
196+
$this->router->match('/de/testroute')
197+
);
198+
// es is not a configured locale
199+
$expected = array(
200+
'_controller' => 'testController',
201+
'_route' => self::ROUTE_ROOT . '/es'
202+
);
203+
$this->assertEquals(
204+
$expected,
205+
$this->router->match('/es')
206+
);
207+
}
208+
166209
public function testEnhanceControllerByAlias()
167210
{
168211
// put a redirect route

Tests/Resources/app/config/cmf_routing.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ cmf_routing:
1515
phpcr:
1616
enabled: true
1717
route_basepath: /test/routing
18+
locales:
19+
- en
20+
- de

0 commit comments

Comments
 (0)