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