-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
116 lines (100 loc) · 4.36 KB
/
Router.php
File metadata and controls
116 lines (100 loc) · 4.36 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
<?php
namespace NGFramer\NGFramerPHPBase;
use App\Config\ApplicationConfig;
use Exception;
use NGFramer\NGFramerPHPBase\Controller\Controller;
use NGFramer\NGFramerPHPBase\Defaults\Exceptions\CallbackException;
use NGFramer\NGFramerPHPBase\Defaults\Exceptions\MiddlewareException;
use NGFramer\NGFramerPHPBase\Defaults\Exceptions\RegistryException;
use NGFramer\NGFramerPHPBase\Middleware\BaseMiddleware;
use NGFramer\NGFramerPHPBase\Registry\RegistryGetter;
use NGFramer\NGFramerPHPExceptions\Exceptions\BaseException;
class Router
{
/**
* Instance of the application class from constructor.
* @var Application $application
*/
public Application $application;
/**
* Instance of the request class from constructor.
* @var Request $request
*/
public Request $request;
/**
* Instance of the registry getter.
* @var RegistryGetter $registry
*/
public RegistryGetter $registry;
/**
* Constructor for the Router class.
*/
public function __construct()
{
$this->application = Application::$application;
$this->request = $this->application->request;
$this->registry = new RegistryGetter();
}
/**
* Function determining URL path, and method, and execute the callback.
*
* @throws MiddlewareException
* @throws CallbackException
* @throws RegistryException
*
* TODO: Look out for the working of getting the middleware for the controller and the path and method request.
* TODO: Check if the callback is a controller or a closure/callback.
* TODO: Check if the executeCallback and process function works.
*/
public function handleRoute(): void
{
$method = $this->request->getMethod();
$path = $this->request->getPath();
// Determine the callback associated with the requested path and method.
$callback = $this->registry->getCallback($method, $path);
// Check if the callback exists.
if (empty($callback)) {
throw new CallbackException("No callback associated for path '$path'.", 1004004, 'base.callback.notFound');
}
try {
// Get all the individual and global middlewares for the requested path.
$individualMiddlewares = $this->registry->getMiddleware($method, $path, $callback);
$globalMiddlewares = $this->registry->getGlobalMiddleware();
$middlewares = array_merge($individualMiddlewares, $globalMiddlewares);
} catch (BaseException $exception) {
throw new MiddlewareException("Error processing middleware for route '$path'. Error: {$e->getMessage()}", 1004001, 'base.middleware.processingError');
}
// If the middleware exists.
if (!empty($middlewares)) {
// Loop across all the middlewares.
foreach ($middlewares as $middleware) {
// Check if the middleware is an instance or just a class string.
if (!$middleware instanceof BaseMiddleware) {
$middleware = new $middleware();
}
// Execute the middleware.
$middleware->execute($this->request, $callback);
}
}
// Check for the callback type.
if (is_string($callback)) {
if (is_callable($callback)) {
call_user_func($callback);
} else {
throw new CallbackException("Invalid callback passed.", 1002002, 'base.callback.invalid');
}
} elseif (is_array($callback)) {
// Callback => [0] = Controller, [1] = Method.
$callbackClass = $callback[0];
$callback[0] = new $callback[0];
// Check if callback is a subclass of the Controller class.
if (!is_subclass_of($callback[0], Controller::class)) {
throw new CallbackException("Invalid callback passed. The callback is not an instance of Controller.", 1002003, 'base.callback.invalidController');
}
// Let's call the controller method.
is_callable($callback) ? call_user_func($callback) : throw new CallbackException("Invalid callback passed. $callbackClass->$callback[1]()", 1002004, 'framework.callback.invalidCallable');
} else {
throw new CallbackException("Invalid callback passed.", 1002005, 'base.callback.invalidCallable.2');
}
}
}