/** * Get Action */ protected function getAction() { $action = new Callback([$this, 'run']); $action->bind(Action::EVENT_START, function (Context $context) { Yii::$app->controller->stdout("Started " . json_encode($context->toArray()) . "\n", Console::FG_YELLOW); }); $action->bind(Action::EVENT_ERROR, function (Context $context) { Yii::$app->controller->stdout("Error " . json_encode($context->toArray()) . "\n", Console::FG_RED); }); $action->bind(Action::EVENT_SUCCESS, function (Context $context) { Yii::$app->controller->stdout("Success " . json_encode($context->toArray()) . "\n", Console::FG_BLUE); }); $action->bind(Action::EVENT_FAILURE, function (Context $context) { Yii::$app->controller->stdout("Failed " . json_encode($context->toArray()) . "\n", Console::FG_RED); }); $action->bind(Action::EVENT_FINISH, function (Context $context) { Yii::$app->controller->stdout("finish " . json_encode($context->toArray()) . "\n\n\n", Console::FG_GREEN); }); $action->bind(Action::EVENT_TIMEOUT, function (Context $context) { Yii::$app->controller->stdout("timeout " . json_encode($context->toArray()) . "\n\n\n", Console::FG_GREEN); }); return $action; }
<?php declare (ticks=1); require_once __DIR__ . '/../vendor/autoload.php'; use Arara\Process\Action\Action; use Arara\Process\Action\Callback; use Arara\Process\Child; use Arara\Process\Context; use Arara\Process\Control; $action = new Callback(function () { trim(['A PHP error occours']); }); $action->bind(Action::EVENT_ERROR, function (Context $context) { echo json_encode($context->toArray(), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL; }); $control = new Control(); $child = new Child($action, $control); $child->start(); $child->wait();
public function testShouldNotRunEventHandlerWhenEventIsNotTriggered() { $control = new Control(); $context = new Context(); $counter = 0; $handler = function () use(&$counter) { $counter++; }; $callback = new Callback(function () { }); $callback->bind(Callback::EVENT_ERROR, $handler); $callback->trigger(Callback::EVENT_FAILURE, $control, $context); $this->assertEquals(0, $counter); }
/** * {@inheritdoc} * * @throws InvalidArgumentException When event binding is forbidden. */ public function bind($event, callable $handler) { if (in_array($event, [self::EVENT_INIT, self::EVENT_FORK, self::EVENT_START])) { throw new InvalidArgumentException('You can not bind a callback for this event'); } parent::bind($event, $handler); }