Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6faae44

Browse files
committed
Fix route URL generation in CLI context
1 parent c248646 commit 6faae44

File tree

5 files changed

+152
-16
lines changed

5 files changed

+152
-16
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<argument type="service" id="router.request_context" on-invalid="ignore" />
7272
<argument type="service" id="parameter_bag" on-invalid="ignore" />
7373
<argument type="service" id="logger" on-invalid="ignore" />
74+
<argument>%kernel.default_locale%</argument>
7475
<call method="setConfigCacheFactory">
7576
<argument type="service" id="config_cache_factory" />
7677
</call>

src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ class Router extends BaseRouter implements WarmableInterface, ServiceSubscriberI
3636
private $paramFetcher;
3737

3838
/**
39-
* @param ContainerInterface $container A ContainerInterface instance
40-
* @param mixed $resource The main resource to load
41-
* @param array $options An array of options
42-
* @param RequestContext $context The context
43-
* @param ContainerInterface|null $parameters A ContainerInterface instance allowing to fetch parameters
39+
* @param ContainerInterface $container A ContainerInterface instance
40+
* @param mixed $resource The main resource to load
41+
* @param array $options An array of options
42+
* @param RequestContext $context The context
43+
* @param ContainerInterface|null $parameters A ContainerInterface instance allowing to fetch parameters
4444
* @param LoggerInterface|null $logger
45+
* @param string|null $defaultLocale The default locale
4546
*/
46-
public function __construct(ContainerInterface $container, $resource, array $options = [], RequestContext $context = null, ContainerInterface $parameters = null, LoggerInterface $logger = null)
47+
public function __construct(ContainerInterface $container, $resource, array $options = [], RequestContext $context = null, ContainerInterface $parameters = null, LoggerInterface $logger = null, ?string $defaultLocale = null)
4748
{
4849
$this->container = $container;
4950
$this->resource = $resource;
@@ -58,6 +59,8 @@ public function __construct(ContainerInterface $container, $resource, array $opt
5859
} else {
5960
throw new \LogicException(sprintf('You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.', SymfonyContainerInterface::class, __METHOD__));
6061
}
62+
63+
$this->defaultLocale = $defaultLocale;
6164
}
6265

6366
/**

src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,32 @@ public function testGenerateWithServiceParamWithSfContainer()
8282
$this->assertSame('"bar" == "bar"', $router->getRouteCollection()->get('foo')->getCondition());
8383
}
8484

85+
public function testGenerateWithDefaultLocale(): void
86+
{
87+
$routes = new RouteCollection();
88+
89+
$route = new Route('');
90+
91+
$name = 'testFoo';
92+
93+
foreach (['hr' => '/test-hr', 'en' => '/test-en'] as $locale => $path) {
94+
$localizedRoute = clone $route;
95+
$localizedRoute->setDefault('_locale', $locale);
96+
$localizedRoute->setDefault('_canonical_route', $name);
97+
$localizedRoute->setPath($path);
98+
$routes->add($name.'.'.$locale, $localizedRoute);
99+
}
100+
101+
$sc = $this->getServiceContainer($routes);
102+
103+
$router = new Router($sc, '', [], null, null, null, 'hr');
104+
105+
$this->assertSame('/test-hr', $router->generate($name));
106+
107+
$this->assertSame('/test-en', $router->generate($name, ['_locale' => 'en']));
108+
$this->assertSame('/test-hr', $router->generate($name, ['_locale' => 'hr']));
109+
}
110+
85111
public function testDefaultsPlaceholders()
86112
{
87113
$routes = new RouteCollection();

src/Symfony/Component/Routing/Router.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class Router implements RouterInterface, RequestMatcherInterface
7373
*/
7474
protected $logger;
7575

76+
/**
77+
* @var string|null
78+
*/
79+
protected $defaultLocale;
80+
7681
/**
7782
* @var ConfigCacheFactoryInterface|null
7883
*/
@@ -84,19 +89,21 @@ class Router implements RouterInterface, RequestMatcherInterface
8489
private $expressionLanguageProviders = [];
8590

8691
/**
87-
* @param LoaderInterface $loader A LoaderInterface instance
88-
* @param mixed $resource The main resource to load
89-
* @param array $options An array of options
90-
* @param RequestContext $context The context
91-
* @param LoggerInterface $logger A logger instance
92+
* @param LoaderInterface $loader A LoaderInterface instance
93+
* @param mixed $resource The main resource to load
94+
* @param array $options An array of options
95+
* @param RequestContext $context The context
96+
* @param LoggerInterface $logger A logger instance
97+
* @param string|null $defaultLocale The default locale
9298
*/
93-
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null)
99+
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, ?string $defaultLocale = null)
94100
{
95101
$this->loader = $loader;
96102
$this->resource = $resource;
97103
$this->logger = $logger;
98104
$this->context = $context ?: new RequestContext();
99105
$this->setOptions($options);
106+
$this->defaultLocale = $defaultLocale;
100107
}
101108

