-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipeline.php
172 lines (162 loc) · 6.93 KB
/
Pipeline.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace Dakujem\Cumulus;
/**
* Creating middleware and pipelines in a breeze...
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
final class Pipeline
{
/**
* Builds a simple FIFO callable pipeline that executes all callables in $stages
* and passes their return value from one to another, returning the result of the last one.
*
* Pipeline diagram for [Stage1, Stage2, Stage3]:
*
* +--------+ +--------+ +--------+
* input --> | Stage1 | --> | Stage2 | --> | Stage3 | --> result
* +--------+ +--------+ +--------+
*
* @param callable[]|iterable $stages an array of callables with signature fn($x):$y
* @return callable Returns a function with signature fn($x=null):$y
*/
public static function tube(iterable $stages): callable
{
return function ($passable = null) use ($stages) {
foreach ($stages as $stage) {
$passable = $stage($passable);
}
return $passable;
};
// ^ rough equivalent of map-reduce:
// return function ($passable = null) use ($stages) {
// return array_reduce($stages, function ($passable, callable $callable) {
// return $callable($passable);
// }, $passable);
// };
}
/**
* Builds a LIFO callable pipeline.
* The stages are executed in reversed order - LIFO - the last stage is executed first!
*
* These pipelines are commonly used as middleware dispatchers
* and resemble an onion with layers added on top of each other.
* The outer-most layer is executed first.
*
* The pipeline is interruptible.
*
* Pipeline diagram for [Stage1, Stage2, Stage3]:
*
* +--------+ +--------+ +------------+
* input --> | | --> | | --> | ---+ |
* | Stage3 | | Stage2 | | Stage1 | |
* result <-- | | <-- | | <-- | <--+ |
* +--------+ +--------+ +------------+
*
* @param callable[] $stages an array of callables with signature fn($x,callable $next):$y
* @return callable Returns a callable with signature fn($x):$y
*/
public static function onion(iterable $stages): callable
{
return static::buildInvertedMiddlewareDispatcher(
array_reverse(is_array($stages) ? $stages : iterator_to_array($stages))
);
}
/**
* Builds a FIFO callable pipeline.
*
* Layers are executed in the provided order - the first stage is executed first.
* This execution is inverted (inside-out) compared to the traditional onion model used for middleware.
*
* The pipeline is interruptible.
*
* Pipeline diagram for [Stage1, Stage2, Stage3]:
*
* +--------+ +--------+ +------------+
* input --> | | --> | | --> | ---+ |
* | Stage1 | | Stage2 | | Stage3 | |
* result <-- | | <-- | | <-- | <--+ |
* +--------+ +--------+ +------------+
*
* @param callable[] $stages an array of callables with signature fn($x,callable $next):$y
* @return callable Returns a callable with signature fn($x):$y
*/
public static function invertedOnion(iterable $stages): callable
{
return static::buildInvertedMiddlewareDispatcher(
is_array($stages) ? $stages : iterator_to_array($stages)
);
}
/**
* Returns an identity function:
* f(x)=x, for every x
*
* @return callable fn($x):$x
*/
public static function identity(): callable
{
// return fn($v) => $v; // PHP 7.4 onwards
return function ($v) {
return $v;
};
}
/**
* @internal
*
* TODO Possible performance optimizations:
* An algorithm might exist that would not need to convert a Traversable object into an array.
* An algorithm might exist that would not need to reverse the stages in the default "onion" mode.
* For real world use both of these optimizations would probably be negligible though.
*
* @param callable[] $reversedMiddlewareStack
* @return callable Returns a callable with signature fn($x):$y
*/
private static function buildInvertedMiddlewareDispatcher(array $reversedMiddlewareStack): callable
{
if ($reversedMiddlewareStack !== []) {
//
// Recursive function that builds a callable onion pipeline from an array of callable stages.
//
$buildPipeline = null;
$buildPipeline = function (array $pipeline, ?callable $previous) use (&$buildPipeline): callable {
//
// When the pipeline is not empty, it contains "stages".
// Stages are callables with signature fn($x,$next):$y.
//
// At this point, the current stage is wrapped in a "wrapper" closure with signature fn($x):$y
// which is then passed as the $next argument to the following stage.
//
// Repeat recursively until the pipeline is empty.
//
if ($pipeline !== []) {
// Note: array_pop (with array_reverse) is used instead of array_shift for performance reasons
$current = array_pop($pipeline);
// Note: The wrapper function from the previous iteration becomes the $next argument for current stage.
// $next = fn($value) => $current($value, $previous ?? static::identity()); // PHP 7.4 onwards
$next = function ($value) use ($current, $previous) {
return $current($value, $previous ?? static::identity());
};
// Note: The $next wrapper becomes $previous in the following iteration.
return $buildPipeline($pipeline, $next);
}
//
// At the end of the recursion, when the pipeline is empty,
// return a function that resolves the first stage in the (reversed) pipeline.
//
// Note:
// The $previous function always has signature fn($val):$result,
// as it is provided by the next stage using static closure variables.
//
// return fn($value) => $previous($value); // PHP 7.4 onwards
return function ($value) use ($previous) {
return $previous($value);
};
};
// Initiate the recursive function.
return $buildPipeline($reversedMiddlewareStack, null);
}
// In case the pipeline is empty, return an identity function f(x)=x right away.
return static::identity();
}
}