1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rAPId\Routing; |
4
|
|
|
|
5
|
|
|
use rAPId\Exceptions\MissingControllerException; |
6
|
|
|
use rAPId\Foundation\Response; |
7
|
|
|
use ReflectionMethod; |
8
|
|
|
|
9
|
|
|
class Router |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param $url |
13
|
|
|
* |
14
|
|
|
* @return Response |
15
|
|
|
* @throws MissingControllerException |
16
|
|
|
*/ |
17
|
|
|
public static function resolve($url) { |
18
|
|
|
$route = Route::parse($url); |
19
|
|
|
|
20
|
|
|
$controller = $route->getController(); |
21
|
|
|
$action = $route->getAction(); |
22
|
|
|
$args = self::getMethodSpecificArgs($route); |
23
|
|
|
|
24
|
|
|
$controller = new $controller(); |
25
|
|
|
|
26
|
|
|
if (empty($controller)) { |
27
|
|
|
throw new MissingControllerException("Call Failed to [$url]. Make sure your DEFAULT_CONTROLLER is set", 1); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$response = $controller->{$action}(...$args); |
31
|
|
|
return new Response($response); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns an array of arguments |
36
|
|
|
* in the order that they are to be called in the controller method |
37
|
|
|
* |
38
|
|
|
* @param Route $route |
39
|
|
|
* |
40
|
|
|
* @return array $args |
41
|
|
|
*/ |
42
|
|
|
private static function getMethodSpecificArgs(Route $route) { |
43
|
|
|
|
44
|
|
|
$parameters = self::getControllerMethodParameters($route->getController(), $route->getAction()); |
45
|
|
|
$route_args = $route->getArgs(); // Args from the url ie www.your-site.com/controller/method/arg1/arg2 |
46
|
|
|
$request_args = self::getRequestArgs(); // GET or POST request args |
47
|
|
|
|
48
|
|
|
$ordered_arguments = []; |
49
|
|
|
|
50
|
|
|
// Iterate through each of the accepted parameters for the controller |
51
|
|
|
foreach ($parameters as $parameter) { |
52
|
|
|
|
53
|
|
|
// Get value from request args, or fallback to default if one exists in the function definition |
54
|
|
|
$value = array_get($request_args, $parameter->name, self::getDefaultParameterValue($parameter)); |
55
|
|
|
|
56
|
|
|
// If there were no request args for this parameter, |
57
|
|
|
// use the next available route arg (if one exists) |
58
|
|
|
if (!array_key_exists($parameter->name, $request_args) && count($route_args) > 0) { |
59
|
|
|
$value = array_shift($route_args); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$ordered_arguments[] = $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $ordered_arguments; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private static function getDefaultParameterValue(\ReflectionParameter $parameter) { |
69
|
|
|
return $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $controller |
74
|
|
|
* @param string $method |
75
|
|
|
* |
76
|
|
|
* @return \ReflectionParameter[] |
77
|
|
|
*/ |
78
|
|
|
private static function getControllerMethodParameters($controller, $method) { |
79
|
|
|
$reflection_method = new ReflectionMethod($controller, $method); |
80
|
|
|
|
81
|
|
|
return $reflection_method->getParameters(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
private static function getRequestArgs() { |
88
|
|
|
return $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET; |
89
|
|
|
} |
90
|
|
|
} |