Skip to content

[Routing] Fix route URL generation in CLI context #31023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<argument type="service" id="router.request_context" on-invalid="ignore" />
<argument type="service" id="parameter_bag" on-invalid="ignore" />
<argument type="service" id="logger" on-invalid="ignore" />
<argument>%kernel.default_locale%</argument>
<call method="setConfigCacheFactory">
<argument type="service" id="config_cache_factory" />
</call>
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Router extends BaseRouter implements WarmableInterface, ServiceSubscriberI
* @param ContainerInterface|null $parameters A ContainerInterface instance allowing to fetch parameters
* @param LoggerInterface|null $logger
*/
public function __construct(ContainerInterface $container, $resource, array $options = [], RequestContext $context = null, ContainerInterface $parameters = null, LoggerInterface $logger = null)
public function __construct(ContainerInterface $container, $resource, array $options = [], RequestContext $context = null, ContainerInterface $parameters = null, LoggerInterface $logger = null, string $defaultLocale = null)
{
$this->container = $container;
$this->resource = $resource;
Expand All @@ -58,6 +58,8 @@ public function __construct(ContainerInterface $container, $resource, array $opt
} else {
throw new \LogicException(sprintf('You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.', SymfonyContainerInterface::class, __METHOD__));
}

$this->defaultLocale = $defaultLocale;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ public function testGenerateWithServiceParamWithSfContainer()
$this->assertSame('"bar" == "bar"', $router->getRouteCollection()->get('foo')->getCondition());
}

public function testGenerateWithDefaultLocale()
{
$routes = new RouteCollection();

$route = new Route('');

$name = 'testFoo';

foreach (['hr' => '/test-hr', 'en' => '/test-en'] as $locale => $path) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setPath($path);
$routes->add($name.'.'.$locale, $localizedRoute);
}

$sc = $this->getServiceContainer($routes);

$router = new Router($sc, '', [], null, null, null, 'hr');

$this->assertSame('/test-hr', $router->generate($name));

$this->assertSame('/test-en', $router->generate($name, ['_locale' => 'en']));
$this->assertSame('/test-hr', $router->generate($name, ['_locale' => 'hr']));
}

public function testDefaultsPlaceholders()
{
$routes = new RouteCollection();
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "~3.4|~4.0",
"symfony/finder": "~3.4|~4.0",
"symfony/routing": "^4.1"
"symfony/routing": "^4.2.6"
},
"require-dev": {
"doctrine/cache": "~1.0",
Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Component/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Router implements RouterInterface, RequestMatcherInterface
*/
protected $logger;

/**
* @var string|null
*/
protected $defaultLocale;

/**
* @var ConfigCacheFactoryInterface|null
*/
Expand All @@ -90,13 +95,14 @@ class Router implements RouterInterface, RequestMatcherInterface
* @param RequestContext $context The context
* @param LoggerInterface $logger A logger instance
*/
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null)
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
{
$this->loader = $loader;
$this->resource = $resource;
$this->logger = $logger;
$this->context = $context ?: new RequestContext();
$this->setOptions($options);
$this->defaultLocale = $defaultLocale;
}

/**
Expand Down Expand Up @@ -321,7 +327,7 @@ public function getGenerator()
}

if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
$this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
$this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger, $this->defaultLocale);
} else {
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
function (ConfigCacheInterface $cache) {
Expand All @@ -340,7 +346,7 @@ function (ConfigCacheInterface $cache) {
require_once $cache->getPath();
}

$this->generator = new $this->options['generator_cache_class']($this->context, $this->logger);
$this->generator = new $this->options['generator_cache_class']($this->context, $this->logger, $this->defaultLocale);
}

if ($this->generator instanceof ConfigurableRequirementsInterface) {
Expand Down
103 changes: 101 additions & 2 deletions src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,82 @@ public function testGlobalParameterHasHigherPriorityThanDefault()
$this->assertSame('/app.php/de', $url);
}

public function testGenerateWithDefaultLocale()
{
$routes = new RouteCollection();

$route = new Route('');

$name = 'test';

foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setPath($path);
$routes->add($name.'.'.$locale, $localizedRoute);
}

$generator = $this->getGenerator($routes, [], null, 'hr');

$this->assertSame(
'http://localhost/app.php/foo',
$generator->generate($name, [], UrlGeneratorInterface::ABSOLUTE_URL)
);
}

public function testGenerateWithOverriddenParameterLocale()
{
$routes = new RouteCollection();

$route = new Route('');

$name = 'test';

foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setPath($path);
$routes->add($name.'.'.$locale, $localizedRoute);
}

$generator = $this->getGenerator($routes, [], null, 'hr');

$this->assertSame(
'http://localhost/app.php/bar',
$generator->generate($name, ['_locale' => 'en'], UrlGeneratorInterface::ABSOLUTE_URL)
);
}

public function testGenerateWithOverriddenParameterLocaleFromRequestContext()
{
$routes = new RouteCollection();

$route = new Route('');

$name = 'test';

foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setPath($path);
$routes->add($name.'.'.$locale, $localizedRoute);
}

$generator = $this->getGenerator($routes, [], null, 'hr');

$context = new RequestContext('/app.php');
$context->setParameter('_locale', 'en');
$generator->setContext($context);

$this->assertSame(
'http://localhost/app.php/bar',
$generator->generate($name, [], UrlGeneratorInterface::ABSOLUTE_URL)
);
}

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
Expand All @@ -171,6 +247,29 @@ public function testGenerateWithoutRoutes()
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
}

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
public function testGenerateWithInvalidLocale()
{
$routes = new RouteCollection();

$route = new Route('');

$name = 'test';

foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setPath($path);
$routes->add($name.'.'.$locale, $localizedRoute);
}

$generator = $this->getGenerator($routes, [], null, 'fr');
$generator->generate($name);
}

/**
* @expectedException \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
*/
Expand Down Expand Up @@ -720,15 +819,15 @@ public function provideLookAroundRequirementsInPath()
yield ['/app.php/bar/a/b/bam/c/d/e', '/bar/{foo}/bam/{baz}', '(?<!^).+'];
}

protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null)
protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null, string $defaultLocale = null)
{
$context = new RequestContext('/app.php');
foreach ($parameters as $key => $value) {
$method = 'set'.$key;
$context->$method($value);
}

return new UrlGenerator($routes, $context, $logger);
return new UrlGenerator($routes, $context, $logger, $defaultLocale);
}

protected function getRoutes($name, Route $route)
Expand Down