Skip to content

Repository files navigation

amethyst-action

Action Status

Amethyst package.

Requirements

PHP 7.1 and later.

Installation

You can install it via Composer by typing the following command:

composer require amethyst/action

The package will automatically register itself.

Documentation

Read

Testing

Configure the .env file before launching ./vendor/bin/phpunit

Dummy

Let's create a simple workflow, the goal is to log the message of an event when is fired.

app/Events/DummyEvent.php

<?phpnamespaceApp\Events;
class DummyEvent
{
public$message;
publicfunction__construct(string$message)
{
$this->message = $message;
}
publicfunctiongetData()
{
return [
'message' => $this->message
];
}
}

app/Actions/LogAction.php

<?phpnamespaceApp\Actions;
useIlluminate\Support\Facades\Log;
useAmethyst\Actions\Action;
useRailken\Bag;
class LogAction extends Action
{
publicfunctionrequires()
{
return [
'message' => 'text'
];
}
publicfunctiondispatch(Closure$next, Bag$data) {
Log::info($data->message);
$next($data);
}
}

app/Actions/EventListenerAction.php

<?phpnamespaceApp\Actions;
useIlluminate\Support\Facades\Log;
useAmethyst\Actions\Action;
useIlluminate\Support\Facades\Event;
useRailken\Bag;
class EventListenerAction extends Action
{
protected$event;
publicfunctiondispatch(Closure$next, Bag$data) {
Event::listen([$this->data->event], function ($event_name, $events) use ($next, $data) {
$next($data->merge(newBag($events[0]->getData())));
});
}
}

Now remains only data entry

useAmethyst\Managers\ActionManager;
useAmethyst\Managers\WorkflowManager;
useAmethyst\Managers\WorkflowNode;
useAmethyst\Managers\AggregatorManager;
useApp\Actions\LogAction;
useApp\Actions\EventListenerAction;
useApp\Events\DummyEvent;
useSymfony\Component\Yaml\Yaml;
app('amethyst.workflow')->addType('log', LogAction::class);
app('amethyst.workflow')->addType('event-listener', EventListenerAction::class);
$actionManager = newActionManager();
$logAction = $actionManager->createOrFail([
'name' => 'Log',
'payload' => Yaml::dump([
'class' => 'log',
'arguments' => []
])
])->getResource();
$eventListenerAction = $actionManager->createOrFail([
'name' => 'Event Listener',
'payload' => Yaml::dump([
'class' => 'event-listener',
'arguments' => []
])
])->getResource();
$workflowManager = newWorkflowManager();
$aggregatorManager = newAggregatorManager();
$workflow = $workflowManager->createOrFail([
'name' => 'Log events'
])->getResource();
$node1 = $workflowNodeManager->createOrFail([
'workflow_id' => $workflow->id,
'target_type' => 'action',
'target_id' => $eventListeneAction->id,
'data' => Yaml::dump([
'event' => DummyEvent::class
]),
])->getResource();
$node2 = $workflowNodeManager->createOrFail([
'workflow_id' => $workflow->id,
'target_type' => 'action',
'target_id' => $logAction->id
])->getResource();
$aggregatorManager->createOrFail([
'source_type' => 'workflow-node',
'source_id' => $node1->id,
'aggregate_type' => 'workflow-node',
'aggregate_type' => $node2->id
]);

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages