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 = [$this->router->route]; |
89
|
|
View Code Duplication |
foreach ($reflectionMethod->getParameters() as $arg) { |
|
|
|
|
90
|
|
|
if (!is_null($arg->getClass())) { |
91
|
|
|
$class = $arg->getClass()->name; |
92
|
|
|
array_unshift($dependencies,call_user_func_array($this->router->getConfig()['di'],[$class])); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return $reflectionMethod->invokeArgs($instance, $dependencies); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.