https://github.com/b2pweb/bdf-pipeline
https://github.com/b2pweb/bdf-pipeline
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/b2pweb/bdf-pipeline
- Owner: b2pweb
- License: mit
- Created: 2018-02-28T12:15:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-01T15:02:38.000Z (about 8 years ago)
- Last Synced: 2025-03-13T06:48:33.904Z (over 1 year ago)
- Language: PHP
- Size: 23.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# B2PWeb Pipeline
A PHP Pipeline pattern.
[](https://travis-ci.org/b2pweb/bdf-pipeline)
[](https://scrutinizer-ci.com/g/b2pweb/bdf-pipeline/?branch=master)
[](https://packagist.org/packages/b2pweb/bdf-pipeline)
[](https://packagist.org/packages/b2pweb/bdf-pipeline)
## Install via composer
```bash
$ composer require b2pweb/bdf-pipeline
```
## Usage Instructions
A basic and classic use of pipeline with a pipe processor.
```PHP
use Bdf\Pipeline\Pipeline;
use Bdf\Pipeline\CallableFactory\LinkedCallableFactory;
$pipeline = new Pipeline(new LinkedCallableFactory());
$pipeline->pipe(function($value) {
return $value + 2;
});
// Returns 12
$pipeline->send(10);
```
The pipeline lib comes with an advanced processor (used by default).
```PHP
use Bdf\Pipeline\Pipeline;
use Bdf\Pipeline\CallableFactory\StackCallableFactory;
$pipeline = new Pipeline(new StackCallableFactory());
$pipeline->pipe(function($next, $foo, $bar) {
// Do something
...
$result = $next($foo, $bar);
// Do something else
...
return $result;
});
$pipeline->outlet(function($foo, $bar) {
return "${foo}.${bar}";
});
// Manage multiple parameters
echo $pipeline->send('foo', 'bar'); // Print foo.bar
```
## Ok, So ?
You can use this package as a classic pipe, but it was designed to be easily extended:
```PHP
$pipeline->pipe(new LogCommand());
$pipeline->outlet(new CreateUserHandler());
...
$pipeline->prepend(new TransactionnalCommand());
$pipeline->send(new CreateUserCommand());
```
```PHP
class TransactionnalCommand
{
public function __invoke($next, $command)
{
try {
$result = $next($command);
// Commit and return the result
...
return $result;
} catch (\Throwable $exception) {
// Rollback and propagate exception
throw $exception;
}
}
}
```
The pipeline is reusable:
```PHP
$pipeline = new Pipeline();
$pipeline->pipe(new Double());
$new = clone $pipeline;
$new->pipe(new Double());
echo $pipeline->send(2); // 4
echo $new->send(2); // 8
```
## License
Distributed under the terms of the MIT license.