diff --git a/src/Symfony/Component/Asset/PathPackage.php b/src/Symfony/Component/Asset/PathPackage.php index 906879f8b064d..4dcda08e062c4 100644 --- a/src/Symfony/Component/Asset/PathPackage.php +++ b/src/Symfony/Component/Asset/PathPackage.php @@ -56,7 +56,13 @@ public function getUrl($path) return $path; } - return $this->getBasePath().ltrim($this->getVersionStrategy()->applyVersion($path), '/'); + $versionedPath = $this->getVersionStrategy()->applyVersion($path); + + if ($this->isAbsoluteUrl($versionedPath)) { + return $versionedPath; + } + + return $this->getBasePath().ltrim($versionedPath, '/'); } /** diff --git a/src/Symfony/Component/Asset/Tests/PathPackageTest.php b/src/Symfony/Component/Asset/Tests/PathPackageTest.php index 6a7c2cc6e45d8..a1ac26442a066 100644 --- a/src/Symfony/Component/Asset/Tests/PathPackageTest.php +++ b/src/Symfony/Component/Asset/Tests/PathPackageTest.php @@ -75,6 +75,17 @@ public function getContextConfigs() ); } + public function testVersionStrategyGivesAbsoluteURL() + { + $versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock(); + $versionStrategy->expects($this->any()) + ->method('applyVersion') + ->willReturn('https://cdn.com/bar/main.css'); + $package = new PathPackage('/subdirectory', $versionStrategy, $this->getContext('/bar')); + + $this->assertEquals('https://cdn.com/bar/main.css', $package->getUrl('main.css')); + } + private function getContext($basePath) { $context = $this->getMockBuilder('Symfony\Component\Asset\Context\ContextInterface')->getMock(); diff --git a/src/Symfony/Component/Asset/Tests/UrlPackageTest.php b/src/Symfony/Component/Asset/Tests/UrlPackageTest.php index 0066834484351..97e7a46d706da 100644 --- a/src/Symfony/Component/Asset/Tests/UrlPackageTest.php +++ b/src/Symfony/Component/Asset/Tests/UrlPackageTest.php @@ -77,6 +77,17 @@ public function getContextConfigs() ); } + public function testVersionStrategyGivesAbsoluteURL() + { + $versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock(); + $versionStrategy->expects($this->any()) + ->method('applyVersion') + ->willReturn('https://cdn.com/bar/main.css'); + $package = new UrlPackage('https://example.com', $versionStrategy); + + $this->assertEquals('https://cdn.com/bar/main.css', $package->getUrl('main.css')); + } + /** * @expectedException \Symfony\Component\Asset\Exception\LogicException */ diff --git a/src/Symfony/Component/Asset/UrlPackage.php b/src/Symfony/Component/Asset/UrlPackage.php index de9c1f07d5f5d..782b2ddfce93b 100644 --- a/src/Symfony/Component/Asset/UrlPackage.php +++ b/src/Symfony/Component/Asset/UrlPackage.php @@ -81,6 +81,10 @@ public function getUrl($path) $url = $this->getVersionStrategy()->applyVersion($path); + if ($this->isAbsoluteUrl($url)) { + return $url; + } + if ($url && '/' != $url[0]) { $url = '/'.$url; }