summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/icingaweb2/src/Url.php
blob: 2c6bf1f0510b9dd08eb805fcaacaff6d891c263e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php

namespace gipfl\IcingaWeb2;

use Exception;
use Icinga\Application\Icinga;
use Icinga\Exception\ProgrammingError;
use Icinga\Web\Url as WebUrl;
use Icinga\Web\UrlParams;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use RuntimeException;

/**
 * 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
 */
class Url extends WebUrl
{
    /**
     * @param string $url
     * @param array $params
     * @param null $request
     * @return Url
     */
    public static function fromPath($url, array $params = array(), $request = null)
    {
        if ($request === null) {
            $request = static::getRequest();
        }

        if (! \is_string($url)) {
            throw new InvalidArgumentException(sprintf(
                'url "%s" is not a string',
                \var_export($url, 1)
            ));
        }

        $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']);
        }

        if (isset($parts['query'])) {
            $params = UrlParams::fromQueryString($parts['query'])->mergeValues($params);
        }

        if (isset($parts['fragment'])) {
            $self->setAnchor($parts['fragment']);
        }

        $self->setParams($params);
        return $self;
    }

    public static function fromUri(UriInterface $uri)
    {
        $query = $uri->getQuery();
        $path = $uri->getPath();
        if (\strlen($query)) {
            $path .= "?$query";
        }

        return static::fromPath($path);
    }

    public static function fromServerRequest(ServerRequestInterface $request)
    {
        return static::fromUri($request->getUri());
    }

    /**
     * Create a new Url class representing the current request
     *
     * If $params are given, those will be added to the request's parameters
     * and overwrite any existing parameters
     *
     * @param   UrlParams|array     $params  Parameters that should additionally be considered for the url
     * @param   \Icinga\Web\Request $request A request to use instead of the default one
     *
     * @return  Url
     */
    public static function fromRequest($params = array(), $request = null)
    {
        if ($request === null) {
            $request = static::getRequest();
        }

        $url = new Url();
        $url->setPath(\ltrim($request->getPathInfo(), '/'));
        $request->getQuery();

        // $urlParams = UrlParams::fromQueryString($request->getQuery());
        if (isset($_SERVER['QUERY_STRING'])) {
            $urlParams = UrlParams::fromQueryString($_SERVER['QUERY_STRING']);
        } else {
            $urlParams = UrlParams::fromQueryString('');
            foreach ($request->getQuery() as $k => $v) {
                $urlParams->set($k, $v);
            }
        }

        foreach ($params as $k => $v) {
            $urlParams->set($k, $v);
        }
        $url->setParams($urlParams);
        $url->setBasePath($request->getBaseUrl());

        return $url;
    }

    public function setBasePath($basePath)
    {
        if (property_exists($this, 'basePath')) {
            parent::setBasePath($basePath);
        } else {
            $this->setBaseUrl($basePath);
        }

        return $this;
    }

    public function setParams($params)
    {
        try {
            return parent::setParams($params);
        } catch (ProgrammingError $e) {
            throw new InvalidArgumentException($e->getMessage(), 0, $e);
        }
    }

    protected static function getRequest()
    {
        try {
            $app = Icinga::app();
        } catch (ProgrammingError $e) {
            throw new RuntimeException($e->getMessage(), 0, $e);
        }
        if ($app->isCli()) {
            try {
                return new FakeRequest();
            } catch (Exception $e) {
                throw new RuntimeException($e->getMessage(), 0, $e);
            }
        } else {
            return $app->getRequest();
        }
    }
}