Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ jobs:
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-
${{ runner.os }}-php-${{ matrix.php-versions }}-

- name: Install dependencies
run: composer install --prefer-dist --no-progress
run: composer update --prefer-dist --no-progress

- name: Check code style
run: composer lint:check
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor/
.DS_Store
.idea
.vscode
.vscode
.claude
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ZenPipe is a simple and flexible PHP pipeline library that allows you to chain o

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

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

```php
zenpipe(100)
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
->pipe(fn($price, $next) => $next($price * 1.1)) // add 10% tax
->process(); // 88
```
Expand Down Expand Up @@ -57,17 +57,22 @@ zenpipe(100)
```bash
composer require dynamik-dev/zenpipe-php
```

## Usage

### Pipeline Operations

Pipeline operations are functions that take an input and return a processed value. Each operation can receive up to four parameters:

- `$input`: The value being processed
- `$next`: A callback to pass the value to the next operation
- `$return`: (Optional) A callback to exit the pipeline early with a value
- `$context`: (Optional) A shared context object passed to all operations

#### Basic Operation Example

Let's build an input sanitization pipeline:

```php
// String sanitization pipeline
$sanitizer = zenpipe()
Expand All @@ -92,7 +97,9 @@ $result = zenpipe($dirtyInput)
```

#### Operation with Early Return

Below is a practical example of a content moderation pipeline with early returns:

```php
// Content moderation pipeline with early returns
$moderationPipeline = zenpipe()
Expand Down Expand Up @@ -125,7 +132,7 @@ $moderationPipeline = zenpipe()
});

// Usage:
$result = $moderationPipeline("Hello, world!");
$result = $moderationPipeline("Hello, world!");
// Trusted user: Immediately returns approved
// Regular user: Goes through full moderation
```
Expand Down Expand Up @@ -225,6 +232,7 @@ $result = zenpipe($userData)
```

The catch handler receives:

- `$e`: The thrown exception (`Throwable`)
- `$value`: The original input value passed to `process()`
- `$context`: The context set via `withContext()` (null if not set)
Expand Down Expand Up @@ -288,14 +296,14 @@ $emailValidationPipeline = zenpipe()
if (!$email) {
return $return('Invalid email format');
}

$domain = substr(strrchr($email, "@"), 1);
$mxhosts = [];

if (!getmxrr($domain, $mxhosts)) {
return $return('Domain has no valid mail servers');
}

return $next(true);
});

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

## Roadmap

- [ ] Add support for PSR-15 middleware
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
"require-dev": {
"pestphp/pest": "^3.7",
"phpstan/phpstan": "^2.1",
"laravel/pint": "^1.2"
"laravel/pint": "^1.2",
"psr/http-server-middleware": "^1.0",
"psr/http-message": "^1.0 || ^2.0",
"nyholm/psr7": "^1.8"
},
"suggest": {
"psr/http-server-middleware": "Required for PSR-15 middleware support (^1.0)",
"psr/http-message": "Required for PSR-15 middleware support (^1.0 || ^2.0)"
},
"config": {
"allow-plugins": {
Expand Down
Loading