-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
476 lines (429 loc) · 13.3 KB
/
Router.php
File metadata and controls
476 lines (429 loc) · 13.3 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
* Devbr\Router
* PHP version 7
*
* @category Access
* @package Librarys
* @author Bill Rocha <prbr@ymail.com>
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
* @license <https://opensource.org/licenses/MIT> MIT
* @version GIT: 0.0.1
* @link https://github.com/devbr
*/
namespace Devbr;
/**
* Router Class
*
* @category Access
* @package Librarys
* @author Bill Rocha <prbr@ymail.com>
* @license <https://opensource.org/licenses/MIT> MIT
* @link https://github.com/devbr
*/
class Router
{
private $autorun = true;
private $url = false;
private $http = '';
private $base = '';
private $request = false;
private $routers = [];
private $params = [];
private $all = [];
private $method = 'GET'; //CLI, GET, POST, DELETE, PUT, PATCH, OPTIONS, HEAD
private $separator = '::';
private $controller = '';
private $action = '';
//WEB
private $defaultController = 'App';
private $defaultAction = 'pageNotFound';
private $namespacePrefix = ''; //namespace prefix for MVC systems - ex.: '\Controller'
//CLI
private $defaultCliController = 'Main';
private $defaultCliAction = 'cliHelp';
private $namespaceCliPrefix = 'Devbr\Cli';
//Statics
private static $node = null;
private static $ctrl = null;
//GETs -----------------------------------------------------------------
function getUrl()
{
return $this->url;
}
function getHttp()
{
return $this->http;
}
function getBase()
{
return $this->base;
}
function getRequest()
{
return $this->request;
}
function getRouters()
{
return $this->routers;
}
function getAll()
{
return $this->all;
}
function getMethod()
{
return $this->method;
}
function getController()
{
return $this->controller;
}
function getAction()
{
return $this->action;
}
function getSeparator()
{
return $this->separator;
}
function getParams()
{
return count($this->params) > 0 ? $this->params : null;
}
//SETs -----------------------------------------------------------------
function setSeparator($v)
{
$this->separator = $v;
return $this;
}
function setDefaultController($v)
{
$this->defaultController = trim( str_replace('/', '\\', $v), '\\/ ');
return $this;
}
function setDefaultAction($v)
{
$this->defaultAction = trim($v, '\\/ ');
return $this;
}
function setNamespacePrefix($v)
{
$this->namespacePrefix = $v === '' ? '' : '\\'.trim( str_replace('/', '\\', $v), '\\/ ');
return $this;
}
//CLI
function setDefaultCliController($v)
{
$this->defaultCliController = trim( str_replace('/', '\\', $v), '\\/ ');
return $this;
}
function setDefaultCliAction($v)
{
$this->defaultCliAction = trim($v, '\\/ ');
return $this;
}
function setNamespaceCliPrefix($v)
{
$this->namespaceCliPrefix = $v === '' ? '' : '\\'.trim( str_replace('/', '\\', $v), '\\/ ');
return $this;
}
/**
* Constructor
*/
function __construct(
$autorun = true,
$request = null,
$url = null
) {
if ($autorun === false){
$this->autorun = false;
}
if ($request !== null) {
$this->request = $request;
}
if ($url !== null) {
$this->url = $url;
}
$this->method = $this->requestMethod();
$this->mount();
//Saving this object in static node (for future static access)
if (!is_object(static::$node)) {
static::$node = $this;
}
}
/**
* Singleton instance
*
*/
public static function this()
{
if (is_object(static::$node)) {
return static::$node;
}
//else...
list($routers, $request, $url) = array_merge(func_get_args(), [null, null, null]);
return static::$node = new static($routers, $request, $url);
}
/**
* Get Controller Object
*/
public static function getCtrl()
{
return static::$ctrl;
}
function loadConfig()
{
if (class_exists('\Config\Devbr\Router')) {
new \Config\Devbr\Router($this);
}
return $this;
}
/**
* Make happen...
*
*/
function run()
{
//Resolve request
$this->resolve();
//If is a CALLBACK...
if (is_object($this->controller)) {
exit(call_user_func_array($this->controller, [$this->request, $this->params]));
}
if ($this->controller === null) {
$this->controller = $this->method == 'CLI' ? $this->defaultCliController : $this->defaultController;
}
if ($this->action === null) {
$this->action = $this->method == 'CLI' ? $this->defaultCliAction : $this->defaultAction;
}
//Name format to Controller namespace
$ctrl = $this->formatePrsr4Name($this->controller, $this->method == 'CLI' ? $this->namespaceCliPrefix : $this->namespacePrefix);
//Save the controller...
$this->controller = $ctrl;
//Check whether to automatically run the Controller
// or return this object
if(!$this->autorun) {
return $this;
}
//Instantiate the controller
if (class_exists($ctrl)) {
static::$ctrl = new $ctrl($this->params, $this->request);
//IN CLI mode finish in this point: Cli\Main::__construct return to CMD.
} else {
if($this->method != 'CLI') {
header("HTTP/1.0 404 Not Found");
exit('Page not Found!');
}
exit("\nController not found!");
}
//Seeking for the METHOD...
if (!method_exists(static::$ctrl, $this->action)) {
$this->action = $this->method == 'CLI' ? $this->defaultCliAction : $this->defaultAction;
if(!method_exists(static::$ctrl, $this->action)){
if($this->method != 'CLI') {
header("HTTP/1.0 404 Not Found");
exit('Page not Found!');
}
exit();
}
}
//Call action
return call_user_func_array([static::$ctrl, $this->action],
[$this->request, $this->params]);
}
/**
* Resolve routers
*
*/
function resolve()
{
if ($this->method == 'CLI') {
$route = $this->searchCliRoute();
} else {
//first: serach in ALL
$route = $this->searchRouter($this->all);
//now: search for access method
if ($route === false && isset($this->routers[$this->method])) {
$route = $this->searchRouter($this->routers[$this->method]);
}
}
//not match...
if ($route === false) {
$route['controller'] = $route['action'] = $route['params'] = $route['request'] = null;
}
//set params
$this->controller = $route['controller'];
$this->action = $route['action'];
$this->params = $route['params'];
//out with decoded router OR all null
return $route;
}
/**
* Insert/config routers
*
*/
function respond(
$method = 'all',
$request = '',
$controller = null,
$action = null
) {
$method = strtoupper(trim($method));
//Para sintaxe: CONTROLLER::ACTION
if (!is_object($controller) && strpos($controller, $this->separator) !== false) {
$a = explode($this->separator, $controller);
$controller = isset($a[0]) ? $a[0] : null;
$action = isset($a[1]) ? $a[1] : null;
}
if ($method == 'ALL') {
$this->all[] = ['request' => trim($request, '/'), 'controller' => $controller, 'action' => $action];
} else {
foreach (explode('|', $method) as $mtd) {
$this->routers[$mtd][] = ['request' => trim($request, '/'), 'controller' => $controller, 'action' => $action];
}
}
return $this;
}
/**
* Mount
*/
private function mount()
{
if ($this->method === 'CLI') {
global $argv;
if (isset($argv[0]) && $argv[0] == 'index.php') {
array_shift($argv);
}
$this->request = $argv;
return;
}
//Detect SSL access
if (!isset($_SERVER['SERVER_PORT'])) {
$_SERVER['SERVER_PORT'] = 80;
}
$this->http = (isset($_SERVER['HTTPS']) && ($_SERVER["HTTPS"] == "on" || $_SERVER["HTTPS"] == 1 || $_SERVER['SERVER_PORT'] == 443)) ? 'https://' : 'http://';
//What's base??!
$this->base = isset($_SERVER['PHAR_SCRIPT_NAME']) ? dirname($_SERVER['PHAR_SCRIPT_NAME']) : rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']), ' /');
if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
$this->base .= ':' . $_SERVER['SERVER_PORT'];
}
//URL & REQST Constants:
if($this->request === false) {
$this->request = urldecode(isset($_SERVER['REQUEST_URI']) ? urldecode(trim(str_replace($this->base, '', trim($_SERVER['REQUEST_URI'])), ' /')) : '');
}
defined('_RQST') || define('_RQST', $this->request);
if($this->url === false) {
$this->url = isset($_SERVER['SERVER_NAME']) ? $this->http . $_SERVER['SERVER_NAME'] . $this->base . '/' : '';
}
defined('_URL') || define('_URL', $this->url);
//Load configurations
$this->loadConfig();
}
/**
* [formatePrsr4Name description]
*
* @param [type] $name [description]
* @param string $prefix [description]
*
* @return [type] [description]
*/
private function formatePrsr4Name($name, $prefix = '')
{
$tmp = explode('\\', str_replace('/', '\\', $name));
$name = $prefix;
foreach ($tmp as $tmp1) {
$name .= '\\'.ucfirst($tmp1);
}
return $name;
}
/**
* [searchCliRoute description]
*
* @return [type] [description]
*/
private function searchCliRoute()
{
$request = $this->request;
$this->request = implode(' ', $this->request);
$route = ['request'=>$request, 'controller'=>false, 'action'=>null, 'params'=>null];
if (isset($request[0])) {
$tmp = explode($this->separator, $request[0]);
if (isset($tmp[0])) {
$route['controller'] = $tmp[0];
}
if (isset($tmp[1])) {
$route['action'] = $tmp[1];
}
array_shift($request);
if (count($request) > 0) {
$route['params'] = $request;
}
}
return $route;
}
/**
* Search for valide router
*
* @params
*/
private function searchRouter($routes)
{
foreach ($routes as $route) {
if ($route['controller'] === null
|| !preg_match_all('#^' . $route['request'] . '$#',
$this->request,
$matches,
PREG_SET_ORDER)
) {
continue;
}
$route['params'] = array_slice($matches[0], 1);
return $route;
}
//não existe rotas
return false;
}
/**
* Get all request headers
* @return array The request headers
*/
private function requestHeaders()
{
// getallheaders available, use that
if (function_exists('getallheaders')) {
return getallheaders();
}
// getallheaders not available: manually extract 'm
$headers = array();
foreach ($_SERVER as $name => $value) {
if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) {
$headers[str_replace(array(' ', 'Http'), array('-', 'HTTP'), ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
/**
* Get the request method used, taking overrides into account
* @return string The Request method to handle
*/
private function requestMethod()
{
if (php_sapi_name() === 'cli') {
return 'CLI';
}
// Take the method as found in $_SERVER
$method = $_SERVER['REQUEST_METHOD'];
if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
ob_start();
$method = 'GET';
} // If it's a POST request, check for a method override header
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$headers = $this->requestHeaders();
if (isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], array('PUT', 'DELETE', 'PATCH'))) {
$method = $headers['X-HTTP-Method-Override'];
}
}
return $method;
}
}