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 $class) |
34
|
|
|
if (class_exists($class)) $this->callHandler($class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @description block middleware |
39
|
|
|
*/ |
40
|
|
|
public function blockMiddleware() |
41
|
|
|
{ |
42
|
|
|
if (isset($this->router->collection->middleware['block_middleware'])) |
43
|
|
View Code Duplication |
if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
|
|
|
44
|
|
|
$class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
45
|
|
|
$this->callHandler($class); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @description controller middleware |
51
|
|
|
*/ |
52
|
|
|
public function classMiddleware() |
53
|
|
|
{ |
54
|
|
|
if (isset($this->router->collection->middleware['class_middleware'])) { |
55
|
|
|
$ctrl = str_replace('\\', '/', $this->router->route->getTarget('controller')); |
56
|
|
|
if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
57
|
|
|
$class = $this->router->collection->middleware['class_middleware'][$ctrl]; |
58
|
|
|
$this->callHandler($class); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @description route middleware |
65
|
|
|
*/ |
66
|
|
|
public function routeMiddleware() |
67
|
|
|
{ |
68
|
|
View Code Duplication |
if (isset($this->router->collection->middleware['route_middleware'])) |
|
|
|
|
69
|
|
|
if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
70
|
|
|
$class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
71
|
|
|
$this->callHandler($class); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $class |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
private function callHandler($class){ |
80
|
|
|
$instance = call_user_func($this->router->getConfig()['di'],$class); |
81
|
|
|
if (method_exists($instance, 'handle')) { |
82
|
|
|
$reflectionMethod = new ReflectionMethod($instance, 'handle'); |
83
|
|
|
$dependencies = []; |
84
|
|
|
foreach ($reflectionMethod->getParameters() as $arg) |
85
|
|
|
if (!is_null($arg->getClass())) |
86
|
|
|
$dependencies[] = $this->getClass($arg->getClass()->name); |
87
|
|
|
$dependencies = array_merge($dependencies, [$this->router->route]); |
88
|
|
|
$reflectionMethod->invokeArgs($instance, $dependencies); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $class |
94
|
|
|
* @return Route|RouteCollection|Router|mixed |
95
|
|
|
*/ |
96
|
|
|
private function getClass($class){ |
97
|
|
|
switch($class){ |
98
|
|
|
case 'JetFire\Routing\Route': |
99
|
|
|
return $this->router->route; |
100
|
|
|
break; |
|
|
|
|
101
|
|
|
case 'JetFire\Routing\Router': |
102
|
|
|
return $this->router; |
103
|
|
|
break; |
|
|
|
|
104
|
|
|
case 'JetFire\Routing\RouteCollection': |
105
|
|
|
return $this->router->collection; |
106
|
|
|
break; |
|
|
|
|
107
|
|
|
default: |
108
|
|
|
return call_user_func_array($this->router->getConfig()['di'],[$class]); |
109
|
|
|
break; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.