Created
March 24, 2016 21:31
-
-
Save dlundgren/315af5a5aa305c0a349e to your computer and use it in GitHub Desktop.
Foil extension to handle route generation in the views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\View\Extension; | |
use Aura\Router\Router; | |
use Foil\Contracts\ExtensionInterface; | |
class UrlFor | |
implements ExtensionInterface | |
{ | |
/** | |
* @var Router | |
*/ | |
private $router; | |
/** | |
* Cache of the routes | |
* | |
* @var array | |
*/ | |
private $routeCache = []; | |
/** | |
* From Symfony HttpFoundation Request getRequestUri() | |
* | |
* @var string | |
*/ | |
private $requestUri; | |
public function __construct(Router $router, $requestUri, $baseUrl) | |
{ | |
$this->router = $router; | |
$this->requestUri = $requestUri; | |
$this->baseUrl = $baseUrl; | |
} | |
/** | |
* Returns whether or not the request uri start with route | |
* | |
* @param $route | |
* @param array $params | |
* @return bool | |
*/ | |
public function urlStartsWithRoute($route, array $params = []) | |
{ | |
return stripos($this->requestUri, $this->urlFor($route, $params, false)) === 0; | |
} | |
/** | |
* Returns whether or not the request uri is the current uri | |
* | |
* @param string $route the route name | |
* @param array $params the route params | |
* @return bool | |
*/ | |
public function urlIs($route, array $params = []) | |
{ | |
return $this->urlFor($route, $params, false) === $this->requestUri; | |
} | |
/** | |
* Returns the url for the given route | |
* | |
* @param string $route The route name | |
* @param array $params Params for the route | |
* @param boolean $absolute Whether or not to create an absolute URL | |
* @return string An empty string if the route isn't defined in the router | |
*/ | |
public function urlFor($route, array $params = [], $absolute = true) | |
{ | |
$key = $route . '.' . md5(json_encode($params)); | |
if (!isset($this->routeCache[$key])) { | |
$url = ''; | |
try { | |
$url = $this->router->generate($route, $params); | |
if ($absolute && !array_key_exists('scheme', parse_url($url))) { | |
$url = $this->baseUrl . $url; | |
} | |
} | |
catch (\Exception $e) { | |
// The route doesn't exist so we return an empty string | |
} | |
$this->routeCache[$key] = $url; | |
} | |
return $this->routeCache[$key]; | |
} | |
/** | |
* @param $url | |
* @param array $query | |
* @return string | |
*/ | |
public function urlWithQuery($url, array $query = []) | |
{ | |
if (empty($query)) { | |
return $url; | |
} | |
return $url . (stripos($url, '?') === false ? '?' : '&') . http_build_query($query); | |
} | |
/** | |
* Provides access to the base url | |
* | |
* @return string | |
*/ | |
public function baseUrl() | |
{ | |
return $this->baseUrl; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function provideFilters() | |
{ | |
return []; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function provideFunctions() | |
{ | |
return [ | |
'urlFor' => [$this, 'urlFor'], | |
'urlWithQuery' => [$this, 'urlWithQuery'], | |
'baseUrl' => [$this, 'baseUrl'], | |
'urlIs' => [$this, 'urlIs'], | |
'urlStartsWithRoute' => [$this, 'urlStartsWithRoute'], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setup(array $args = []) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment