PHP code example of babicaja / chainer
1. Go to this page and download the library: Download babicaja/chainer 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/ */
babicaja / chainer example snippets
Chain::do(TaskOne::class)
->then(TaskTwo::class)
->then(TaskThree::class)
->run('payload');
namespace Examples;
use Chainer\Chain;
use Chainer\Link;
class FirstAction extends Link
{
public function handle($payload = null)
{
$payload[] = __METHOD__;
return $payload;
}
}
class SecondAction extends Link
{
public function handle($payload = null)
{
$payload[] = __METHOD__;
return $payload;
}
}
$result = Chain::do(FirstAction::class)
->then(SecondAction::class)
->run();
echo json_encode($result);
[
"Examples\\FirstAction::handle",
"Examples\\SecondAction::handle"
]
namespace Examples;
use Chainer\Chain;
use Chainer\Link;
class FirstAction extends Link
{
public function handle($payload = null)
{
$payload[] = __METHOD__;
return $payload;
}
}
class SecondAction extends Link
{
public function handle($payload = null)
{
$payload[] = __METHOD__;
return $payload;
}
}
$chain = Chain::do(FirstAction::class)
->then(SecondAction::class);
$result = Chain::do($chain)
->then(FirstAction::class)
->run([]);
echo json_encode($result);
[
"Examples\\FirstAction::handle",
"Examples\\SecondAction::handle",
"Examples\\FirstAction::handle"
]
namespace Examples;
use Chainer\Chain;
class FirstAction
{
public function __invoke($payload = null)
{
$payload[] = __METHOD__;
return $payload;
}
}
class SecondAction
{
public function __invoke($payload = null)
{
$payload[] = __METHOD__;
return $payload;
}
}
$result = Chain::do(FirstAction::class)
->then(SecondAction::class)
->run();
echo json_encode($result);
[
"Examples\\FirstAction::__invoke",
"Examples\\SecondAction::__invoke"
]
namespace Examples;
use Chainer\Chain;
function helper($payload)
{
$payload[] = __METHOD__;
return $payload;
}
class Util
{
public function method($payload)
{
$payload[] = __METHOD__;
return $payload;
}
public static function staticMethod($payload)
{
$payload[] = __METHOD__;
return $payload;
}
}
class App
{
public function run()
{
return Chain::do(fn($payload) => $this->method($payload))
->then(fn($payload) => self::staticMethod($payload))
->then([new Util(), 'method'])
->then([Util::class, 'staticMethod'])
->then('Examples\helper')
->then(function ($payload) {
$payload[] = __METHOD__;
return $payload;
})
->run([]);
}
private function method($payload)
{
$payload[] = __METHOD__;
return $payload;
}
private static function staticMethod($payload)
{
$payload[] = __METHOD__;
return $payload;
}
}
$app = new App();
echo json_encode($app->run());
[
"Examples\\App::method",
"Examples\\App::staticMethod",
"Examples\\Util::method",
"Examples\\Util::staticMethod",
"Examples\\helper",
"Examples\\{closure}"
]