-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRouter.php
More file actions
135 lines (113 loc) · 3.63 KB
/
Router.php
File metadata and controls
135 lines (113 loc) · 3.63 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
<?php
/**
* User: TheCodeholic
* Date: 7/7/2020
* Time: 10:01 AM
*/
namespace thecodeholic\phpmvc;
use thecodeholic\phpmvc\exception\NotFoundException;
/**
* Class Router
*
* @author Zura Sekhniashvili <zurasekhniashvili@gmail.com>
* @package thecodeholic\mvc
*/
class Router
{
private Request $request;
private Response $response;
private array $routeMap = [];
public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
}
public function get(string $url, $callback)
{
$this->routeMap['get'][$url] = $callback;
}
public function post(string $url, $callback)
{
$this->routeMap['post'][$url] = $callback;
}
/**
* @return array
*/
public function getRouteMap($method): array
{
return $this->routeMap[$method] ?? [];
}
public function getCallback()
{
$method = $this->request->getMethod();
$url = $this->request->getUrl();
// Trim slashes
$url = trim($url, '/');
// Get all routes for current request method
$routes = $this->getRouteMap($method);
$routeParams = false;
// Start iterating registed routes
foreach ($routes as $route => $callback) {
// Trim slashes
$route = trim($route, '/');
$routeNames = [];
if (!$route) {
continue;
}
// Find all route names from route and save in $routeNames
if (preg_match_all('/\{(\w+)(:[^}]+)?}/', $route, $matches)) {
$routeNames = $matches[1];
}
// Convert route name into regex pattern
$routeRegex = "@^" . preg_replace_callback('/\{\w+(:([^}]+))?}/', fn($m) => isset($m[2]) ? "({$m[2]})" : '(\w+)', $route) . "$@";
// Test and match current route against $routeRegex
if (preg_match_all($routeRegex, $url, $valueMatches)) {
$values = [];
for ($i = 1; $i < count($valueMatches); $i++) {
$values[] = $valueMatches[$i][0];
}
$routeParams = array_combine($routeNames, $values);
$this->request->setRouteParams($routeParams);
return $callback;
}
}
return false;
}
public function resolve()
{
$method = $this->request->getMethod();
$url = $this->request->getUrl();
$callback = $this->routeMap[$method][$url] ?? false;
if (!$callback) {
$callback = $this->getCallback();
if ($callback === false) {
throw new NotFoundException();
}
}
if (is_string($callback)) {
return $this->renderView($callback);
}
if (is_array($callback)) {
/**
* @var $controller \thecodeholic\phpmvc\Controller
*/
$controller = new $callback[0];
$controller->action = $callback[1];
Application::$app->controller = $controller;
$middlewares = $controller->getMiddlewares();
foreach ($middlewares as $middleware) {
$middleware->execute();
}
$callback[0] = $controller;
}
return call_user_func($callback, $this->request, $this->response);
}
public function renderView($view, $params = [])
{
return Application::$app->view->renderView($view, $params);
}
public function renderViewOnly($view, $params = [])
{
return Application::$app->view->renderViewOnly($view, $params);
}
}