102109
/**
@@ -321,7 +328,7 @@ public function getGenerator()
321328
}
322329

323330
if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
324-
$this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
331+
$this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger, $this->defaultLocale);
325332
} else {
326333
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
327334
function (ConfigCacheInterface $cache) {
@@ -340,7 +347,7 @@ function (ConfigCacheInterface $cache) {
340347
require_once $cache->getPath();
341348
}
342349

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

346353
if ($this->generator instanceof ConfigurableRequirementsInterface) {

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,82 @@ public function testGlobalParameterHasHigherPriorityThanDefault()
162162
$this->assertSame('/app.php/de', $url);
163163
}
164164

165+
public function testGenerateWithDefaultLocale(): void
166+
{
167+
$routes = new RouteCollection();
168+
169+
$route = new Route('');
170+
171+
$name = 'test';
172+
173+
foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
174+
$localizedRoute = clone $route;
175+
$localizedRoute->setDefault('_locale', $locale);
176+
$localizedRoute->setDefault('_canonical_route', $name);
177+
$localizedRoute->setPath($path);
178+
$routes->add($name.'.'.$locale, $localizedRoute);
179+
}
180+
181+
$generator = $this->getGenerator($routes, [], null, 'hr');
182+
183+
$this->assertSame(
184+
'http://localhost/app.php/foo',
185+
$generator->generate($name, [], UrlGeneratorInterface::ABSOLUTE_URL)
186+
);
187+
}
188+
189+
public function testGenerateWithOverriddenParameterLocale(): void
190+
{
191+
$routes = new RouteCollection();
192+
193+
$route = new Route('');
194+
195+
$name = 'test';
196+
197+
foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
198+
$localizedRoute = clone $route;
199+
$localizedRoute->setDefault('_locale', $locale);
200+
$localizedRoute->setDefault('_canonical_route', $name);
201+
$localizedRoute->setPath($path);
202+
$routes->add($name.'.'.$locale, $localizedRoute);
203+
}
204+
205+
$generator = $this->getGenerator($routes, [], null, 'hr');
206+
207+
$this->assertSame(
208+
'http://localhost/app.php/bar',
209+
$generator->generate($name, ['_locale' => 'en'], UrlGeneratorInterface::ABSOLUTE_URL)
210+
);
211+
}
212+
213+
public function testGenerateWithOverriddenParameterLocaleFromRequestContext(): void
214+
{
215+
$routes = new RouteCollection();
216+
217+
$route = new Route('');
218+
219+
$name = 'test';
220+
221+
foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
222+
$localizedRoute = clone $route;
223+
$localizedRoute->setDefault('_locale', $locale);
224+
$localizedRoute->setDefault('_canonical_route', $name);
225+
$localizedRoute->setPath($path);
226+
$routes->add($name.'.'.$locale, $localizedRoute);
227+
}
228+
229+
$generator = $this->getGenerator($routes, [], null, 'hr');
230+
231+
$context = new RequestContext('/app.php');
232+
$context->setParameter('_locale', 'en');
233+
$generator->setContext($context);
234+
235+
$this->assertSame(
236+
'http://localhost/app.php/bar',
237+
$generator->generate($name, [], UrlGeneratorInterface::ABSOLUTE_URL)
238+
);
239+
}
240+
165241
/**
166242
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
167243
*/
@@ -171,6 +247,29 @@ public function testGenerateWithoutRoutes()
171247
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
172248
}
173249

250+
/**
251+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
252+
*/
253+
public function testGenerateWithInvalidLocale(): void
254+
{
255+
$routes = new RouteCollection();
256+
257+
$route = new Route('');
258+
259+
$name = 'test';
260+
261+
foreach (['hr' => '/foo', 'en' => '/bar'] as $locale => $path) {
262+
$localizedRoute = clone $route;
263+
$localizedRoute->setDefault('_locale', $locale);
264+
$localizedRoute->setDefault('_canonical_route', $name);
265+
$localizedRoute->setPath($path);
266+
$routes->add($name.'.'.$locale, $localizedRoute);
267+
}
268+
269+
$generator = $this->getGenerator($routes, [], null, 'fr');
270+
$generator->generate($name);
271+
}
272+
174273
/**
175274
* @expectedException \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
176275
*/
@@ -720,15 +819,15 @@ public function provideLookAroundRequirementsInPath()
720819
yield ['/app.php/bar/a/b/bam/c/d/e', '/bar/{foo}/bam/{baz}', '(?<!^).+'];
721820
}
722821

723-
protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null)
822+
protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null, ?string $defaultLocale = null)
724823
{
725824
$context = new RequestContext('/app.php');
726825
foreach ($parameters as $key => $value) {
727826
$method = 'set'.$key;
728827
$context->$method($value);
729828
}
730829

731-
return new UrlGenerator($routes, $context, $logger);
830+
return new UrlGenerator($routes, $context, $logger, $defaultLocale);
732831
}
733832

734833
protected function getRoutes($name, Route $route)

0 commit comments

Comments
 (0)