Skip to content

Commit

Permalink
bug #31023 [Routing] Fix route URL generation in CLI context (X-Coder…
Browse files Browse the repository at this point in the history
…264)

This PR was squashed before being merged into the 4.2 branch (closes #31023).

Discussion
----------

[Routing] Fix route URL generation in CLI context

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30996
| License       | MIT
| Doc PR        | -

This fixes #30996 and makes URL generation in the CLI context behave the same as it does in the web context where the `LocaleListener` sets the default locale (to the router context).

The Travis CI failure is related to the fact that the constraint for `symfony/routing` should be bumped to `^4.2.6` in the composer.json of the FrameworkBundle (when it gets tagged).

Commits
-------

4a1ad4a5d6 [Routing] Fix route URL generation in CLI context
  • Loading branch information
fabpot committed Apr 22, 2019
2 parents d64b359 + 6c32dde commit 8d975c2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions Resources/config/routing.xml
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 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 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 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

0 comments on commit 8d975c2

Please sign in to comment.