From 71639de5ecf6eb7385a436d32da6690a8a6a01c8 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 10 Sep 2020 15:43:57 -0600 Subject: [PATCH] Added Utils::fullPath() helper method --- system/src/Grav/Common/Utils.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 56b2d891b..8a6d987dd 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -129,6 +129,28 @@ abstract class Utils return rtrim($uri->rootUrl($domain), '/') . '/' . ($resource ?? ''); } + /** + * Helper method to find the full path to a file, be it a stream, a relative path, or + * already a full path + * + * @param $path + * @return string + */ + public static function fullPath($path) + { + $locator = Grav::instance()['locator']; + + if ($locator->isStream($path)) { + $path = $locator->findResource($path, true); + } elseif (!Utils::startsWith($path, GRAV_ROOT)) { + $base_url = Grav::instance()['base_url']; + $path = GRAV_ROOT . '/' . ltrim(Utils::replaceFirstOccurrence($base_url, '', $path), '/'); + } + + return $path; + } + + /** * Check if the $haystack string starts with the substring $needle *