Skip to content

Commit fc1fe8d

Browse files
committed
bug symfony#22526 [Asset] Preventing the base path or absolute URL from being prefixed incorrectly (weaverryan)
This PR was merged into the 2.7 branch. Discussion ---------- [Asset] Preventing the base path or absolute URL from being prefixed incorrectly | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Fixes an edge case (which I need) where the version strategy returns an absolute URL. Currently, if this happens, the baseUrl or basePath is prefixed - giving `https://baseurl.com/https://pathreturnedfromversioning.com` or `/basePath/https://pathreturnedfromversioning.com`. I don't see any reason to prevent an absolute URL from being returned by the version strategy. And it's not a BC break, because the previous paths that were returned were nonsense. Cheers! Commits ------- 746c91e Preventing the base path or absolute URL from being prefixed incorrectly on an absolute URL
2 parents 9774780 + 746c91e commit fc1fe8d

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/Symfony/Component/Asset/PathPackage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ public function getUrl($path)
5656
return $path;
5757
}
5858

59-
return $this->getBasePath().ltrim($this->getVersionStrategy()->applyVersion($path), '/');
59+
$versionedPath = $this->getVersionStrategy()->applyVersion($path);
60+
61+
if ($this->isAbsoluteUrl($versionedPath)) {
62+
return $versionedPath;
63+
}
64+
65+
return $this->getBasePath().ltrim($versionedPath, '/');
6066
}
6167

6268
/**

src/Symfony/Component/Asset/Tests/PathPackageTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ public function getContextConfigs()
7575
);
7676
}
7777

78+
public function testVersionStrategyGivesAbsoluteURL()
79+
{
80+
$versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock();
81+
$versionStrategy->expects($this->any())
82+
->method('applyVersion')
83+
->willReturn('https://cdn.com/bar/main.css');
84+
$package = new PathPackage('/subdirectory', $versionStrategy, $this->getContext('/bar'));
85+
86+
$this->assertEquals('https://cdn.com/bar/main.css', $package->getUrl('main.css'));
87+
}
88+
7889
private function getContext($basePath)
7990
{
8091
$context = $this->getMockBuilder('Symfony\Component\Asset\Context\ContextInterface')->getMock();

src/Symfony/Component/Asset/Tests/UrlPackageTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public function getContextConfigs()
7777
);
7878
}
7979

80+
public function testVersionStrategyGivesAbsoluteURL()
81+
{
82+
$versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock();
83+
$versionStrategy->expects($this->any())
84+
->method('applyVersion')
85+
->willReturn('https://cdn.com/bar/main.css');
86+
$package = new UrlPackage('https://example.com', $versionStrategy);
87+
88+
$this->assertEquals('https://cdn.com/bar/main.css', $package->getUrl('main.css'));
89+
}
90+
8091
/**
8192
* @expectedException \Symfony\Component\Asset\Exception\LogicException
8293
*/

src/Symfony/Component/Asset/UrlPackage.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function getUrl($path)
8181

8282
$url = $this->getVersionStrategy()->applyVersion($path);
8383

84+
if ($this->isAbsoluteUrl($url)) {
85+
return $url;
86+
}
87+
8488
if ($url && '/' != $url[0]) {
8589
$url = '/'.$url;
8690
}

0 commit comments

Comments
 (0)