bug #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
-------

746c91eea4 Preventing the base path or absolute URL from being prefixed incorrectly on an absolute URL
This commit is contained in:
Fabien Potencier 2017-04-26 08:47:35 -04:00
commit fc1fe8decf
4 changed files with 33 additions and 1 deletions

View File

@ -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, '/');
}
/**

View File

@ -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();

View File

@ -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
*/

View File

@ -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;
}