Passed
Push — release/1.11.0 ( 94cc26 )
by Schlaefer
02:26
created

Router::getCurrentUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
nc 2
nop 0
dl 0
loc 18
c 0
b 0
f 0
cc 3
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
1
<?php
2
/*
3
 * @author  PhileCMS
4
 * @link    https://philecms.github.io
5
 * @license http://opensource.org/licenses/MIT
6
 */
7
8
namespace Phile\Core;
9
10
/**
11
 * this Router class is responsible for Phile's basic URL management
12
 */
13
class Router
14
{
15
16
    /**
17
     * @var array with $_SERVER environment
18
     */
19
    protected $server;
20
21
    /**
22
     * @param array $server $_SERVER environment
23
     */
24 41
    public function __construct(array $server = [])
25
    {
26 41
        if (empty($server)) {
27 41
            $server = $_SERVER;
28
        }
29 41
        $this->server = $server;
30 41
    }
31
32
    /**
33
     * get request-URL relative to Phile base-URL
34
     *
35
     * @return string relative URL e.g. `index`, `sub/`, `sub/page`
36
     */
37 10
    public function getCurrentUrl()
38
    {
39 10
        $url = $this->server['REQUEST_URI'];
40
41
        // remove query string
42 10
        list($url) = explode('?', $url);
43
44
        // resolve root-relative URL-path
45 10
        $baseUrl = $this->getBaseUrl();
46 10
        $basePath = $this->getUrlPath($baseUrl);
47 10
        if (!empty($basePath) && strpos($url, $basePath) === 0) {
48 1
            $url = substr($url, strlen($basePath));
49
        }
50 10
        $url = ltrim($url, '/');
51
52 10
        $url = rawurldecode($url);
53
54 10
        return $url;
55
    }
56
57
    /**
58
     * Get base-URL of the Phile installation
59
     *
60
     * @return string `scheme://host/path/phile-root`
61
     */
62 39
    public function getBaseUrl()
63
    {
64 39
        $baseUrl = Container::getInstance()->get('Phile_Config')->get('base_url');
65 39
        if (!empty($baseUrl)) {
66 37
            return $baseUrl;
67
        }
68
69 33
        $url = '';
70
71 33
        if (isset($this->server['PHP_SELF'])) {
72 33
            $url = preg_replace('/index\.php(.*)?$/', '', (string)$this->server['PHP_SELF']);
73
        }
74
75 33
        $protocol = $this->getProtocol();
76 33
        if (!empty($protocol)) {
77 2
            $host = $this->server['HTTP_HOST'];
78 2
            $url = $protocol . '://' . $host . $url;
79
        }
80
81 33
        $url = rtrim($url, '/');
82 33
        return $url;
83
    }
84
85
    /**
86
     * get the URL for a page-Id
87
     *
88
     * e.g. `sub/index` --> `http://host/phile-root/sub`
89
     *
90
     * @param  string $pageId
91
     * @param  bool   $base   return a full or root-relative URL
92
     * @return string URL
93
     */
94 9
    public function urlForPage($pageId, $base = true)
95
    {
96 9
        $url = $pageId;
97 9
        if ($base) {
98 2
            $url = $this->url($url);
99
        }
100 9
        return $url;
101
    }
102
103
    /**
104
     * converts Phile-root relative URL to full URL
105
     *
106
     * e.g. `foo/bar.ext` --> `http://host/phile-root/foo/bar.ext`
107
     *
108
     * @param  string $url
109
     * @return string
110
     */
111 3
    public function url($url)
112
    {
113 3
        return $this->getBaseUrl() . '/' . ltrim($url, '/');
114
    }
115
116
    /**
117
     * get the HTTP-protocol
118
     *
119
     * @return string|null
120
     */
121 34
    public function getProtocol(): ?string
122
    {
123 34
        if (empty($this->server['HTTP_HOST'])) {
124 32
            return null;
125
        }
126 3
        $protocol = 'http';
127 3
        if (isset($this->server['HTTPS']) && strtolower($this->server['HTTPS']) !== 'off') {
128 1
            $protocol = 'https';
129
        }
130 3
        return $protocol;
131
    }
132
133
    /**
134
     * get path of an URL
135
     *
136
     * `scheme://host/path/sub` --> `/path/sub`
137
     *
138
     * @param  string $url
139
     * @return string
140
     */
141 10
    protected function getUrlPath($url)
142
    {
143 10
        $path = '';
144 10
        if (strpos($url, '://') !== false) {
145 4
            $parsed = parse_url($url);
146 4
            if (isset($parsed['path'])) {
147 1
                $path = $parsed['path'];
148
            }
149
        }
150 10
        return $path;
151
    }
152
}
153