amethyst / action
v0.3.3
2024-04-04 18:12 UTC
Requires
- php: >=8.2
- amethyst/core: 0.3.*
- amethyst/file: 0.3.*
- amethyst/notification: 0.3.*
- amethyst/relation: 0.3.*
- box/spout: ^2.7
- guzzlehttp/guzzle: ^7.8
- nicoswd/php-rule-parser: ^0.7.1
- railken/template: ^1.1
Requires (Dev)
- amethyst/foo: 0.3.*
- friendsofphp/php-cs-fixer: ^3.52
- orchestra/testbench: *
README
Amethyst 包。
要求
PHP 7.1 及以上。
安装
您可以通过输入以下命令使用 Composer 安装它:
composer require amethyst/action
该包将自动注册自己。
文档
测试
在启动 ./vendor/bin/phpunit
之前配置 .env 文件
虚拟
让我们创建一个简单的流程,目标是记录事件被触发时的消息。
app/Events/DummyEvent.php
<?php namespace App\Events; class DummyEvent { public $message; public function __construct(string $message) { $this->message = $message; } public function getData() { return [ 'message' => $this->message ]; } }
app/Actions/LogAction.php
<?php namespace App\Actions; use Illuminate\Support\Facades\Log; use Amethyst\Actions\Action; use Railken\Bag; class LogAction extends Action { public function requires() { return [ 'message' => 'text' ]; } public function dispatch(Closure $next, Bag $data) { Log::info($data->message); $next($data); } }
app/Actions/EventListenerAction.php
<?php namespace App\Actions; use Illuminate\Support\Facades\Log; use Amethyst\Actions\Action; use Illuminate\Support\Facades\Event; use Railken\Bag; class EventListenerAction extends Action { protected $event; public function dispatch(Closure $next, Bag $data) { Event::listen([$this->data->event], function ($event_name, $events) use ($next, $data) { $next($data->merge(new Bag($events[0]->getData()))); }); } }
现在只剩下数据录入
use Amethyst\Managers\ActionManager; use Amethyst\Managers\WorkflowManager; use Amethyst\Managers\WorkflowNode; use Amethyst\Managers\AggregatorManager; use App\Actions\LogAction; use App\Actions\EventListenerAction; use App\Events\DummyEvent; use Symfony\Component\Yaml\Yaml; app('amethyst.workflow')->addType('log', LogAction::class); app('amethyst.workflow')->addType('event-listener', EventListenerAction::class); $actionManager = new ActionManager(); $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 = new WorkflowManager(); $aggregatorManager = new AggregatorManager(); $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 ]);