Merge pull request #7787 from NightJar/patch-2

FIX: Allow absolute URLs be use as resources
This commit is contained in:
Robbie Averill 2018-01-24 09:44:16 +13:00 committed by GitHub
commit 795c07c51d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -71,6 +71,9 @@ class SimpleResourceURLGenerator implements ResourceURLGenerator
$relativePath = $resource->getRelativePath();
$exists = $resource->exists();
$absolutePath = $resource->getPath();
} elseif (Director::is_absolute_url($relativePath)) {
// Path is not relative, and probably not of this site
return $relativePath;
} else {
// Use normal string
$absolutePath = preg_replace('/\?.*/', '', Director::baseFolder() . '/' . $relativePath);

View File

@ -64,4 +64,12 @@ class SimpleResourceURLGeneratorTest extends SapphireTest
$generator->urlForResource($module->getResource('client/style.css'))
);
}
public function testAbsoluteResource()
{
/** @var SimpleResourceURLGenerator $generator */
$generator = Injector::inst()->get(ResourceURLGenerator::class);
$fakeExternalAsset = 'https://cdn.example.com/some_library.css';
$this->assertEquals($fakeExternalAsset, $generator->urlForResource($fakeExternalAsset));
}
}