1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Middleware class |
5
|
|
|
* |
6
|
|
|
* @author Anderson Salas <[email protected]> |
7
|
|
|
* @copyright 2017 |
8
|
|
|
* @license GNU-3.0 |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Luthier\Core; |
13
|
|
|
|
14
|
|
|
class Middleware |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* CodeIgniter instance (in dynamic context) |
19
|
|
|
* |
20
|
|
|
* @var $CI |
21
|
|
|
* |
22
|
|
|
* @access protected |
23
|
|
|
*/ |
24
|
|
|
protected $CI; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* CodeIgniter instance (in static context) |
28
|
|
|
* |
29
|
|
|
* @var static $instance |
30
|
|
|
* |
31
|
|
|
* @access protected |
32
|
|
|
*/ |
33
|
|
|
protected static $instance; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Current URI string |
37
|
|
|
* |
38
|
|
|
* @var static $uri_string |
39
|
|
|
* |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected static $uri_string; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class constructor |
46
|
|
|
* |
47
|
|
|
* @return void |
|
|
|
|
48
|
|
|
* |
49
|
|
|
* @access public |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
$this->CI =& self::$instance; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Class initialization (in static context) |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
* |
61
|
|
|
* @access public |
62
|
|
|
* @static |
63
|
|
|
*/ |
64
|
|
|
public static function init() |
65
|
|
|
{ |
66
|
|
|
self::$instance =& get_instance(); |
67
|
|
|
self::$uri_string = self::$instance->router->uri->uri_string(); |
68
|
|
|
|
69
|
|
|
// Execute user defined middleware: |
70
|
|
|
self::routeMiddleware(); |
71
|
|
|
|
72
|
|
|
// Execute Luthier's internal middleware |
73
|
|
|
$internalMiddleware = dirname(__DIR__).DIRECTORY_SEPARATOR.'Middleware'.DIRECTORY_SEPARATOR; |
74
|
|
|
require $internalMiddleware.'Request.php'; |
75
|
|
|
|
76
|
|
|
$request = new \Luthier\Middleware\Request(); |
77
|
|
|
$request->run(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Executes the current route middleware (if any) |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
* |
85
|
|
|
* @access public |
86
|
|
|
* @static |
87
|
|
|
*/ |
88
|
|
|
public static function routeMiddleware() |
89
|
|
|
{ |
90
|
|
|
$currentRoute = Route::getRouteByPath(self::$uri_string); |
91
|
|
|
|
92
|
|
|
$_run = array(); |
93
|
|
|
|
94
|
|
|
// Current route middleware |
95
|
|
|
if($currentRoute !== FALSE) |
96
|
|
|
{ |
97
|
|
|
foreach($currentRoute->middleware as $middleware) |
98
|
|
|
{ |
99
|
|
|
if(!in_array($middleware,$_run)) |
100
|
|
|
$_run[] = $middleware; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach($_run as $middleware) |
105
|
|
|
{ |
106
|
|
|
self::runMiddleware($middleware); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Run middleware |
112
|
|
|
* |
113
|
|
|
* @param string $middlewareName |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
* |
117
|
|
|
* @access private |
118
|
|
|
* @static |
119
|
|
|
*/ |
120
|
|
|
private static function runMiddleware($middlewareName) |
121
|
|
|
{ |
122
|
|
|
$middlewareDir = APPPATH.'middleware'.DIRECTORY_SEPARATOR; |
123
|
|
|
|
124
|
|
|
$middlewareOriginalName = $middlewareName; |
125
|
|
|
|
126
|
|
|
$middlewareName = ucfirst($middlewareName).'_middleware'; |
127
|
|
|
|
128
|
|
|
if(!file_exists($middlewareDir)) |
129
|
|
|
show_error('Unable to find (or read) the middleware folder: "'.$middlewareDir.'"'); |
130
|
|
|
|
131
|
|
|
$runMiddleware = $middlewareDir.$middlewareName.'.php'; |
132
|
|
|
|
133
|
|
|
if(!file_exists($runMiddleware)) |
134
|
|
|
show_error('Unable to find (or read) the middleware "'.$runMiddleware.'"'); |
135
|
|
|
|
136
|
|
|
require $runMiddleware; |
137
|
|
|
|
138
|
|
|
if(!class_exists($middlewareName)) |
139
|
|
|
show_error('Class "'.$middlewareName.'" not found'); |
140
|
|
|
|
141
|
|
|
$middleware = new $middlewareName(); |
142
|
|
|
|
143
|
|
|
// Call the current controller __beforeMiddleware() method, if exists: |
144
|
|
|
if(method_exists(self::$instance, '_beforeMiddleware')) |
145
|
|
|
self::$instance->_beforeMiddleware(); |
|
|
|
|
146
|
|
|
|
147
|
|
|
// Run the middleware |
148
|
|
|
$middleware->run(); |
149
|
|
|
|
150
|
|
|
// Call the current controller _afterMiddleware() method, if exists: |
151
|
|
|
if(method_exists(self::$instance, '_afterMiddleware')) |
152
|
|
|
self::$instance->_afterMiddleware(); |
|
|
|
|
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.