|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
|
5
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
|
6
|
|
|
* |
|
7
|
|
|
* Licensed under The MIT License |
|
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
10
|
|
|
* |
|
11
|
|
|
* @link https://github.com/knut7/framework/ for the canonical source repository |
|
12
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
|
13
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
|
14
|
|
|
* @author Marcio Zebedeu - [email protected] |
|
15
|
|
|
* @version 1.0.14 |
|
16
|
|
|
* |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Ballybran\Routing\Router; |
|
21
|
|
|
|
|
22
|
|
|
class RouterCommand |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var RouterCommand|null Class instance variable |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $instance = null; |
|
28
|
|
|
|
|
29
|
|
|
protected $baseFolder; |
|
30
|
|
|
protected $paths; |
|
31
|
|
|
protected $namespaces; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* RouterCommand constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param $baseFolder |
|
37
|
|
|
* @param $paths |
|
38
|
|
|
* @param $namespaces |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($baseFolder, $paths, $namespaces) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->baseFolder = $baseFolder; |
|
43
|
|
|
$this->paths = $paths; |
|
44
|
|
|
$this->namespaces = $namespaces; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getMiddlewareInfo() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
'path' => $this->baseFolder . '/' . $this->paths['middlewares'], |
|
51
|
|
|
'namespace' => $this->namespaces['middlewares'], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getControllerInfo() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
'path' => $this->baseFolder . '/' . $this->paths['controllers'], |
|
59
|
|
|
'namespace' => $this->namespaces['controllers'], |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $baseFolder |
|
65
|
|
|
* @param $paths |
|
66
|
|
|
* @param $namespaces |
|
67
|
|
|
* |
|
68
|
|
|
* @return RouterCommand|static |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function getInstance($baseFolder, $paths, $namespaces) |
|
71
|
|
|
{ |
|
72
|
|
|
if (null === self::$instance) { |
|
73
|
|
|
self::$instance = new static($baseFolder, $paths, $namespaces); |
|
74
|
|
|
} |
|
75
|
|
|
return self::$instance; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Throw new Exception for Router Error |
|
80
|
|
|
* |
|
81
|
|
|
* @param $message |
|
82
|
|
|
* |
|
83
|
|
|
* @return RouterException |
|
84
|
|
|
* @throws |
|
85
|
|
|
*/ |
|
86
|
|
|
public function exception($message = '') |
|
87
|
|
|
{ |
|
88
|
|
|
return new RouterException($message); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Run Route Middlewares |
|
93
|
|
|
* |
|
94
|
|
|
* @param $command |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed|void |
|
97
|
|
|
* @throws |
|
98
|
|
|
*/ |
|
99
|
|
|
public function beforeAfter($command) |
|
100
|
|
|
{ |
|
101
|
|
|
if (! is_null($command)) { |
|
102
|
|
|
$info = $this->getMiddlewareInfo(); |
|
103
|
|
|
if (is_array($command)) { |
|
104
|
|
|
foreach ($command as $value) { |
|
105
|
|
|
$this->beforeAfter($value); |
|
106
|
|
|
} |
|
107
|
|
|
} elseif (is_string($command)) { |
|
108
|
|
|
$middleware = explode(':', $command); |
|
109
|
|
|
$params = []; |
|
110
|
|
|
if (count($middleware) > 1) { |
|
111
|
|
|
$params = explode(',', $middleware[1]); |
|
112
|
|
|
} |
|
113
|
|
|
$controller = $this->resolveClass($middleware[0], $info['path'], $info['namespace']); |
|
114
|
|
|
if (method_exists($controller, 'handle')) { |
|
115
|
|
|
$response = call_user_func_array([$controller, 'handle'], $params); |
|
116
|
|
|
if ($response !== true) { |
|
117
|
|
|
echo $response; |
|
118
|
|
|
exit; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $response; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->exception('handle() method is not found in <b>'.$command.'</b> class.'); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Run Route Command; Controller or Closure |
|
133
|
|
|
* |
|
134
|
|
|
* @param $command |
|
135
|
|
|
* @param $params |
|
136
|
|
|
* |
|
137
|
|
|
* @return mixed|void |
|
138
|
|
|
* @throws |
|
139
|
|
|
*/ |
|
140
|
|
|
public function runRoute($command, $params = null) |
|
141
|
|
|
{ |
|
142
|
|
|
$info = $this->getControllerInfo(); |
|
143
|
|
|
if (! is_object($command)) { |
|
144
|
|
|
$segments = explode('@', $command); |
|
145
|
|
|
$controllerClass = str_replace([$info['namespace'], '\\', '.'], ['', '/', '/'], $segments[0]); |
|
146
|
|
|
$controllerMethod = $segments[1]; |
|
147
|
|
|
|
|
148
|
|
|
$controller = $this->resolveClass($controllerClass, $info['path'], $info['namespace']); |
|
149
|
|
|
if (method_exists($controller, $controllerMethod)) { |
|
150
|
|
|
echo $this->runMethodWithParams([$controller, $controllerMethod], $params); |
|
151
|
|
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $this->exception($controllerMethod . ' method is not found in '.$controllerClass.' class.'); |
|
155
|
|
|
} else { |
|
156
|
|
|
echo $this->runMethodWithParams($command, $params); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Resolve Controller or Middleware class. |
|
164
|
|
|
* |
|
165
|
|
|
* @param $class |
|
166
|
|
|
* @param $path |
|
167
|
|
|
* @param $namespace |
|
168
|
|
|
* |
|
169
|
|
|
* @return object |
|
170
|
|
|
* @throws |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function resolveClass($class, $path, $namespace) |
|
173
|
|
|
{ |
|
174
|
|
|
$file = realpath(rtrim($path, '/') . '/' . $class . '.php'); |
|
175
|
|
|
if (! file_exists($file)) { |
|
176
|
|
|
return $this->exception($class . ' class is not found. Please, check file.'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$class = $namespace . str_replace('/', '\\', $class); |
|
180
|
|
|
if (!class_exists($class)) { |
|
181
|
|
|
require_once($file); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return new $class(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param $function |
|
189
|
|
|
* @param $params |
|
190
|
|
|
* |
|
191
|
|
|
* @return mixed |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function runMethodWithParams($function, $params) |
|
194
|
|
|
{ |
|
195
|
|
|
return call_user_func_array($function, (!is_null($params) ? $params : [])); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.