1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Middlewarize; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Responsable; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Pipeline\Pipeline as CorePipe; |
8
|
|
|
|
9
|
|
|
class Pipeline extends CorePipe |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get a Closure that represents a slice of the application onion. |
13
|
|
|
* |
14
|
|
|
* @return \Closure |
15
|
|
|
*/ |
16
|
7 |
|
protected function carry() |
17
|
|
|
{ |
18
|
|
|
return function ($stack, $pipe) { |
19
|
|
|
return function ($passable) use ($stack, $pipe) { |
20
|
7 |
|
if (is_callable($pipe)) { |
21
|
|
|
// If the pipe is an instance of a Closure, we will just call it directly but |
22
|
|
|
// otherwise we'll resolve the pipes out of the container and call it with |
23
|
|
|
// the appropriate method and arguments, returning the results back out. |
24
|
|
|
return $pipe($passable, $stack); |
25
|
7 |
|
} elseif (! is_object($pipe)) { |
26
|
6 |
|
[$name, $parameters] = $this->parsePipeString($pipe); |
|
|
|
|
27
|
|
|
|
28
|
|
|
// If the pipe is a string we will parse the string and resolve the class out |
29
|
|
|
// of the dependency injection container. We can then build a callable and |
30
|
|
|
// execute the pipe function giving in the parameters that are required. |
31
|
6 |
|
$name = explode('@', $name); |
|
|
|
|
32
|
6 |
|
$pipe = $this->getContainer()->make($name[0]); |
|
|
|
|
33
|
|
|
|
34
|
6 |
|
$parameters = array_merge([$passable, $stack], $parameters); |
|
|
|
|
35
|
|
|
} else { |
36
|
|
|
// If the pipe is already an object we'll just make a callable and pass it to |
37
|
|
|
// the pipe as-is. There is no need to do any extra parsing and formatting |
38
|
|
|
// since the object we're given was already a fully instantiated object. |
39
|
1 |
|
$parameters = [$passable, $stack]; |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
$method = $name[1] ?? $this->method; |
43
|
|
|
|
44
|
7 |
|
return method_exists($pipe, $method) |
45
|
7 |
|
? $pipe->{$method}(...$parameters) |
46
|
7 |
|
: $pipe(...$parameters); |
47
|
7 |
|
}; |
48
|
7 |
|
}; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
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.