mirror of
https://github.com/getgrav/grav.git
synced 2026-07-07 18:21:48 +02:00
Moved url() function into Utils class
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user