Skip to content

Commit a546d4f

Browse files
Merge branch '3.4' into 4.2
* 3.4: Fix expired lock not cleaned [HttpFoundation] Fix SA/phpdoc JsonResponse SimpleCacheAdapter fails to cache any item if a namespace is used validate composite constraints in all groups [Serializer] Handle true and false appropriately in CSV encoder Fix binary operation `+`, `-` or `*` on string [VarDumper] fix dumping objects that implement __debugInfo() [Routing] fix absolute url generation when scheme is not known
2 parents b7d6c99 + c3c4860 commit a546d4f

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

Generator/UrlGenerator.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,18 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
239239
}
240240
}
241241

242-
if ((self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) && !empty($host)) {
243-
$port = '';
244-
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
245-
$port = ':'.$this->context->getHttpPort();
246-
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
247-
$port = ':'.$this->context->getHttpsPort();
248-
}
242+
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
243+
if ('' !== $host || ('' !== $scheme && 'http' !== $scheme && 'https' !== $scheme)) {
244+
$port = '';
245+
if ('http' === $scheme && 80 !== $this->context->getHttpPort()) {
246+
$port = ':'.$this->context->getHttpPort();
247+
} elseif ('https' === $scheme && 443 !== $this->context->getHttpsPort()) {
248+
$port = ':'.$this->context->getHttpsPort();
249+
}
249250

250-
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
251-
$schemeAuthority .= $host.$port;
251+
$schemeAuthority = self::NETWORK_PATH === $referenceType || '' === $scheme ? '//' : "$scheme://";
252+
$schemeAuthority .= $host.$port;
253+
}
252254
}
253255

254256
if (self::RELATIVE_PATH === $referenceType) {

Tests/Generator/UrlGeneratorTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -579,28 +579,27 @@ public function testHostIsCaseInsensitive()
579579

580580
public function testDefaultHostIsUsedWhenContextHostIsEmpty()
581581
{
582-
$routes = $this->getRoutes('test', new Route('/route', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}', ['http']));
582+
$routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
583583

584584
$generator = $this->getGenerator($routes);
585585
$generator->getContext()->setHost('');
586586

587-
$this->assertSame('http://my.fallback.host/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
587+
$this->assertSame('http://my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
588588
}
589589

590-
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndSchemeIsNot()
590+
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndPathReferenceType()
591591
{
592-
$routes = $this->getRoutes('test', new Route('/route', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}', ['http', 'https']));
592+
$routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
593593

594594
$generator = $this->getGenerator($routes);
595595
$generator->getContext()->setHost('');
596-
$generator->getContext()->setScheme('https');
597596

598-
$this->assertSame('https://my.fallback.host/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
597+
$this->assertSame('//my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH));
599598
}
600599

601-
public function testAbsoluteUrlFallbackToRelativeIfHostIsEmptyAndSchemeIsNot()
600+
public function testAbsoluteUrlFallbackToPathIfHostIsEmptyAndSchemeIsHttp()
602601
{
603-
$routes = $this->getRoutes('test', new Route('/route', [], [], [], '', ['http', 'https']));
602+
$routes = $this->getRoutes('test', new Route('/route'));
604603

605604
$generator = $this->getGenerator($routes);
606605
$generator->getContext()->setHost('');
@@ -609,6 +608,39 @@ public function testAbsoluteUrlFallbackToRelativeIfHostIsEmptyAndSchemeIsNot()
609608
$this->assertSame('/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
610609
}
611610

611+
public function testAbsoluteUrlFallbackToNetworkIfSchemeIsEmptyAndHostIsNot()
612+
{
613+
$routes = $this->getRoutes('test', new Route('/path'));
614+
615+
$generator = $this->getGenerator($routes);
616+
$generator->getContext()->setHost('example.com');
617+
$generator->getContext()->setScheme('');
618+
619+
$this->assertSame('//example.com/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
620+
}
621+
622+
public function testAbsoluteUrlFallbackToPathIfSchemeAndHostAreEmpty()
623+
{
624+
$routes = $this->getRoutes('test', new Route('/path'));
625+
626+
$generator = $this->getGenerator($routes);
627+
$generator->getContext()->setHost('');
628+
$generator->getContext()->setScheme('');
629+
630+
$this->assertSame('/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
631+
}
632+
633+
public function testAbsoluteUrlWithNonHttpSchemeAndEmptyHost()
634+
{
635+
$routes = $this->getRoutes('test', new Route('/path', [], [], [], '', ['file']));
636+
637+
$generator = $this->getGenerator($routes);
638+
$generator->getContext()->setBaseUrl('');
639+
$generator->getContext()->setHost('');
640+
641+
$this->assertSame('file:///path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
642+
}
643+
612644
public function testGenerateNetworkPath()
613645
{
614646
$routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com', ['http']));

0 commit comments

Comments
 (0)