-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUri.php
More file actions
328 lines (265 loc) · 8.13 KB
/
Uri.php
File metadata and controls
328 lines (265 loc) · 8.13 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
namespace Panada\Http;
/**
* Parse information from URI
*
* So if we have an URI like this: http://localhost:8081/project/panada/labs/v2POC/public/index.php/foo/bar/john/doe
* where:
* - frontController: index.php
* - basePath: /project/panada/labs/v2POC/public/; a folder where front controller located. use for asstet
* - pathInfo: foo/bar/john/doe; every path right after frontController
* - location: /project/panada/labs/v2POC/public/index.php/foo/bar/john/doe; REQUEST URI excluding query
* - relLocation: /project/panada/labs/v2POC/public/index.php/; use for redirect or url href
*
* @author kandar <iskandarsoesman@gmail.com>
*/
class Uri extends \Panada\Utility\Factory
{
protected $config = [
'defaultController' => 'Home',
'defaultAction' => 'index',
'assetPath' => null,
// including protocol URI ex: http://cdn.mysite.com or //cdn.mysite.com
'assetBaseDomain' => null
];
protected $frontController;
protected $basePath;
protected $pathInfo;
protected $location;
protected $pathSegment = [];
protected $requestMethod = 'GET';
protected $host;
protected $queryString;
protected $port = 80;
protected $scheme = 'http';
public function __construct($config = [])
{
$this->setConfig($config);
}
public static function getInstance($config = [])
{
$child = __CLASS__;
if (! isset(parent::$instance[$child])) {
parent::$instance[$child] = (new static($config))->fromServer();
}
return self::$instance[$child];
}
public static function setInstance($config = [])
{
$child = __CLASS__;
return parent::$instance[$child] = new Uri($config);
}
/**
* The default value is equal to $this->basePath
*
* @param string $path Additional path or file for assets
* @return string
*/
public function getAssetURI($path = null)
{
return $this->config['assetBaseDomain'].$this->config['assetPath'].$path;
}
/**
* override default configuration
*
* @param array $config
* @return void
*/
public function setConfig($config = [])
{
$this->config = array_merge($this->config, $config);
return $this;
}
/**
* Get path and URI information from PHP global $_SERVER vars.
*
* @return object An instance from this class
*/
public function fromServer()
{
$this->basePath = str_replace(
$_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']
);
$scriptPath = explode('/', $_SERVER['SCRIPT_FILENAME']);
$this->frontController = end($scriptPath);
$this->basePath = str_replace(
$this->frontController, '', $this->basePath
);
$this->config['assetPath']= $this->basePath;
$scriptName = str_replace($this->frontController, '', $_SERVER['SCRIPT_NAME']);
$requestURI = str_replace($this->basePath.$this->frontController, '', $_SERVER['REQUEST_URI']);
$this->pathInfo = trim(strtok($requestURI, '?'), '/');
$this->location = rtrim(strtok($_SERVER['REQUEST_URI'], '?'), '/');
$this->pathSegment = explode('/', $this->pathInfo);
$this->relLocation = str_replace($this->pathInfo, '', $this->location);
$this->requestMethod = $_SERVER['REQUEST_METHOD'];
$this->host = $_SERVER['SERVER_NAME'];
$this->port = $_SERVER['SERVER_PORT'];
$this->scheme = preg_replace('/[^a-z]/i', '', $_SERVER['SERVER_PROTOCOL']);
if(isset($_SERVER['QUERY_STRING'])) {
$this->queryString = $_SERVER['QUERY_STRING'];
}
return $this;
}
/**
* Just for development propose
*/
public function debug()
{
echo 'SCRIPT_NAME: '.$_SERVER['SCRIPT_NAME'].'<br>';
echo 'REQUEST_URI: '.$_SERVER['REQUEST_URI'].'<br><br>';
echo 'frontController: '.$this->frontController.'<br>';
echo 'basePath: '.$this->basePath.'<br>';
echo 'pathInfo: '.$this->pathInfo.'<br>';
echo 'location: '.$this->location.'<br>';
print_r($this->pathSegment);
exit;
}
public function setScheme($scheme)
{
$this->scheme = $scheme;
return $this;
}
public function getScheme()
{
return $this->scheme;
}
public function setPort($port)
{
$this->port = $port;
return $this;
}
public function getPort()
{
return $this->port;
}
public function setQueryString($queryString)
{
$this->queryString = $queryString;
return $this;
}
public function setHost($host)
{
$this->host = $host;
return $this;
}
public function getHost()
{
return $this->host;
}
public function setRequestMethod($requestMethod)
{
$this->requestMethod = $requestMethod;
return $this;
}
public function getRequestMethod()
{
return $this->requestMethod;
}
public function setFrontController($frontController)
{
$this->frontController = $frontController;
return $this;
}
public function getFrontController()
{
return ltrim($this->frontController, '/');
}
public function setBasePath($basePath)
{
$this->basePath = $this->config['assetPath'] = $basePath;
return $this;
}
public function getBasePath()
{
return $this->basePath;
}
public function setPathInfo($pathInfo)
{
$this->pathInfo = $pathInfo;
$this->pathSegment = explode('/', $this->pathInfo);
return $this;
}
public function getPathInfo()
{
return $this->pathInfo;
}
public function setLocation($location)
{
$this->location = $location;
$this->relLocation = $location;
return $this;
}
public function getLocation()
{
return $this->location;
}
public function location($path = null)
{
return $this->relLocation.$path;
}
/*
* @return array
*/
public function getPathSegment()
{
return $this->pathSegment;
}
public function getSegment($segment = false)
{
if ($segment !== false) {
return ( isset($this->pathSegment[$segment]) ) ? $this->pathSegment[$segment] : false;
}
return $this->pathSegment;
}
public function getController()
{
if ($uriString = $this->getSegment(0)) {
if ($this->stripUriString($uriString)) {
return $uriString;
}
throw new \Exception('Invalid controller name: ' . htmlentities($uriString));
}
return $this->config['defaultController'];
}
/**
* Get action name from the url.
*
* @return string
*/
public function getAction()
{
$uriString = $this->getSegment(1);
if (isset($uriString) && !empty($uriString)) {
if ($this->stripUriString($uriString)) {
return $uriString;
}
throw new \Exception('Invalid action name: ' . htmlentities($uriString));
}
return $this->config['defaultAction'];
}
/**
* Get "GET" request from the url.
*
* @param int
* @return array
*/
public function getRequests($segment = 2)
{
$uriString = $this->getSegment($segment);
if (isset($uriString)) {
return array_slice($this->pathSegment, $segment);
}
return [];
}
/**
* Cleaner for class and method name
*
* @param string
* @return boolean
*/
private function stripUriString($uri)
{
return (!preg_match('/[^a-zA-Z0-9_.-]/', $uri)) ? true : false;
}
}