-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
272 lines (226 loc) · 8.89 KB
/
Copy pathexample.php
File metadata and controls
272 lines (226 loc) · 8.89 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
declare(strict_types=1);
namespace ItalyStrap\Pipeline;
require(__DIR__ . '/vendor/autoload.php');
// =============================================================================
// Basic Pipeline Usage
// =============================================================================
echo "=== Basic Pipeline Usage ===" . PHP_EOL . PHP_EOL;
echo "--- Dispatching with a handler ---" . PHP_EOL;
$handler = new Pipeline(new CallbackHandler(static fn (object $message): string => 'Handled: ' . $message->name));
$command = new class () {
public string $name = 'CreateUser';
};
$result = $handler->handle($command);
echo "Result: {$result}" . PHP_EOL . PHP_EOL;
echo "--- Dispatching without a handler ---" . PHP_EOL;
$handler = new Pipeline();
try {
$handler->handle(new \stdClass());
} catch (\RuntimeException $e) {
echo "Caught: {$e->getMessage()}" . PHP_EOL;
}
echo PHP_EOL;
// =============================================================================
// Single-Use Guard
// =============================================================================
echo "=== Single-Use Guard ===" . PHP_EOL . PHP_EOL;
$handler = new Pipeline(new CallbackHandler(static fn (object $message): string => 'ok'));
$handler->handle(new \stdClass());
echo "First dispatch: ok" . PHP_EOL;
try {
$handler->handle(new \stdClass());
} catch (\RuntimeException $e) {
echo "Second dispatch caught: {$e->getMessage()}" . PHP_EOL;
}
echo PHP_EOL;
// =============================================================================
// Middleware Pipeline
// =============================================================================
echo "=== Middleware Pipeline ===" . PHP_EOL . PHP_EOL;
echo "--- Middleware execution order ---" . PHP_EOL;
$handler = new Pipeline(
new CallbackHandler(static function (object $message): string {
echo " [handler] Final handler reached" . PHP_EOL;
return 'done';
}),
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [middleware-1] Before" . PHP_EOL;
$result = $handler->handle($message);
echo " [middleware-1] After" . PHP_EOL;
return $result;
}
},
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [middleware-2] Before" . PHP_EOL;
$result = $handler->handle($message);
echo " [middleware-2] After" . PHP_EOL;
return $result;
}
},
);
$result = $handler->handle(new \stdClass());
echo "Result: {$result}" . PHP_EOL . PHP_EOL;
// =============================================================================
// Short-Circuiting
// =============================================================================
echo "=== Short-Circuiting ===" . PHP_EOL . PHP_EOL;
$handler = new Pipeline(
new CallbackHandler(static function (object $message): string {
echo " [handler] This should NOT be reached" . PHP_EOL;
return 'unreachable';
}),
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [auth] Unauthorized — stopping pipeline" . PHP_EOL;
return 'access-denied';
}
},
);
$result = $handler->handle(new \stdClass());
echo "Result: {$result}" . PHP_EOL . PHP_EOL;
// =============================================================================
// Middleware Mutating Message State
// =============================================================================
echo "=== Middleware Mutating Message State ===" . PHP_EOL . PHP_EOL;
$message = new class () {
private int $value = 0;
public function increment(): void
{
$this->value++;
}
public function value(): int
{
return $this->value;
}
};
$handler = new Pipeline(
new CallbackHandler(static function (object $message): int {
echo " [handler] Final value: {$message->value()}" . PHP_EOL;
return $message->value();
}),
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
$message->increment();
echo " [middleware-1] Incremented to: {$message->value()}" . PHP_EOL;
return $handler->handle($message);
}
},
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
$message->increment();
echo " [middleware-2] Incremented to: {$message->value()}" . PHP_EOL;
return $handler->handle($message);
}
},
);
$result = $handler->handle($message);
echo "Result: {$result}" . PHP_EOL . PHP_EOL;
// =============================================================================
// CallbackHandler
// =============================================================================
echo "=== CallbackHandler ===" . PHP_EOL . PHP_EOL;
$handler = new CallbackHandler(static fn (object $message): string => 'Callback handled: ' . get_class($message));
$result = $handler->handle(new \stdClass());
echo "Result: {$result}" . PHP_EOL . PHP_EOL;
// =============================================================================
// DecorateHandler — Immutable & Reusable
// =============================================================================
echo "=== DecorateHandler ===" . PHP_EOL . PHP_EOL;
echo "--- Single decoration ---" . PHP_EOL;
$decorated = new DecorateHandler(
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [logging] Before" . PHP_EOL;
$result = $handler->handle($message);
echo " [logging] After" . PHP_EOL;
return $result;
}
},
new CallbackHandler(static fn (object $message): string => 'inner-result'),
);
$result = $decorated->handle(new \stdClass());
echo "Result: {$result}" . PHP_EOL . PHP_EOL;
echo "--- Nested decoration ---" . PHP_EOL;
$pipeline = new DecorateHandler(
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [outer] Before" . PHP_EOL;
$result = $handler->handle($message);
echo " [outer] After" . PHP_EOL;
return $result;
}
},
new DecorateHandler(
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [inner] Before" . PHP_EOL;
$result = $handler->handle($message);
echo " [inner] After" . PHP_EOL;
return $result;
}
},
new CallbackHandler(static function (object $message): string {
echo " [handler] Reached" . PHP_EOL;
return 'done';
}),
),
);
$result = $pipeline->handle(new \stdClass());
echo "Result: {$result}" . PHP_EOL . PHP_EOL;
echo "--- Reusability (calling handle multiple times) ---" . PHP_EOL;
$reusable = new DecorateHandler(
new class () implements MiddlewareInterface {
private int $callCount = 0;
public function process(object $message, HandlerInterface $handler): mixed
{
$this->callCount++;
echo " Call #{$this->callCount}" . PHP_EOL;
return $handler->handle($message);
}
},
new CallbackHandler(static fn (object $message): string => 'ok'),
);
$reusable->handle(new \stdClass());
$reusable->handle(new \stdClass());
$reusable->handle(new \stdClass());
echo PHP_EOL;
// =============================================================================
// Composing Pipeline + DecorateHandler
// =============================================================================
echo "=== Composing Pipeline + DecorateHandler ===" . PHP_EOL . PHP_EOL;
$reusableInner = new DecorateHandler(
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [validation] Always runs" . PHP_EOL;
return $handler->handle($message);
}
},
new CallbackHandler(static function (object $message): string {
echo " [handler] Persisting..." . PHP_EOL;
return 'persisted';
}),
);
$handler = new Pipeline(
$reusableInner,
new class () implements MiddlewareInterface {
public function process(object $message, HandlerInterface $handler): mixed
{
echo " [request-logging] Dynamic, per-request middleware" . PHP_EOL;
return $handler->handle($message);
}
},
);
$result = $handler->handle(new \stdClass());
echo "Result: {$result}" . PHP_EOL;