Moved url() function into Utils class

This commit is contained in:
Andy Miller
2018-01-11 17:52:10 -07:00
parent 2c4d5307f6
commit 14af38fb0f
3 changed files with 42 additions and 30 deletions

View File

@@ -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)

View File

@@ -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);
}
/**

View File

@@ -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
*