1. Go to this page and download the library: Download andydune/pipeline 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/ */
$instance = new class() {
public function __invoke($context, $next)
{
$context['invoke'] = 'was here';
return $next($context);
}
}
$pipeline = new Pipeline();
$pipeline->pipe($instance);
$instance = new class() {
public function doIt($context, $next)
{
$context['invoke'] = 'was here';
return $next($context);
}
}
$pipeline = new Pipeline();
$pipeline->pipe($instance, 'doIt');
class Trim
{
public function __invoke($context, $next)
{
$context['class_invoke'] = 'was here';
return $next($context);
}
}
$pipeline = new Pipeline();
$pipeline->pipe(Trim::class);
class Trim
{
public function handle($context, $next)
{
$context['class_invoke'] = 'was here';
return $next($context);
}
}
$pipeline = new Pipeline();
$pipeline->pipe(Trim::class, 'handle');
namespace AndyDune\Pipeline\Example;
class Methods
{
// return result - no calling next()
public function addBraceLeft($string)
{
return '(' . $string;
}
// It has middleware interface
public function addBraceRight($string, callable $next)
{
$string = $string . ')';
return $next($string);
}
}
$instance = new Methods();
$pipeline = new Pipeline();
$pipeline->send('puh');
$pipeline->pipeForContainer($instance, 'addBraceLeft');
$pipeline->pipe(Methods::class, 'addBraceRight');
$result = $pipeline->execute();
$result == '(puh)';
use Interop\Container\ContainerInterface;
class SpecialPipelineContainer implements ContainerInterface
{
/**
* @var \Rzn\ObjectBuilder
*/
protected $objectBuilder;
public function get($name)
{
/*
do anything to build object using $name
*/
return $object;
}
public function has($name)
{
/*
do anything to check
namespace AndyDune\Pipeline\Example;
class PowerOfNumber
{
public function __invoke($data, callable $next, $power = 2)
{
if (is_array($power)) {
array_walk($power, function (&$value, $key) use ($data) {
$value = pow($data, $value);
});
return $next($power);
}
$data = $this->handle($data, $power);
return $next($data);
}
protected function handle($number, $power)
{
return pow($number, $power);
}
}
use use AndyDune\Pipeline\Pipeline;
use AndyDune\Pipeline\Example;
$pipeline = new Pipeline();
$pipeline->send(2);
$pipeline->pipe(PowerOfNumber::class);
$result = $pipeline->execute(); // == 4
$pipeline = new Pipeline();
$pipeline->send(2);
$pipeline->pipe(PowerOfNumber::class, null, 3);
$result = $pipeline->execute(); // == 8
$pipeline = new Pipeline();
$pipeline->send(2);
$pipeline->pipe(PowerOfNumber::class, null, 4);
$result = $pipeline->execute(); // == 16
use use AndyDune\Pipeline\Pipeline;
$pipeline = new Pipeline();
$pipeline->addInitializer(function($stageObject) use ($someService) {
if ($stageObject instanceof SomeServiceAwareInterface) {
$stageObject->setSomeService($someService)
}
});
$pipeline->pipe(ClassHaveInterface::class);
$result = $pipeline->execute();
use AndyDune\Pipeline\Pipeline;
$pipeline = new Pipeline();
$pipeline->send(['zub' => 'kovoy']);
$pipeline->pipe(function ($context, $next) {
try {
return $next($context);
} catch (Exception $e) {
$context['exception'] = 'caught';
}
return $context;
});
$pipeline->pipe(function ($context, $next) {
$context['action'] = 'before_exception';
throw new Exception();
return $next($context); // it will be never execute
});
// This stage will never be executed
$pipeline->pipe(function ($context, $next) {
$context['after_exception'] = 'ignored';
return $next($context);
});
$result = $pipeline->execute();
array_key_exists('zub', $result); // true
array_key_exists('exception', $result); // true
array_key_exists('action', $result); // false
array_key_exists('after_exception', $result); // false