From 9c3feb4ab4c1b62f2dc6551d34a0d0f42221e4a0 Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Tue, 23 Jan 2018 17:31:43 +1300 Subject: [PATCH 1/3] FIX: Allow absolute URLs be use as resources At current certain interfaces exist that assume only local assets will be loaded (e.g. `SilverStripe\Forms\HTMLEditor\TinyMCEConfig::getConfig()`), where as someone may wish to load an off site resource via the use of an absolute URL (e.g. for fontawesome css provided via a CDN). Because asset path parsing is filtered through a `SilverStripe\Core\Manifest\ResourceURLGenerator`, one must either know in advance if they want an internal or external resource (loading different generators), or the API must allow for this (i.e. an inclusion function for each type of asset). So we can either double the API on the implementing class, or simply make an exception for an absolute URL as high as possible; inside the filter - for which the `vendor/module : path/to/file.asset` shorthand syntax was specifically designed not to conflict with. --- src/Control/SimpleResourceURLGenerator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Control/SimpleResourceURLGenerator.php b/src/Control/SimpleResourceURLGenerator.php index d30b8251d..5a825dbd6 100644 --- a/src/Control/SimpleResourceURLGenerator.php +++ b/src/Control/SimpleResourceURLGenerator.php @@ -71,6 +71,9 @@ class SimpleResourceURLGenerator implements ResourceURLGenerator $relativePath = $resource->getRelativePath(); $exists = $resource->exists(); $absolutePath = $resource->getPath(); + } else if (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); From 943821f984b8ec6d5a4e766f545110955f3e0a74 Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Tue, 23 Jan 2018 17:43:01 +1300 Subject: [PATCH 2/3] Add a test for external resource support `SimpleResourceURLGenerator` has been altered to allow absolute URLs to be loaded directly, as so is now also tested to ensure the added functionality is true to design. --- tests/php/Control/SimpleResourceURLGeneratorTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/php/Control/SimpleResourceURLGeneratorTest.php b/tests/php/Control/SimpleResourceURLGeneratorTest.php index 3ec61d5ae..7c6f2f58a 100644 --- a/tests/php/Control/SimpleResourceURLGeneratorTest.php +++ b/tests/php/Control/SimpleResourceURLGeneratorTest.php @@ -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)); + } } From e1a4b89912cae051db5b25fd53383a371589155c Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Wed, 24 Jan 2018 09:04:22 +1300 Subject: [PATCH 3/3] Code lint fix change `else if` to `elseif` --- src/Control/SimpleResourceURLGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Control/SimpleResourceURLGenerator.php b/src/Control/SimpleResourceURLGenerator.php index 5a825dbd6..502e504df 100644 --- a/src/Control/SimpleResourceURLGenerator.php +++ b/src/Control/SimpleResourceURLGenerator.php @@ -71,7 +71,7 @@ class SimpleResourceURLGenerator implements ResourceURLGenerator $relativePath = $resource->getRelativePath(); $exists = $resource->exists(); $absolutePath = $resource->getPath(); - } else if (Director::is_absolute_url($relativePath)) { + } elseif (Director::is_absolute_url($relativePath)) { // Path is not relative, and probably not of this site return $relativePath; } else {