From 14af38fb0f64f32e5d1754cb192d2fc346ba0b3a Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 11 Jan 2018 17:52:10 -0700 Subject: [PATCH] Moved url() function into Utils class --- CHANGELOG.md | 2 + system/src/Grav/Common/Twig/TwigExtension.php | 31 +-------------- system/src/Grav/Common/Utils.php | 39 +++++++++++++++++++ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f5662547..3fb9a791b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.4.0-rc.1 ## mm/dd/2017 +1. [](#new) + * Moved Twig `urlFunc()` to `Utils::url()` as its so darn handy 1. [](#improved) * Made `modular` blueprint more flexible 1. [](#bugfix) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index be41fc8a0..e0f5a2deb 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -677,36 +677,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn */ public function urlFunc($input, $domain = false) { - if (!trim((string)$input)) { - return false; - } - - if ($this->grav['config']->get('system.absolute_urls', false)) { - $domain = true; - } - - if (Grav::instance()['uri']->isExternal($input)) { - return $input; - } - - $input = ltrim((string)$input, '/'); - - if (Utils::contains((string)$input, '://')) { - /** @var UniformResourceLocator $locator */ - $locator = $this->grav['locator']; - - - - // Get relative path to the resource (or false if not found). - $resource = $locator->findResource($input, false); - } else { - $resource = $input; - } - - /** @var Uri $uri */ - $uri = $this->grav['uri']; - - return $resource ? rtrim($uri->rootUrl($domain), '/') . '/' . $resource : null; + return Utils::url($input, $domain); } /** diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 16d0ed4d8..ba912c857 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -16,6 +16,45 @@ abstract class Utils { protected static $nonces = []; + /** + * Simple helper method to make getting a Grav URL easier + * + * @param $input + * @param bool $domain + * @return bool|null|string + */ + public static function url($input, $domain = false) + { + if (!trim((string)$input)) { + return false; + } + + if (Grav::instance()['config']->get('system.absolute_urls', false)) { + $domain = true; + } + + if (Grav::instance()['uri']->isExternal($input)) { + return $input; + } + + $input = ltrim((string)$input, '/'); + + if (Utils::contains((string)$input, '://')) { + /** @var UniformResourceLocator $locator */ + $locator = Grav::instance()['locator']; + + // Get relative path to the resource (or false if not found). + $resource = $locator->findResource($input, false); + } else { + $resource = $input; + } + + /** @var Uri $uri */ + $uri = Grav::instance()['uri']; + + return $resource ? rtrim($uri->rootUrl($domain), '/') . '/' . $resource : null; + } + /** * Check if the $haystack string starts with the substring $needle *