2016-11-23 18:20:59 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Businessprocess\Web;
|
|
|
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
use Icinga\Web\Url as WebUrl;
|
2016-11-26 15:18:18 -05:00
|
|
|
use Icinga\Web\UrlParams;
|
2016-11-23 18:20:59 -05:00
|
|
|
|
2016-11-26 15:18:18 -05:00
|
|
|
/**
|
|
|
|
|
* Class Url
|
|
|
|
|
*
|
|
|
|
|
* The main purpose of this class is to get unit tests running on CLI
|
|
|
|
|
* Little code from Icinga\Web\Url has been duplicated, as neither fromPath()
|
|
|
|
|
* nor getRequest() can be extended in a meaningful way at the time of this
|
|
|
|
|
* writing
|
|
|
|
|
*
|
|
|
|
|
* @package Icinga\Module\Businessprocess\Web
|
|
|
|
|
*/
|
2016-11-23 18:20:59 -05:00
|
|
|
class Url extends WebUrl
|
|
|
|
|
{
|
|
|
|
|
public static function fromPath($url, array $params = array(), $request = null)
|
|
|
|
|
{
|
|
|
|
|
if ($request === null) {
|
|
|
|
|
$request = static::getRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! is_string($url)) {
|
|
|
|
|
throw new ProgrammingError(
|
|
|
|
|
'url "%s" is not a string',
|
|
|
|
|
$url
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self = new static;
|
|
|
|
|
|
|
|
|
|
if ($url === '#') {
|
|
|
|
|
return $self->setPath($url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parts = parse_url($url);
|
|
|
|
|
|
|
|
|
|
$self->setBasePath($request->getBaseUrl());
|
|
|
|
|
if (isset($parts['path'])) {
|
|
|
|
|
$self->setPath($parts['path']);
|
|
|
|
|
}
|
2016-11-26 15:18:18 -05:00
|
|
|
|
|
|
|
|
if (isset($urlParts['query'])) {
|
|
|
|
|
$params = UrlParams::fromQueryString($urlParts['query'])->mergeValues($params);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-23 18:20:59 -05:00
|
|
|
if (isset($parts['fragment'])) {
|
|
|
|
|
$self->setAnchor($parts['fragment']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->setParams($params);
|
|
|
|
|
return $self;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 10:17:31 -05:00
|
|
|
public function setBasePath($basePath)
|
|
|
|
|
{
|
2016-11-28 10:40:45 -05:00
|
|
|
if (property_exists($this, 'basePath')) {
|
|
|
|
|
parent::setBasePath($basePath);
|
|
|
|
|
} else {
|
|
|
|
|
return $this->setBaseUrl($basePath);
|
|
|
|
|
}
|
2016-11-28 10:17:31 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 18:20:59 -05:00
|
|
|
protected static function getRequest()
|
|
|
|
|
{
|
|
|
|
|
$app = Icinga::app();
|
|
|
|
|
if ($app->isCli()) {
|
|
|
|
|
return new FakeRequest();
|
|
|
|
|
} else {
|
|
|
|
|
return $app->getRequest();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-26 15:18:18 -05:00
|
|
|
}
|