1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vertex\Core; |
4
|
|
|
|
5
|
|
|
use \FastRoute\Dispatcher; |
6
|
|
|
|
7
|
|
|
class Router |
8
|
|
|
{ |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Placeholder for the dispatcher class. |
12
|
|
|
* |
13
|
|
|
* @var object |
14
|
|
|
*/ |
15
|
|
|
private $dispatcher; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Main controller namespace. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $namespace = 'Vertex\\Controller\\'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new router. |
26
|
|
|
* |
27
|
|
|
* @var \FastRoute\simpleDispatcher |
28
|
|
|
* @return void |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
public function __construct($dispatcher) |
31
|
|
|
{ |
32
|
|
|
$this->dispatcher = $dispatcher; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Dispatch the route. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function dispatch() |
41
|
|
|
{ |
42
|
|
|
$routeInfo = $this->info(); |
43
|
|
|
|
44
|
|
|
switch ($routeInfo[0]) { |
45
|
|
|
case Dispatcher::NOT_FOUND: |
46
|
|
|
echo $this->error('404', 'Page Could Not Be Found'); |
47
|
|
|
break; |
48
|
|
|
case Dispatcher::METHOD_NOT_ALLOWED: |
49
|
|
|
echo $this->error('405', 'Method is not allowed'); |
50
|
|
|
break; |
51
|
|
|
case Dispatcher::FOUND: |
52
|
|
|
echo $this->found($routeInfo[1], $routeInfo[2]); |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the current route information. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function info() |
63
|
|
|
{ |
64
|
|
|
return $this->dispatcher->dispatch($_SERVER['REQUEST_METHOD'], strtok($_SERVER['REQUEST_URI'], '?')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if a route was found. |
69
|
|
|
* |
70
|
|
|
* @param string $handler |
71
|
|
|
* @param array $parameters |
72
|
|
|
*/ |
73
|
|
|
private function found($handler, $parameters) |
74
|
|
|
{ |
75
|
|
|
/* its a closure */ |
76
|
|
|
if (is_callable($handler)) { |
77
|
|
|
return $this->handle($handler, $parameters); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* its a class */ |
81
|
|
|
list($class, $method) = explode("@", $handler, 2); |
82
|
|
|
$class = $this->namespace . $class; |
83
|
|
|
return $this->handle([new $class, $method], $parameters); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Handle the callback. |
88
|
|
|
* |
89
|
|
|
* @param string $callback |
90
|
|
|
* @param array $parameters |
91
|
|
|
*/ |
92
|
|
|
public function handle($callback, ...$parameters) |
93
|
|
|
{ |
94
|
|
|
$callback = call_user_func_array($callback, ...$parameters); |
95
|
|
|
|
96
|
|
|
echo (is_array($callback)) ? json_encode($callback) : $callback; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Error responce. |
101
|
|
|
* |
102
|
|
|
* @param string $type |
103
|
|
|
* @param string $description |
104
|
|
|
*/ |
105
|
|
|
private function error($type, $description) |
106
|
|
|
{ |
107
|
|
|
return View::render('errors.request', [ |
108
|
|
|
'title' => $type, |
109
|
|
|
'error_number' => $type, |
110
|
|
|
'error_message' => $description |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|