diff --git a/ContentAwareGenerator.php b/ContentAwareGenerator.php index d8d8545b..a6fda995 100644 --- a/ContentAwareGenerator.php +++ b/ContentAwareGenerator.php @@ -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 @@ -218,8 +226,9 @@ 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) { @@ -227,7 +236,22 @@ protected function getLocale($parameters) 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; } /** diff --git a/ProviderBasedGenerator.php b/ProviderBasedGenerator.php index bbdc5f15..e0d50b99 100644 --- a/ProviderBasedGenerator.php +++ b/ProviderBasedGenerator.php @@ -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 * @@ -28,6 +29,7 @@ public function __construct(RouteProviderInterface $provider, LoggerInterface $l { $this->provider = $provider; $this->logger = $logger; + $this->context = new RequestContext(); } /** diff --git a/Tests/Routing/ContentAwareGeneratorTest.php b/Tests/Routing/ContentAwareGeneratorTest.php index 66d7ff86..5b68d7dc 100644 --- a/Tests/Routing/ContentAwareGeneratorTest.php +++ b/Tests/Routing/ContentAwareGeneratorTest.php @@ -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('')); @@ -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