From 28cff4e1dab1adaca0a8813b291c3094e4c2a799 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 18 Feb 2015 15:51:27 -0700 Subject: [PATCH] Added more flexible param separator option --- system/config/system.yaml | 1 + system/src/Grav/Common/Grav.php | 1 + system/src/Grav/Common/Uri.php | 62 ++++++++++++++++++++++----------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/system/config/system.yaml b/system/config/system.yaml index 9edc14046..5c84d5ceb 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -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 / diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 4eefa12ad..f72d799ae 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -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(); diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index 152d390cb..839b91c11 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -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)