Skip to content

Commit 5ee7664

Browse files
Merge pull request #5 from dynamik-dev/feat/middleware
Adds PSR15 middleware support
2 parents f9b9417 + d1aa673 commit 5ee7664

10 files changed

Lines changed: 1279 additions & 351 deletions

File tree

.github/workflows/CI.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ jobs:
3131
uses: actions/cache@v4
3232
with:
3333
path: vendor
34-
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
34+
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.json') }}
3535
restore-keys: |
36-
${{ runner.os }}-php-
36+
${{ runner.os }}-php-${{ matrix.php-versions }}-
3737
3838
- name: Install dependencies
39-
run: composer install --prefer-dist --no-progress
39+
run: composer update --prefer-dist --no-progress
4040

4141
- name: Check code style
4242
run: composer lint:check

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor/
22
.DS_Store
33
.idea
4-
.vscode
4+
.vscode
5+
.claude

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ZenPipe is a simple and flexible PHP pipeline library that allows you to chain o
1717

1818
```php
1919
$calculator = zenpipe()
20-
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
20+
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
2121
->pipe(fn($price, $next) => $next($price * 1.1)); // add 10% tax
2222

2323
$calculator(100); // $88 (100 -> 80 -> 88)
@@ -27,7 +27,7 @@ You can also run the pipeline on demand:
2727

2828
```php
2929
zenpipe(100)
30-
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
30+
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
3131
->pipe(fn($price, $next) => $next($price * 1.1)) // add 10% tax
3232
->process(); // 88
3333
```
@@ -57,17 +57,22 @@ zenpipe(100)
5757
```bash
5858
composer require dynamik-dev/zenpipe-php
5959
```
60+
6061
## Usage
62+
6163
### Pipeline Operations
6264

6365
Pipeline operations are functions that take an input and return a processed value. Each operation can receive up to four parameters:
66+
6467
- `$input`: The value being processed
6568
- `$next`: A callback to pass the value to the next operation
6669
- `$return`: (Optional) A callback to exit the pipeline early with a value
6770
- `$context`: (Optional) A shared context object passed to all operations
6871

6972
#### Basic Operation Example
73+
7074
Let's build an input sanitization pipeline:
75+
7176
```php
7277
// String sanitization pipeline
7378
$sanitizer = zenpipe()
@@ -92,7 +97,9 @@ $result = zenpipe($dirtyInput)
9297
```
9398

9499
#### Operation with Early Return
100+
95101
Below is a practical example of a content moderation pipeline with early returns:
102+
96103
```php
97104
// Content moderation pipeline with early returns
98105
$moderationPipeline = zenpipe()
@@ -125,7 +132,7 @@ $moderationPipeline = zenpipe()
125132
});
126133

127134
// Usage:
128-
$result = $moderationPipeline("Hello, world!");
135+
$result = $moderationPipeline("Hello, world!");
129136
// Trusted user: Immediately returns approved
130137
// Regular user: Goes through full moderation
131138
```
@@ -225,6 +232,7 @@ $result = zenpipe($userData)
225232
```
226233

227234
The catch handler receives:
235+
228236
- `$e`: The thrown exception (`Throwable`)
229237
- `$value`: The original input value passed to `process()`
230238
- `$context`: The context set via `withContext()` (null if not set)
@@ -288,14 +296,14 @@ $emailValidationPipeline = zenpipe()
288296
if (!$email) {
289297
return $return('Invalid email format');
290298
}
291-
299+
292300
$domain = substr(strrchr($email, "@"), 1);
293301
$mxhosts = [];
294-
302+
295303
if (!getmxrr($domain, $mxhosts)) {
296304
return $return('Domain has no valid mail servers');
297305
}
298-
306+
299307
return $next(true);
300308
});
301309

@@ -319,5 +327,3 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
319327
The MIT License (MIT). See [LICENSE](LICENSE) for details.
320328

321329
## Roadmap
322-
323-
- [ ] Add support for PSR-15 middleware

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
"require-dev": {
2020
"pestphp/pest": "^3.7",
2121
"phpstan/phpstan": "^2.1",
22-
"laravel/pint": "^1.2"
22+
"laravel/pint": "^1.2",
23+
"psr/http-server-middleware": "^1.0",
24+
"psr/http-message": "^1.0 || ^2.0",
25+
"nyholm/psr7": "^1.8"
26+
},
27+
"suggest": {
28+
"psr/http-server-middleware": "Required for PSR-15 middleware support (^1.0)",
29+
"psr/http-message": "Required for PSR-15 middleware support (^1.0 || ^2.0)"
2330
},
2431
"config": {
2532
"allow-plugins": {

0 commit comments

Comments
 (0)