PHP code example of bhittani / dispenser
1. Go to this page and download the library: Download bhittani/dispenser library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bhittani / dispenser example snippets
Bhittani\Dispenser\Dispenser;
$dispenser = new Dispenser(function ($a, $b) {
return $a . '.' . $b;
});
$dispenser->dispense('foo', 'bar'); // 'foo.bar'
Bhittani\Dispenser\Queue;
use Bhittani\Dispenser\Dispenser;
$queue = new Queue;
$queue->push(new Dispenser(function ($a, $b) { return $a . $b . 1; }));
$queue->push(new Dispenser(function ($a, $b) { return $a . $b . 2; }));
// Doesn't have to be a dispenser, but recommended.
$queue->push(function ($a, $b) { return $a . $b . 3; });
$queue->dispense('a', 'b'); // ['ab1', 'ab2', 'ab3']
Bhittani\Dispenser\Stack;
use Bhittani\Dispenser\Dispenser;
$stack = new Stack;
$stack->push(new Dispenser(function ($a, $b) { return $a . $b . 1; }));
$stack->push(new Dispenser(function ($a, $b) { return $a . $b . 2; }));
// Doesn't have to be a dispenser, but recommended.
$stack->push(function ($a, $b) { return $a . $b . 3; });
$stack->dispense('a', 'b'); // ['ab3', 'ab2', 'ab1']
Bhittani\Dispenser\Priority;
use Bhittani\Dispenser\Dispenser;
$priority = new Priority;
$priority->insert(new Dispenser(function ($a, $b) { return $a . $b . 3; }), 1);
$priority->insert(new Dispenser(function ($a, $b) { return $a . $b . 1; }), 3);
// Doesn't have to be a dispenser, but recommended.
$priority->insert(function ($a, $b) { return $a . $b . 2; }, 2);
$priority->dispense('a', 'b'); // ['ab1', 'ab2', 'ab3']
Bhittani\Dispenser\Stack;
use Bhittani\Dispenser\Pipeline;
use Bhittani\Dispenser\Dispenser;
$pipeline = new Pipeline;
$pipeline->push($stack = new Stack);
$stack->push(new Dispenser(function ($n) { return $n / 5; })); // 100/5=20
$stack->push(new Dispenser(function ($n) { return $n + 60; })); // 40+60=100
$stack->push(function ($n) { return $n * 4; }); // 10*4=40
$pipeline->push(function ($n) { return $n / 2; }); // 20/2=10
$pipeline->dispense(10); // 10
Bhittani\Dispenser\Chain;
use Bhittani\Dispenser\Dispenser;
// Accepts an optional fallback dispenser.
$chain = new Chain(function ($foo, $bar) {
return '!';
});
$chain->push(new Dispenser(function ($foo, $bar, $next) {
return '(' . $next($foo, $bar) . ')';
}));
// Doesn't have to be a dispenser, but recommended.
$chain->push(function ($foo, $bar, $next) {
return $foo.$next($foo, $bar);
});
$chain->push(function ($foo, $bar, $next) {
return $next($foo, $bar).$bar;
});
$chain->dispense('middle', 'ware'); // (middle!ware)
Bhittani\Dispenser\Chain;
use Bhittani\Dispenser\Queue;
use Bhittani\Dispenser\Dispenser;
$chain = new Chain(function ($request) {
return make_a_response_however_you_want_to($request);
});
$chain->push($middlewares = new Queue);
// With a fictional request & response objects,
// lets record the time spent on the request.
$middlewares->push(new Dispenser(function ($request, $next) {
$request = $request->startTime();
$response = $next($request);
$request->stopTime();
return $response->withTime($request->getElapsedTime());
}));
$middlewares->push(new Dispenser(function ($request, $next) {
// Do something...
return $next($request);
}));
// Handle the $request...
$chain->dispense($request);
Bhittani\Dispenser\DispenserInterface;
class Dispatcher implements DispenserInterface
{
protected $subscribers = [];
public function subscribe($key, DispenserInterface $subscriber)
{
if (! isset($this->subscribers[$key])) {
$this->subscribers[$key] = [];
}
$this->subscribers[$key][] = $subscriber;
return $this;
}
public function dispense(...$parameters)
{
$key = array_shift($parameters);
if (! isset($this->subscribers[$key])) {
return [];
}
return array_map(function ($subscriber) use ($parameters) {
return $subscriber->dispense($parameters);
}, $this->subscribers[$key]);
}
}
$dispatcher = new Dispatcher;
$dispatcher->subscribe('foo', new Dispenser(function ($a, $b) {
return $a.'1foo1'.$b;
}));
$dispatcher->subscribe('foo', new Dispenser(function ($a, $b) {
return $a.'2foo2'.$b;
}));
$dispatcher->subscribe('bar', new Dispenser(function ($a, $b) {
return $a.'bar'.$b;
}));
$dispatcher->dispense('bar', 'a', 'b'); // ['abarb']
$dispatcher->dispense('foo', 'a', 'b'); // ['a1foo1b', 'a2foo2b']