Uri::getBasePath()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 0
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     8/1/15
16
 */
17
class Uri
18
{
19
    /**
20
     * @var ServerBag
21
     */
22
    private $server;
23
24
    /**
25
     * @var string
26
     */
27
    private $pathInfo;
28
29
    /**
30
     * @var string
31
     */
32
    private $baseUri;
33
34
    /**
35
     * @var string
36
     */
37
    private $basePath;
38
39
    /**
40
     * @var array
41
     */
42
    private $segments;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param ServerBag $server
48
     */
49
    public function __construct(ServerBag $server)
50
    {
51
        $this->server = $server;
52
        $this->baseUri = $this->getUri();
53
        $this->segments = explode('/', trim($this->getPathInfo(), '/'));
54
    }
55
56
    /**
57
     * Get all segment.
58
     *
59
     * @return array
60
     */
61
    public function allSegments()
62
    {
63
        return $this->segments;
64
    }
65
66
    /**
67
     * Get uri segment.
68
     *
69
     * @param int   $index
70
     * @param mixed $default
71
     *
72
     * @return string|mixed
73
     */
74
    public function getSegment($index, $default = null)
75
    {
76
        if (isset($this->segments[$index])) {
77
            return $this->segments[$index];
78
        }
79
80
        return $default;
81
    }
82
83
    /**
84
     * Filter uri segment.
85
     *
86
     * @param int        $index
87
     * @param int        $filter
88
     * @param mixed      $default
89
     * @param array|null $options
90
     *
91
     * @return mixed
92
     */
93
    public function filterSegment($index, $filter = FILTER_DEFAULT, $default = null, array $options = null)
94
    {
95
        $variable = $this->getSegment($index, $default);
96
97
        return filter_var($variable, $filter, $options);
98
    }
99
100
    /**
101
     * Get path info.
102
     *
103
     * @return string
104
     */
105
    public function getPathInfo()
106
    {
107
        if (null === $this->pathInfo) {
108
            $requestUri = $this->getRequestUri();
109
            if (empty($requestUri)) {
110
                $this->pathInfo = '/';
111
            } else {
112
                $baseUri = $this->getUri();
113
114
                if ($pos = strpos($requestUri, '?')) {
115
                    $requestUri = substr($requestUri, 0, $pos);
116
                }
117
118
                if (false === empty($baseUri) && false === $this->pathInfo = substr($requestUri, strlen($baseUri))) {
119
                    $this->pathInfo = '/';
120
                } elseif (empty($baseUri)) {
121
                    $this->pathInfo = $requestUri;
122
                } else {
123
                    $this->pathInfo = (string) $this->pathInfo;
124
                }
125
            }
126
        }
127
128
        return '/' . ltrim($this->pathInfo, '/');
129
    }
130
131
    /**
132
     * Get uri for specified path.
133
     *
134
     * @param string $path
135
     *
136
     * @return string
137
     */
138
    public function getUriForPath($path)
139
    {
140
        return $this->getUri() . '/' . trim($path, '/');
141
    }
142
143
    /**
144
     * Get uri.
145
     *
146
     * @return string
147
     */
148
    public function getUri()
149
    {
150
        if (null === $this->baseUri) {
151
            $filename = basename($this->server->get('SCRIPT_FILENAME'));
152
153
            if (basename($this->server->get('SCRIPT_NAME')) === $filename) {
154
                $baseUri = $this->server->get('SCRIPT_NAME');
155
            } else {
156
                $path = $this->server->get('PHP_SELF', '');
157
                $file = $this->server->get('SCRIPT_FILENAME', '');
158
                $parts = explode('/', trim($file, '/'));
159
                $baseUri = '';
160
161
                // build base url from last segment.
162
                for ($i = count($parts) - 1; $i > 0; $i--) {
163
                    $baseUri = '/' . $parts[$i] . $baseUri;
164
                    $pos = strpos($path, $baseUri);
165
166
                    // stop build when $baseUri not match with path.
167
                    if (false === $pos || 0 === $pos) {
168
                        break;
169
                    }
170
                }
171
            }
172
173
            $requestUri = $this->getRequestUri();
174
175
            // check is base url start with request uri.
176
            if (!empty($baseUri) && 0 === strpos(rawurldecode($requestUri), $baseUri)) {
177
                $baseUri = rtrim($baseUri, DIRECTORY_SEPARATOR);
178
            } elseif (!empty($baseUri)
179
                && strlen($requestUri) >= $len = strlen(rtrim(dirname($baseUri), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR)) {
180
                $baseUri = rtrim(substr($requestUri, 0, $len), DIRECTORY_SEPARATOR);
181
            }
182
183
            $this->baseUri = rtrim($baseUri, DIRECTORY_SEPARATOR);
184
        }
185
186
        return $this->baseUri;
187
    }
188
189
    /**
190
     * Get base path.
191
     *
192
     * @return string
193
     */
194
    public function getBasePath()
195
    {
196
        if (null === $this->basePath) {
197
            $uri = $this->getUri();
198
            if (empty($uri)) {
199
                $this->basePath = '';
200
            }
201
            else {
202
                if (basename($this->getUri()) === basename($this->server->get('SCRIPT_NAME'))) {
203
                    $basePath = dirname($this->getUri());
204
                } else {
205
                    $basePath = $this->getUri();
206
                }
207
208
                $this->basePath = rtrim(str_replace('\\', '/', $basePath), '/');
209
            }
210
        }
211
212
        return $this->basePath;
213
    }
214
215
    /**
216
     * Get request uri.
217
     *
218
     * @return string
219
     */
220
    public function getRequestUri()
221
    {
222
        return $this->server->get('REQUEST_URI', '');
223
    }
224
}
225