Skip to content

Commit 0a66187

Browse files
committed
Merge pull request #80 from symfony-cmf/use-request-context
use request context to determine locale
2 parents c0f8fcb + 2428963 commit 0a66187

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

ContentAwareGenerator.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
*/
1717
class ContentAwareGenerator extends ProviderBasedGenerator
1818
{
19+
/**
20+
* The locale to use when neither the parameters nor the request context
21+
* indicate the locale to use.
22+
*
23+
* @var string
24+
*/
25+
protected $defaultLocale = null;
26+
1927
/**
2028
* The content repository used to find content by it's id
2129
* This can be used to specify a parameter content_id when generating urls
@@ -218,16 +226,32 @@ private function checkLocaleRequirement(SymfonyRoute $route, $locale)
218226
*
219227
* @param array $parameters the parameters determined by the route
220228
*
221-
* @return string|null the locale following of the parameters or any other
222-
* information the router has available.
229+
* @return string the locale following of the parameters or any other
230+
* information the router has available. defaultLocale if no other locale
231+
* can be determined.
223232
*/
224233
protected function getLocale($parameters)
225234
{
226235
if (isset($parameters['_locale'])) {
227236
return $parameters['_locale'];
228237
}
229238

230-
return null;
239+
if ($this->getContext()->hasParameter('_locale')) {
240+
return $this->getContext()->getParameter('_locale');
241+
}
242+
243+
return $this->defaultLocale;
244+
}
245+
246+
/**
247+
* Overwrite the locale to be used by default if there is neither one in
248+
* the parameters when building the route nor a request available (i.e. CLI).
249+
*
250+
* @param string $locale
251+
*/
252+
public function setDefaultLocale($locale)
253+
{
254+
$this->defaultLocale = $locale;
231255
}
232256

233257
/**

ProviderBasedGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Symfony\Cmf\Component\Routing;
44

5+
use Symfony\Component\Routing\Generator\UrlGenerator;
56
use Symfony\Component\Routing\Route as SymfonyRoute;
7+
use Symfony\Component\Routing\RequestContext;
68
use Symfony\Component\Routing\Exception\RouteNotFoundException;
79

8-
use Symfony\Component\Routing\Generator\UrlGenerator;
9-
use Psr\Log\LoggerInterface;
10-
1110
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
1211

12+
use Psr\Log\LoggerInterface;
13+
1314
/**
1415
* A Generator that uses a RouteProvider rather than a RouteCollection
1516
*
@@ -28,6 +29,7 @@ public function __construct(RouteProviderInterface $provider, LoggerInterface $l
2829
{
2930
$this->provider = $provider;
3031
$this->logger = $logger;
32+
$this->context = new RequestContext();
3133
}
3234

3335
/**

Tests/Routing/ContentAwareGeneratorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,32 @@ public function testGenerateInvalidRoute()
380380
$this->generator->generate($this->contentDocument);
381381
}
382382

383+
public function testGetLocaleAttribute()
384+
{
385+
$this->generator->setDefaultLocale('en');
386+
387+
$attributes = array('_locale' => 'fr');
388+
$this->assertEquals('fr', $this->generator->getLocale($attributes));
389+
}
390+
391+
public function testGetLocaleDefault()
392+
{
393+
$this->generator->setDefaultLocale('en');
394+
395+
$attributes = array();
396+
$this->assertEquals('en', $this->generator->getLocale($attributes));
397+
}
398+
399+
public function testGetLocaleContext()
400+
{
401+
$this->generator->setDefaultLocale('en');
402+
403+
$this->generator->getContext()->setParameter('_locale', 'de');
404+
405+
$attributes = array();
406+
$this->assertEquals('de', $this->generator->getLocale($attributes));
407+
}
408+
383409
public function testSupports()
384410
{
385411
$this->assertTrue($this->generator->supports(''));
@@ -405,6 +431,12 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
405431
{
406432
return 'result_url';
407433
}
434+
435+
// expose as public
436+
public function getLocale($parameters)
437+
{
438+
return parent::getLocale($parameters);
439+
}
408440
}
409441

410442
class RouteAware implements RouteReferrersReadInterface

0 commit comments

Comments
 (0)