https://github.com/phuria/pipolino
A immutable dispatcher to process values in sequence.
https://github.com/phuria/pipolino
dispatcher immutable pipeline processing
Last synced: 6 months ago
JSON representation
A immutable dispatcher to process values in sequence.
- Host: GitHub
- URL: https://github.com/phuria/pipolino
- Owner: phuria
- License: mit
- Created: 2017-03-03T15:25:27.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-08T19:49:54.000Z (almost 9 years ago)
- Last Synced: 2025-07-20T10:26:23.516Z (11 months ago)
- Topics: dispatcher, immutable, pipeline, processing
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Pipolino
[](https://scrutinizer-ci.com/g/phuria/pipolino/build-status/master)
[](https://scrutinizer-ci.com/g/phuria/pipolino/?branch=master)
[](https://scrutinizer-ci.com/g/phuria/pipolino/?branch=master)
[](https://packagist.org/packages/phuria/pipolino)
[]()
[]()
The package provides dispatcher that can be used to process
one or more values in sequence. It is a combination of
[thephpleague/pipeline](https://github.com/thephpleague/pipeline)
and [PSR-7 middleware dispatcher (eg. Relay)](https://github.com/relayphp/Relay.Relay).
## Installation
```
composer require phuria/pipolino
```
## Usage
```php
$pipolino = (new Pipolino())
->addStage(function(callable $next, $payload) {
return $next($payload * 2); // 20 * 2
})
->addStage(function (callable $next, $payload) {
return $next($payload + 1); // 40 + 1
})
->addStage(function (callable $next, $payload) {
return $next($payload / 10); // 41 / 10
});
echo $pipolino->process(20); //output: 4.1
```
## Immutable
Pipolino are implemented as __immutable__. Any change on object (eg. add new stage)
creates new object.
## Stage objects
Object can be used as a stage. It must have only implemented `__invoke` method.
```php
class CacheStage
{
private $cache;
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
public function __invoke(callable $next, $result, array $criteria)
{
$hash = sha1(serialize($criteria));
if ($this->cache->has($hash)) {
return $this->cache->get($hash);
}
$result = $next($result, $criteria);
$this->cache->set($hash, $result, 3600);
return $result;
}
}
```
## Many arguments
Pipolino (in contrast to pipeline) accepts any number of arguments to process.
```php
$pipolino = (new Pipolino)
->addStage(function (callable $next, $result, array $options) {
if (null === $result) {
return $options['onNull'];
}
return $next($result, $context);
})
->addStage(function (callable $next, $result, array $options) {
$formatted = $next(number_format($result, 2).' '.$options['currency'])
// Can be replaced with: "return $forrmated;",
// however, should call $next().
return $next($formatted, $options);
});
$options = ['onNull' => '-', 'currency' => 'USD'];
echo $pipolino->process(null, $options); // output: -
echo $pipolino->process(10, $options); // output: 10.00 USD
```
## Pipolino composite
Each pipolino can be used as stage. This allows for easy re-use pipolino.
```php
$loadingProcess = (new Pipolino())
->add(new CheckCache())
->add(new LoadFromDB());
$showProcces = (new Pipolino())
->add(new JsonFormat())
->add($loadingProccess)
->add(new NullResult());
```
## Default stage
Default stage is last stage in pipolino,
and does not accept `$next` callable as first argument.
If you need to alter behavior of default stage (return first argument)
call `Pipolino::withDefaultStage()` method.
```php
$pipolino = (new Pipolino)
->addStage(function (callable $net, $i) {
if (is_string($i)) {
$i = (int) $i;
}
return $next($i);
})
->addStage(function (callable $next, $i) {
if (is_int($i)) {
return $i % 2 ? 'even' : 'odd';
}
return $next($i);
})
->withDefaultStage(function () {
throw new \Exception('Unable to guess type.');
});
echo $pipolino->process(2); // output: even
echo $pipolino->process('4'); // output: even
echo $pipolino->proccess(2.50); // throws exception
```