generated from farzai/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware-example.php
More file actions
281 lines (222 loc) · 8.01 KB
/
middleware-example.php
File metadata and controls
281 lines (222 loc) · 8.01 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
273
274
275
276
277
278
279
280
281
<?php
/**
* Custom Middleware Example
*
* This example demonstrates how to create and use custom middleware
* with Transport PHP to extend functionality.
*/
require __DIR__.'/../vendor/autoload.php';
use Farzai\Transport\Middleware\MiddlewareInterface;
use Farzai\Transport\TransportBuilder;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
echo "=== Custom Middleware Examples ===\n\n";
// Example 1: Simple Header Middleware
echo "1. Custom Header Middleware\n";
echo str_repeat('-', 50)."\n";
class CustomHeaderMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly string $headerName,
private readonly string $headerValue
) {}
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
// Add custom header to request
$request = $request->withHeader($this->headerName, $this->headerValue);
return $next($request);
}
}
$transport = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->withMiddleware(new CustomHeaderMiddleware('X-Custom-Header', 'my-value'))
->build();
try {
$response = $transport->get('/posts/1')->send();
echo "✓ Request completed with custom header\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 2: Request/Response Logging Middleware
echo "2. Request/Response Logging Middleware\n";
echo str_repeat('-', 50)."\n";
class DetailedLoggingMiddleware implements MiddlewareInterface
{
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
$startTime = microtime(true);
echo "→ Request:\n";
echo " Method: {$request->getMethod()}\n";
echo " URI: {$request->getUri()}\n";
echo " Headers:\n";
foreach ($request->getHeaders() as $name => $values) {
echo " {$name}: ".implode(', ', $values)."\n";
}
// Execute request
$response = $next($request);
$duration = round((microtime(true) - $startTime) * 1000, 2);
echo "\n← Response:\n";
echo " Status: {$response->getStatusCode()}\n";
echo " Duration: {$duration}ms\n";
echo " Headers:\n";
foreach ($response->getHeaders() as $name => $values) {
echo " {$name}: ".implode(', ', $values)."\n";
}
echo "\n";
return $response;
}
}
$transport2 = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->withoutDefaultMiddlewares() // Disable default logging
->withMiddleware(new DetailedLoggingMiddleware)
->build();
try {
$response = $transport2->get('/posts/1')->send();
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 3: Authentication Middleware
echo "3. API Key Authentication Middleware\n";
echo str_repeat('-', 50)."\n";
class ApiKeyAuthMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly string $apiKey,
private readonly string $headerName = 'X-API-Key'
) {}
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
// Add API key to all requests
$request = $request->withHeader($this->headerName, $this->apiKey);
return $next($request);
}
}
$transport3 = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->withMiddleware(new ApiKeyAuthMiddleware('your-api-key-here'))
->build();
try {
$response = $transport3->get('/posts/1')->send();
echo "✓ Request completed with API key authentication\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 4: Response Caching Middleware
echo "4. Simple Response Caching Middleware\n";
echo str_repeat('-', 50)."\n";
class SimpleCacheMiddleware implements MiddlewareInterface
{
private array $cache = [];
public function __construct(
private readonly int $ttlSeconds = 60
) {}
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
// Only cache GET requests
if ($request->getMethod() !== 'GET') {
return $next($request);
}
$cacheKey = (string) $request->getUri();
// Check cache
if (isset($this->cache[$cacheKey])) {
$cached = $this->cache[$cacheKey];
if (time() - $cached['time'] < $this->ttlSeconds) {
echo " ✓ Cache HIT for: {$cacheKey}\n\n";
return $cached['response'];
}
unset($this->cache[$cacheKey]);
}
echo " ✗ Cache MISS for: {$cacheKey}\n";
$response = $next($request);
// Store in cache
$this->cache[$cacheKey] = [
'response' => $response,
'time' => time(),
];
echo " ✓ Cached response\n\n";
return $response;
}
}
$cacheMiddleware = new SimpleCacheMiddleware(ttlSeconds: 60);
$transport4 = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->withMiddleware($cacheMiddleware)
->build();
try {
echo "First request (should MISS cache):\n";
$response1 = $transport4->get('/posts/1')->send();
echo "Second request (should HIT cache):\n";
$response2 = $transport4->get('/posts/1')->send();
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 5: Rate Limiting Middleware
echo "5. Simple Rate Limiting Middleware\n";
echo str_repeat('-', 50)."\n";
class RateLimitMiddleware implements MiddlewareInterface
{
private array $requests = [];
public function __construct(
private readonly int $maxRequests = 10,
private readonly int $perSeconds = 60
) {}
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
$now = time();
// Clean old requests
$this->requests = array_filter(
$this->requests,
fn ($timestamp) => $now - $timestamp < $this->perSeconds
);
// Check rate limit
if (count($this->requests) >= $this->maxRequests) {
echo " ⚠ Rate limit exceeded ({$this->maxRequests} requests per {$this->perSeconds}s)\n";
echo " Waiting...\n\n";
// Wait until we can make the next request
$oldestRequest = min($this->requests);
$waitTime = $this->perSeconds - ($now - $oldestRequest) + 1;
sleep($waitTime);
// Clean again after waiting
$now = time();
$this->requests = array_filter(
$this->requests,
fn ($timestamp) => $now - $timestamp < $this->perSeconds
);
}
// Record this request
$this->requests[] = $now;
echo ' ✓ Rate limit OK ('.count($this->requests)."/{$this->maxRequests} requests)\n\n";
return $next($request);
}
}
$transport5 = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->withMiddleware(new RateLimitMiddleware(maxRequests: 3, perSeconds: 10))
->build();
try {
echo "Making 4 requests (rate limit: 3/10s):\n\n";
for ($i = 1; $i <= 4; $i++) {
echo "Request {$i}:\n";
$response = $transport5->get('/posts/1')->send();
}
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 6: Chaining Multiple Middlewares
echo "6. Chaining Multiple Middlewares\n";
echo str_repeat('-', 50)."\n";
$transport6 = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->withMiddleware(new ApiKeyAuthMiddleware('test-key'))
->withMiddleware(new CustomHeaderMiddleware('X-Request-ID', uniqid()))
->build();
try {
$response = $transport6->get('/posts/1')->send();
echo "✓ Request completed with multiple middlewares:\n";
echo " - API Key Authentication\n";
echo " - Custom Request ID Header\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "=== Examples Complete ===\n";