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