From bdcb57ec2b22b335a526d5e303c4705cac6b3290 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Wed, 12 Apr 2023 17:29:59 +1200 Subject: [PATCH] NEW Add a cascading theme version of $resourceURL --- src/Core/Manifest/ModuleResourceLoader.php | 4 +-- src/View/ThemeResourceLoader.php | 29 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Core/Manifest/ModuleResourceLoader.php b/src/Core/Manifest/ModuleResourceLoader.php index 9dcb25de5..96cf80e2c 100644 --- a/src/Core/Manifest/ModuleResourceLoader.php +++ b/src/Core/Manifest/ModuleResourceLoader.php @@ -15,7 +15,7 @@ class ModuleResourceLoader implements TemplateGlobalProvider use Injectable; /** - * Convert a file of the form "vendor/package:resource" into a BASE_PATH-relative file + * Convert a file of the form "vendor/package:resource" into a BASE_PATH-relative file or folder * For other files, return original value * * @param string $resource @@ -91,7 +91,7 @@ class ModuleResourceLoader implements TemplateGlobalProvider * Returns the original resource otherwise. * * @param string $resource - * @return ModuleResource|string The resource, or input string if not a module resource + * @return ModuleResource|string The resource (or directory), or input string if not a module resource */ public function resolveResource($resource) { diff --git a/src/View/ThemeResourceLoader.php b/src/View/ThemeResourceLoader.php index e6f3d9b7e..7967c22bd 100644 --- a/src/View/ThemeResourceLoader.php +++ b/src/View/ThemeResourceLoader.php @@ -7,12 +7,13 @@ use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Flushable; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Manifest\ModuleLoader; +use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\Core\Path; /** * Handles finding templates from a stack of template manifest objects. */ -class ThemeResourceLoader implements Flushable +class ThemeResourceLoader implements Flushable, TemplateGlobalProvider { /** @@ -308,9 +309,9 @@ class ThemeResourceLoader implements Flushable } /** - * Resolve a themed resource + * Resolve a themed resource or directory * - * A themed resource and be any file that resides in a theme folder. + * A themed resource can be any file that resides in a theme folder. * * @param string $resource A file path relative to the root folder of a theme * @param array $themes An order listed of themes to search, Defaults to {@see SSViewer::get_themes()} @@ -336,6 +337,28 @@ class ThemeResourceLoader implements Flushable return null; } + /** + * Return the URL for a given themed resource or directory within the project. + * + * A themed resource can be any file that resides in a theme folder. + */ + public static function themedResourceURL(string $resource): ?string + { + $filePath = static::inst()->findThemedResource($resource); + if (!$filePath) { + return ''; + } + + return ModuleResourceLoader::singleton()->resolveURL($filePath); + } + + public static function get_template_global_variables() + { + return [ + 'themedResourceURL', + ]; + } + /** * Resolve all themes to the list of root folders relative to site root *