Added more flexible param separator option

This commit is contained in:
Andy Miller
2015-02-18 15:51:27 -07:00
parent 2bf67e482d
commit 28cff4e1da
3 changed files with 43 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
absolute_urls: false # Absolute or relative URLs for `base_url`
timezone: '' # Valid values: http://php.net/manual/en/timezones.php
param_sep: ':' # Parameter separator
home:
alias: '/home' # Default path for home, ie /

View File

@@ -178,6 +178,7 @@ class Grav extends Container
// Initialize configuration.
$debugger->startTimer('_config', 'Configuration');
$this['config']->init();
$this['uri']->init();
$this['errors']->resetHandlers();
$debugger->init();
$this['config']->debug();

View File

@@ -64,9 +64,6 @@ class Uri
$this->base = $base;
$this->root = $base . $root_path;
$this->url = $base . $uri;
$this->init();
}
/**
@@ -74,25 +71,16 @@ class Uri
*/
public function init()
{
$config = Grav::instance()['config'];
// get any params and remove them
$uri = str_replace($this->root, '', $this->url);
$this->params = array();
if (strpos($uri, ':')) {
$bits = explode('/', $uri);
$path = array();
foreach ($bits as $bit) {
if (strpos($bit, ':') !== false) {
$param = explode(':', $bit);
if (count($param) == 2) {
$this->params[$param[0]] = str_replace('%7C', '/', filter_var($param[1], FILTER_SANITIZE_STRING));
}
} else {
$path[] = $bit;
}
}
$uri = implode('/', $path);
}
// reset params
$this->params = [];
// process params
$uri = $this->processParams($uri, $config->get('system.param_sep'));
// remove the extension if there is one set
$parts = pathinfo($uri);
@@ -120,6 +108,34 @@ class Uri
}
}
/**
* Process any params based in this URL, supports any valid delimiter
*
* @param $uri
* @param string $delimiter
*
* @return string
*/
private function processParams($uri, $delimiter = ':')
{
if (strpos($uri, $delimiter) !== false) {
$bits = explode('/', $uri);
$path = array();
foreach ($bits as $bit) {
if (strpos($bit, $delimiter) !== false) {
$param = explode($delimiter, $bit);
if (count($param) == 2) {
$this->params[$param[0]] = str_replace(urlencode($delimiter), '/', filter_var($param[1], FILTER_SANITIZE_STRING));
}
} else {
$path[] = $bit;
}
}
$uri = implode('/', $path);
}
return $uri;
}
/**
* Return URI path.
*
@@ -174,15 +190,17 @@ class Uri
*/
public function params($id = null)
{
$config = Grav::instance()['config'];
$params = null;
if ($id === null) {
$output = array();
foreach ($this->params as $key => $value) {
$output[] = $key . ':' . $value;
$output[] = $key . $config->get('system.param_sep') . $value;
$params = '/'.implode('/', $output);
}
} elseif (isset($this->params[$id])) {
$params = "/{$id}:".$this->params[$id];
$params = "/{$id}". $config->get('system.param_sep') . $this->params[$id];
}
return $params;
@@ -232,6 +250,8 @@ class Uri
/**
* Return the Extension of the URI
*
* @param null $default
*
* @return String The extension of the URI
*/
public function extension($default = null)