|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JetFire\Routing; |
|
4
|
|
|
use ReflectionMethod; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Middleware |
|
9
|
|
|
* @package JetFire\Routing |
|
10
|
|
|
*/ |
|
11
|
|
|
class Middleware |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Router |
|
16
|
|
|
*/ |
|
17
|
|
|
private $router; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Router $router |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(Router $router) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->router = $router; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @description global middleware |
|
29
|
|
|
*/ |
|
30
|
|
|
public function globalMiddleware() |
|
31
|
|
|
{ |
|
32
|
|
|
if (isset($this->router->collection->middleware['global_middleware'])) |
|
33
|
|
|
foreach ($this->router->collection->middleware['global_middleware'] as $mid) { |
|
34
|
|
|
if (class_exists($mid)) { |
|
35
|
|
|
$mid_global = call_user_func($this->router->getConfig()['di'],$mid); |
|
36
|
|
|
if (method_exists($mid_global, 'handle')) $this->callHandler($mid_global); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @description block middleware |
|
43
|
|
|
*/ |
|
44
|
|
|
public function blockMiddleware() |
|
45
|
|
|
{ |
|
46
|
|
|
if (isset($this->router->collection->middleware['block_middleware'])) |
|
47
|
|
|
if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getBlock()]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getBlock()])) { |
|
48
|
|
|
$class = $this->router->collection->middleware['block_middleware'][$this->router->route->getBlock()]; |
|
49
|
|
|
$mid_block = call_user_func($this->router->getConfig()['di'],$class); |
|
50
|
|
|
if (method_exists($mid_block, 'handle')) $this->callHandler($mid_block); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @description controller middleware |
|
56
|
|
|
*/ |
|
57
|
|
|
public function classMiddleware() |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($this->router->collection->middleware['class_middleware'])) { |
|
60
|
|
|
$ctrl = str_replace('\\', '/', $this->router->route->getTarget('controller')); |
|
61
|
|
|
if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
|
62
|
|
|
$class = $this->router->collection->middleware['class_middleware'][$ctrl]; |
|
63
|
|
|
$mid_class = call_user_func($this->router->getConfig()['di'],$class); |
|
64
|
|
|
if (method_exists($mid_class, 'handle')) $this->callHandler($mid_class); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @description route middleware |
|
71
|
|
|
*/ |
|
72
|
|
|
public function routeMiddleware() |
|
73
|
|
|
{ |
|
74
|
|
|
if (isset($this->router->collection->middleware['route_middleware'])) |
|
75
|
|
|
if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
76
|
|
|
$class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
77
|
|
|
$mid_route = call_user_func($this->router->getConfig()['di'],$class); |
|
78
|
|
|
if (method_exists($mid_route, 'handle')) $this->callHandler($mid_route); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $instance |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
private function callHandler($instance){ |
|
87
|
|
|
$reflectionMethod = new ReflectionMethod($instance, 'handle'); |
|
88
|
|
|
$dependencies = []; |
|
89
|
|
|
foreach ($reflectionMethod->getParameters() as $arg) |
|
90
|
|
|
if (!is_null($arg->getClass())) |
|
91
|
|
|
$dependencies[] = $this->getClass($arg->getClass()->name); |
|
92
|
|
|
$dependencies = array_merge($dependencies,[$this->router->route]); |
|
93
|
|
|
return $reflectionMethod->invokeArgs($instance, $dependencies); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $class |
|
98
|
|
|
* @return Route|RouteCollection|Router|mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
private function getClass($class){ |
|
101
|
|
|
switch($class){ |
|
102
|
|
|
case 'JetFire\Routing\Route': |
|
103
|
|
|
return $this->router->route; |
|
104
|
|
|
break; |
|
|
|
|
|
|
105
|
|
|
case 'JetFire\Routing\Router': |
|
106
|
|
|
return $this->router; |
|
107
|
|
|
break; |
|
|
|
|
|
|
108
|
|
|
case 'JetFire\Routing\RouteCollection': |
|
109
|
|
|
return $this->router->collection; |
|
110
|
|
|
break; |
|
|
|
|
|
|
111
|
|
|
default: |
|
112
|
|
|
return call_user_func_array($this->router->getConfig()['di'],[$class]); |
|
113
|
|
|
break; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.