From 746c91eea42ee4799b6ffeb2b4aada531e430b47 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Tue, 25 Apr 2017 21:08:25 -0400 Subject: [PATCH] Preventing the base path or absolute URL from being prefixed incorrectly on an absolute URL If the version strategy returns an absolute URL, that should be the final URL. --- src/Symfony/Component/Asset/PathPackage.php | 8 +++++++- src/Symfony/Component/Asset/Tests/PathPackageTest.php | 11 +++++++++++ src/Symfony/Component/Asset/Tests/UrlPackageTest.php | 11 +++++++++++ src/Symfony/Component/Asset/UrlPackage.php | 4 ++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Asset/PathPackage.php b/src/Symfony/Component/Asset/PathPackage.php index 906879f8b0..4dcda08e06 100644 --- a/src/Symfony/Component/Asset/PathPackage.php +++ b/src/Symfony/Component/Asset/PathPackage.php @@ -56,7 +56,13 @@ class PathPackage extends Package 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 6a7c2cc6e4..a1ac26442a 100644 --- a/src/Symfony/Component/Asset/Tests/PathPackageTest.php +++ b/src/Symfony/Component/Asset/Tests/PathPackageTest.php @@ -75,6 +75,17 @@ class PathPackageTest extends TestCase ); } + 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 0066834484..97e7a46d70 100644 --- a/src/Symfony/Component/Asset/Tests/UrlPackageTest.php +++ b/src/Symfony/Component/Asset/Tests/UrlPackageTest.php @@ -77,6 +77,17 @@ class UrlPackageTest extends TestCase ); } + 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 de9c1f07d5..782b2ddfce 100644 --- a/src/Symfony/Component/Asset/UrlPackage.php +++ b/src/Symfony/Component/Asset/UrlPackage.php @@ -81,6 +81,10 @@ class UrlPackage extends Package $url = $this->getVersionStrategy()->applyVersion($path); + if ($this->isAbsoluteUrl($url)) { + return $url; + } + if ($url && '/' != $url[0]) { $url = '/'.$url; }