1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @link http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
namespace Infuse; |
12
|
|
|
|
13
|
|
|
class RouteResolver |
14
|
|
|
{ |
15
|
|
|
use HasApp; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $namespace; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $defaultController; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $defaultAction = 'index'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set the namespace. |
34
|
|
|
* |
35
|
|
|
* @param string $namespace |
36
|
|
|
* |
37
|
|
|
* @return self |
38
|
|
|
*/ |
39
|
|
|
public function setNamespace($namespace) |
40
|
|
|
{ |
41
|
|
|
$this->namespace = $namespace; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets the namespace. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getNamespace() |
52
|
|
|
{ |
53
|
|
|
return $this->namespace; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the default controller class. |
58
|
|
|
* |
59
|
|
|
* @param string $class |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
public function setDefaultController($class) |
64
|
|
|
{ |
65
|
|
|
$this->defaultController = $class; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Gets the default controller class. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getDefaultController() |
76
|
|
|
{ |
77
|
|
|
return $this->defaultController; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the default action. |
82
|
|
|
* |
83
|
|
|
* @param string $defaultAction |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
|
|
public function setDefaultAction($defaultAction) |
88
|
|
|
{ |
89
|
|
|
$this->defaultAction = $defaultAction; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the default action. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getDefaultAction() |
100
|
|
|
{ |
101
|
|
|
return $this->defaultAction; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Executes a route handler. |
106
|
|
|
* |
107
|
|
|
* @param array|string $route array('controller','method') or array('controller') |
108
|
|
|
* or 'method' |
109
|
|
|
* @param Request $req |
110
|
|
|
* @param Response $res |
111
|
|
|
* @param array $args |
112
|
|
|
* |
113
|
|
|
* @throws Exception when the route cannot be resolved. |
114
|
|
|
* |
115
|
|
|
* @return Response |
116
|
|
|
*/ |
117
|
|
|
public function resolve($route, $req, $res, array $args) |
118
|
|
|
{ |
119
|
|
|
$result = false; |
120
|
|
|
if (is_array($route) || is_string($route)) { |
121
|
|
|
// method name and controller supplied |
122
|
|
|
if (is_string($route) && $req->params('controller')) { |
123
|
|
|
$route = [$req->params('controller'), $route]; |
124
|
|
|
// method name supplied |
125
|
|
|
} elseif (is_string($route)) { |
126
|
|
|
$route = [$this->defaultController, $route]; |
127
|
|
|
// no method name? fallback to the index() method |
128
|
|
|
} elseif (count($route) == 1) { |
129
|
|
|
$route[] = $this->defaultAction; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
list($controller, $method) = $route; |
133
|
|
|
|
134
|
|
|
$controller = $this->namespace.'\\'.$controller; |
135
|
|
|
|
136
|
|
|
if (!class_exists($controller)) { |
137
|
|
|
throw new \Exception("Controller does not exist: $controller"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$controllerObj = new $controller(); |
141
|
|
|
|
142
|
|
|
// give the controller access to the DI container |
143
|
|
|
if (method_exists($controllerObj, 'setApp')) { |
144
|
|
|
$controllerObj->setApp($this->app); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// collect any preset route parameters |
148
|
|
|
if (isset($route[2])) { |
149
|
|
|
$params = $route[2]; |
150
|
|
|
$req->setParams($params); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$result = $controllerObj->$method($req, $res, $args); |
154
|
|
|
} elseif (is_callable($route)) { |
155
|
|
|
$result = call_user_func($route, $req, $res, $args); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($result instanceof View) { |
159
|
|
|
$res->render($result); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $res; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|