Skip to content

Commit 665c743

Browse files
Merge branch '4.2'
* 4.2: fixed constraint [Routing] Fix route URL generation in CLI context fix math depth handler
2 parents 1a859d2 + 36ac088 commit 665c743

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

Router.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class Router implements RouterInterface, RequestMatcherInterface
8080
*/
8181
protected $logger;
8282

83+
/**
84+
* @var string|null
85+
*/
86+
protected $defaultLocale;
87+
8388
/**
8489
* @var ConfigCacheFactoryInterface|null
8590
*/
@@ -97,13 +102,14 @@ class Router implements RouterInterface, RequestMatcherInterface
97102
* @param RequestContext $context The context
98103
* @param LoggerInterface $logger A logger instance
99104
*/
100-
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null)
105+
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
101106
{
102107
$this->loader = $loader;
103108
$this->resource = $resource;
104109
$this->logger = $logger;
105110
$this->context = $context ?: new RequestContext();
106111
$this->setOptions($options);
112+
$this->defaultLocale = $defaultLocale;
107113
}
108114

109115
/**
@@ -345,7 +351,7 @@ public function getGenerator()
345351
if ($compiled) {
346352
$routes = (new CompiledUrlGeneratorDumper($routes))->getCompiledRoutes();
347353
}
348-
$this->generator = new $this->options['generator_class']($routes, $this->context, $this->logger);
354+
$this->generator = new $this->options['generator_class']($routes, $this->context, $this->logger, $this->defaultLocale);
349355
} else {
350356
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
351357
function (ConfigCacheInterface $cache) {
@@ -367,7 +373,7 @@ function (ConfigCacheInterface $cache) {
367373
require_once $cache->getPath();
368374
}
369375

370-
$this->generator = new $this->options['generator_cache_class']($this->context, $this->logger);
376+
$this->generator = new $this->options['generator_cache_class']($this->context, $this->logger, $this->defaultLocale);
371377
}
372378
}
373379

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()
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()
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()
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()
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
*/
@@ -741,15 +840,15 @@ public function provideLookAroundRequirementsInPath()
741840
yield ['/app.php/bar/a/b/bam/c/d/e', '/bar/{foo}/bam/{baz}', '(?<!^).+'];
742841
}
743842

744-
protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null)
843+
protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null, string $defaultLocale = null)
745844
{
746845
$context = new RequestContext('/app.php');
747846
foreach ($parameters as $key => $value) {
748847
$method = 'set'.$key;
749848
$context->$method($value);
750849
}
751850

752-
return new UrlGenerator($routes, $context, $logger);
851+
return new UrlGenerator($routes, $context, $logger, $defaultLocale);
753852
}
754853

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

0 commit comments

Comments
 (0)