1 | <?php |
||
25 | class Middleware |
||
26 | { |
||
27 | /** |
||
28 | * 中间件执行队列 |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $queue = []; |
||
32 | |||
33 | /** |
||
34 | * 应用对象 |
||
35 | * @var App |
||
36 | */ |
||
37 | protected $app; |
||
38 | |||
39 | 30 | public function __construct(App $app) |
|
43 | |||
44 | /** |
||
45 | * 导入中间件 |
||
46 | * @access public |
||
47 | * @param array $middlewares |
||
48 | * @param string $type 中间件类型 |
||
49 | * @return void |
||
50 | */ |
||
51 | 12 | public function import(array $middlewares = [], string $type = 'global'): void |
|
57 | |||
58 | /** |
||
59 | * 注册中间件 |
||
60 | * @access public |
||
61 | * @param mixed $middleware |
||
62 | * @param string $type 中间件类型 |
||
63 | * @return void |
||
64 | */ |
||
65 | 12 | public function add($middleware, string $type = 'global'): void |
|
74 | |||
75 | /** |
||
76 | * 注册路由中间件 |
||
77 | * @access public |
||
78 | * @param mixed $middleware |
||
79 | * @return void |
||
80 | */ |
||
81 | public function route($middleware): void |
||
85 | |||
86 | /** |
||
87 | * 注册控制器中间件 |
||
88 | * @access public |
||
89 | * @param mixed $middleware |
||
90 | * @return void |
||
91 | */ |
||
92 | 6 | public function controller($middleware): void |
|
96 | |||
97 | /** |
||
98 | * 注册中间件到开始位置 |
||
99 | * @access public |
||
100 | * @param mixed $middleware |
||
101 | * @param string $type 中间件类型 |
||
102 | */ |
||
103 | 3 | public function unshift($middleware, string $type = 'global') |
|
115 | |||
116 | /** |
||
117 | * 获取注册的中间件 |
||
118 | * @access public |
||
119 | * @param string $type 中间件类型 |
||
120 | * @return array |
||
121 | */ |
||
122 | 3 | public function all(string $type = 'global'): array |
|
126 | |||
127 | /** |
||
128 | * 调度管道 |
||
129 | * @access public |
||
130 | * @param string $type 中间件类型 |
||
131 | * @return Pipeline |
||
132 | */ |
||
133 | 24 | public function pipeline(string $type = 'global') |
|
152 | |||
153 | /** |
||
154 | * 结束调度 |
||
155 | * @param Response $response |
||
156 | */ |
||
157 | 6 | public function end(Response $response) |
|
171 | |||
172 | /** |
||
173 | * 异常处理 |
||
174 | * @param Request $passable |
||
175 | * @param Throwable $e |
||
176 | * @return Response |
||
177 | */ |
||
178 | 3 | public function handleException($passable, Throwable $e) |
|
187 | |||
188 | /** |
||
189 | * 解析中间件 |
||
190 | * @access protected |
||
191 | * @param mixed $middleware |
||
192 | * @param string $type 中间件类型 |
||
193 | * @return array |
||
194 | */ |
||
195 | 12 | protected function buildMiddleware($middleware, string $type): array |
|
223 | |||
224 | /** |
||
225 | * 中间件排序 |
||
226 | * @param array $middlewares |
||
227 | * @return array |
||
228 | */ |
||
229 | 24 | protected function sortMiddleware(array $middlewares) |
|
240 | |||
241 | /** |
||
242 | * 获取中间件优先级 |
||
243 | * @param $priority |
||
244 | * @param $middleware |
||
245 | * @return int |
||
246 | */ |
||
247 | 6 | protected function getMiddlewarePriority($priority, $middleware) |
|
256 | |||
257 | } |
||
258 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.