Skip to content

Commit

Permalink
use request context to determine locale
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Aug 28, 2013
1 parent 601edfc commit 2428963
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
30 changes: 27 additions & 3 deletions ContentAwareGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
class ContentAwareGenerator extends ProviderBasedGenerator
{
/**
* The locale to use when neither the parameters nor the request context
* indicate the locale to use.
*
* @var string
*/
protected $defaultLocale = null;

/**
* The content repository used to find content by it's id
* This can be used to specify a parameter content_id when generating urls
Expand Down Expand Up @@ -218,16 +226,32 @@ private function checkLocaleRequirement(SymfonyRoute $route, $locale)
*
* @param array $parameters the parameters determined by the route
*
* @return string|null the locale following of the parameters or any other
* information the router has available.
* @return string the locale following of the parameters or any other
* information the router has available. defaultLocale if no other locale
* can be determined.
*/
protected function getLocale($parameters)
{
if (isset($parameters['_locale'])) {
return $parameters['_locale'];
}

return null;
if ($this->getContext()->hasParameter('_locale')) {
return $this->getContext()->getParameter('_locale');
}

return $this->defaultLocale;
}

/**
* Overwrite the locale to be used by default if there is neither one in
* the parameters when building the route nor a request available (i.e. CLI).
*
* @param string $locale
*/
public function setDefaultLocale($locale)
{
$this->defaultLocale = $locale;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions ProviderBasedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Symfony\Cmf\Component\Routing;

use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Route as SymfonyRoute;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

use Symfony\Component\Routing\Generator\UrlGenerator;
use Psr\Log\LoggerInterface;

use Symfony\Cmf\Component\Routing\RouteProviderInterface;

use Psr\Log\LoggerInterface;

/**
* A Generator that uses a RouteProvider rather than a RouteCollection
*
Expand All @@ -28,6 +29,7 @@ public function __construct(RouteProviderInterface $provider, LoggerInterface $l
{
$this->provider = $provider;
$this->logger = $logger;
$this->context = new RequestContext();
}

/**
Expand Down
32 changes: 32 additions & 0 deletions Tests/Routing/ContentAwareGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,32 @@ public function testGenerateInvalidRoute()
$this->generator->generate($this->contentDocument);
}

public function testGetLocaleAttribute()
{
$this->generator->setDefaultLocale('en');

$attributes = array('_locale' => 'fr');
$this->assertEquals('fr', $this->generator->getLocale($attributes));
}

public function testGetLocaleDefault()
{
$this->generator->setDefaultLocale('en');

$attributes = array();
$this->assertEquals('en', $this->generator->getLocale($attributes));
}

public function testGetLocaleContext()
{
$this->generator->setDefaultLocale('en');

$this->generator->getContext()->setParameter('_locale', 'de');

$attributes = array();
$this->assertEquals('de', $this->generator->getLocale($attributes));
}

public function testSupports()
{
$this->assertTrue($this->generator->supports(''));
Expand All @@ -405,6 +431,12 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
{
return 'result_url';
}

// expose as public
public function getLocale($parameters)
{
return parent::getLocale($parameters);
}
}

class RouteAware implements RouteReferrersReadInterface
Expand Down

0 comments on commit 2428963

Please sign in to comment.