Add remove and without functions to Url class

refs #4381
This commit is contained in:
Eric Lippmann 2013-07-12 12:06:30 +02:00
parent 7a075ca52a
commit 78cbeadff2

View File

@ -8,31 +8,39 @@ use Icinga\Exception\ProgrammingError;
class Url class Url
{ {
protected $params = array(); protected $params = array();
protected $url; protected $path;
protected $baseUrl; protected $baseUrl;
protected $request;
public function __construct($url, $params = null) public function __construct($url, $params = null, $request = null)
{ {
if (($split = strpos($url, '?')) === false) { if ($request === null) {
$this->url = $url; $this->request = Icinga::app()->frontController()->getRequest();
if (! empty($params)) {
$this->params = $params;
}
} else { } else {
$this->url = substr($url, 0, $split); // Tests only
parse_str(substr($url, $split + 1), $urlParams); $this->request = $request;
$this->params = $urlParams; }
if (! empty($params)) { if ($url === null) {
$this->params += $params; $this->path = $this->request->getPathInfo();
// TODO: Test += behavior! $this->params = $this->request->getQuery();
} else {
if (($split = strpos($url, '?')) === false) {
$this->path = $url;
} else {
$this->path = substr($url, 0, $split);
// TODO: Use something better than parse_str
parse_str(substr($url, $split + 1), $urlParams);
$this->params = $urlParams;
} }
} }
if (! empty($params)) {
$this->setParams($params);
}
} }
public static function create($url, $params = null) public static function create($url, $params = null, $request = null)
{ {
$u = new Url($url, $params); $u = new Url($url, $params, $request);
return $u; return $u;
} }
@ -43,30 +51,15 @@ class Url
return $this; return $this;
} }
public static function current() public static function current($request = null)
{ {
$app = Icinga::app(); $url = new Url(null, null, $request);
$view = $app->getView()->view;
$request = $app->frontController()->getRequest();
$parts = array();
// TODO: getQuery!
$params = $request->getParams();
foreach (array('module', 'controller', 'action') as $param) {
if ($view->{$param . '_name'} !== 'default') {
$parts[] = $view->{$param . '_name'};
}
if (array_key_exists($param, $params)) {
unset($params[$param]);
}
}
$rel = implode('/', $parts);
$url = new Url($rel, $params);
return $url; return $url;
} }
public function getScript() public function getPath()
{ {
return $this->url; return $this->path;
} }
public function getRelative() public function getRelative()
@ -79,9 +72,9 @@ class Url
$args[] = rawurlencode($name) . '=' . rawurlencode($value); $args[] = rawurlencode($name) . '=' . rawurlencode($value);
} }
} }
$url = vsprintf($this->url, $params); $url = vsprintf($this->path, $params);
if (! empty($args)) { if (! empty($args)) {
$url .= '?' . implode('&', $args); $url .= '?' . implode('&', $args);
} }
return $url; return $url;
} }
@ -94,17 +87,19 @@ class Url
public function setParams($params) public function setParams($params)
{ {
$this->params = $params; if ($params === null) {
$this->params = array();
} else {
$this->params = $params;
}
return $this; return $this;
} }
public function set($key, $val) public function getParams()
{ {
$this->params[$key] = $val; return $this->params;
return $this;
} }
public function hasParam($key) public function hasParam($key)
{ {
return array_key_exists($key, $this->params); return array_key_exists($key, $this->params);
@ -118,31 +113,42 @@ class Url
return $default; return $default;
} }
public function getParams() public function setParam($key, $value)
{ {
return $this->params; $this->params[$key] = $value;
return $this;
} }
public function without($keys) public function remove()
{ {
if (! is_array($keys)) { $args = func_get_args();
$keys = array($keys); foreach ($args as $keys) {
} if (! is_array($keys)) {
foreach ($keys as $key) { $keys = array($keys);
if (array_key_exists($key, $this->params)) { }
unset($this->params[$key]); foreach ($keys as $key) {
if (array_key_exists($key, $this->params)) {
unset($this->params[$key]);
}
} }
} }
return $this; return $this;
} }
public function without()
{
$url = clone($this);
$args = func_get_args();
return call_user_func_array(array($url, 'remove'), $args);
}
public function __toString() public function __toString()
{ {
$url = $this->getRelative(); $url = $this->getRelative();
$base = is_null($this->baseUrl) $base = null === $this->baseUrl
? Icinga::app()->getView()->view->baseUrl() ? $this->request->getBaseUrl()
: $this->baseUrl; : $this->baseUrl;
if ($base === '') { if ($base === '' && $url[0]!== '/') {
// Otherwise all URLs would be relative to wherever you are // Otherwise all URLs would be relative to wherever you are
$base = '/'; $base = '/';
} }
@ -152,3 +158,4 @@ class Url
return $base . $url; return $base . $url;
} }
} }