-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced-features.php
More file actions
255 lines (202 loc) · 7 KB
/
advanced-features.php
File metadata and controls
255 lines (202 loc) · 7 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
<?php
require __DIR__ . '/../vendor/autoload.php';
use tommyknocker\chain\Chain;
use tommyknocker\chain\ChainConfig;
use tommyknocker\chain\ChainExtensionInterface;
use tommyknocker\chain\tests\fixtures\Calculator;
use tommyknocker\chain\tests\fixtures\User;
/*
* Advanced Chain Features Demo
*
* Demonstrates all the new enhanced features:
* - Enhanced conditional methods (whenAll, whenAny, whenNone)
* - Timeout protection
* - Configuration system
* - Extension system
* - Method caching
* - Specific exception types
*/
echo "\n🚀 Advanced Chain Features Demo\n";
echo str_repeat('=', 60) . "\n\n";
// ==================== Configuration Demo ====================
echo "1. Configuration System\n";
echo str_repeat('-', 60) . "\n";
// Set performance configuration
Chain::configure(ChainConfig::performance());
echo "✓ Performance configuration enabled\n";
// Set development configuration
Chain::configure(ChainConfig::development());
echo "✓ Development configuration enabled\n\n";
// ==================== Extension System Demo ====================
echo "2. Extension System\n";
echo str_repeat('-', 60) . "\n";
class LoggingExtension implements ChainExtensionInterface
{
private array $logs = [];
public function beforeMethodCall(string $method, array $args): void
{
$this->logs[] = "Before: {$method}(" . implode(', ', $args) . ')';
}
public function afterMethodCall(string $method, mixed $result): void
{
$this->logs[] = "After: {$method} -> " . (is_object($result) ? get_class($result) : gettype($result));
}
public function getLogs(): array
{
return $this->logs;
}
}
$logger = new LoggingExtension();
$result = Chain::of(new Calculator(10))
->addExtension($logger)
->add(5)
->multiply(2)
->getValue()
->get();
echo "Calculation result: $result\n";
echo "Extension logs:\n";
foreach ($logger->getLogs() as $log) {
echo " - $log\n";
}
echo "\n";
// ==================== Enhanced Conditional Methods Demo ====================
echo "3. Enhanced Conditional Methods\n";
echo str_repeat('-', 60) . "\n";
$user = new User('Alice', 25);
// whenAll - all conditions must be true
$result1 = Chain::of($user)
->whenAll(
fn ($u) => $u->isAdult(),
fn ($u) => strlen($u->getName()) > 3,
fn ($u) => $u->getAge() < 50
)
->getEmail()
->get();
echo "whenAll result: $result1 (should be [email protected])\n";
// whenAny - any condition can be true
$result2 = Chain::of($user)
->whenAny(
fn ($u) => $u->getAge() > 30, // false
fn ($u) => $u->isAdult(), // true
fn ($u) => $u->getAge() < 18 // false
)
->tap(fn ($u) => $u->addRole('verified'))
->getRoles()
->get();
echo 'whenAny result: ' . implode(', ', $result2) . " (should include 'verified')\n";
// whenNone - no conditions should be true
$result3 = Chain::of($user)
->whenNone(
fn ($u) => $u->getAge() > 30, // false
fn ($u) => $u->getAge() < 18, // false
fn ($u) => strlen($u->getName()) < 3 // false
)
->tap(fn ($u) => $u->addRole('special'))
->getRoles()
->get();
echo 'whenNone result: ' . implode(', ', $result3) . " (should include 'special')\n\n";
// ==================== Timeout Protection Demo ====================
echo "4. Timeout Protection\n";
echo str_repeat('-', 60) . "\n";
try {
$result = Chain::of(new Calculator(10))
->timeout(1, function ($calc) {
// Simulate a slow operation
usleep(500000); // 0.5 seconds
return $calc->add(5);
})
->getValue()
->get();
echo "✓ Operation completed within timeout: $result\n";
} catch (\tommyknocker\chain\Exception\ChainTimeoutException $e) {
echo '✗ Operation timed out: ' . $e->getMessage() . "\n";
}
try {
$result = Chain::of(new Calculator(10))
->timeout(1, function ($calc) {
// Simulate a very slow operation
sleep(2); // 2 seconds
return $calc->add(5);
})
->getValue()
->get();
echo "✓ Operation completed within timeout: $result\n";
} catch (\tommyknocker\chain\Exception\ChainTimeoutException $e) {
echo '✗ Operation timed out: ' . $e->getMessage() . "\n";
}
echo "\n";
// ==================== Error Handling with Specific Exceptions ====================
echo "5. Enhanced Error Handling\n";
echo str_repeat('-', 60) . "\n";
try {
Chain::of(new Calculator(10))
->nonExistentMethod();
} catch (\tommyknocker\chain\Exception\ChainMethodNotFoundException $e) {
echo '✓ Caught specific exception: ' . $e->getMessage() . "\n";
}
try {
Chain::of(new Calculator(10))
->map(fn ($c) => 'not-an-object');
} catch (\tommyknocker\chain\Exception\ChainInvalidOperationException $e) {
echo '✓ Caught invalid operation exception: ' . $e->getMessage() . "\n";
}
echo "\n";
// ==================== Complex Integration Demo ====================
echo "6. Complex Integration Example\n";
echo str_repeat('-', 60) . "\n";
class PerformanceMonitor implements ChainExtensionInterface
{
private array $timings = [];
public function beforeMethodCall(string $method, array $args): void
{
$this->timings[$method] = microtime(true);
}
public function afterMethodCall(string $method, mixed $result): void
{
if (isset($this->timings[$method])) {
$duration = (microtime(true) - $this->timings[$method]) * 1000;
echo " Method '$method' took " . round($duration, 2) . "ms\n";
}
}
}
$monitor = new PerformanceMonitor();
echo "Processing complex calculation with monitoring:\n";
$finalResult = Chain::of(new Calculator(100))
->addExtension($monitor)
->whenAll(
fn ($c) => $c->isPositive(),
fn ($c) => $c->getValue() > 50
)
->whenAny(
fn ($c) => $c->getValue() > 80,
fn ($c) => $c->getValue() < 200
)
->timeout(5, fn ($c) => $c->multiply(1.1))
->whenNone(
fn ($c) => $c->getValue() > 1000,
fn ($c) => $c->getValue() < 0
)
->pipe(
fn ($c) => $c->subtract(10),
fn ($c) => $c->multiply(0.9),
fn ($c) => round($c->getValue(), 2)
)
->get();
echo "Final result: $finalResult\n\n";
// ==================== Summary ====================
echo str_repeat('=', 60) . "\n";
echo "✅ All advanced features demonstrated successfully!\n\n";
echo "New features showcased:\n";
echo "✓ ChainConfig - Configuration system\n";
echo "✓ ChainExtensionInterface - Extension system\n";
echo "✓ whenAll() - All conditions must be true\n";
echo "✓ whenAny() - Any condition can be true\n";
echo "✓ whenNone() - No conditions should be true\n";
echo "✓ timeout() - Timeout protection\n";
echo "✓ Specific exception types\n";
echo "✓ Method caching (performance optimization)\n";
echo "✓ Enhanced error handling\n";
echo "✓ Complex integration scenarios\n";
echo "\n" . str_repeat('=', 60) . "\n";
echo "🎉 Advanced Chain Features Demo Complete!\n\n";