laravel-workflow: Announce events not fired
Hi,
I have the following test model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use ZeroDaHero\LaravelWorkflow\Traits\WorkflowTrait;
class Test extends Model
{
use HasFactory;
use WorkflowTrait;
protected $table = 'test';
protected $casts = [
'marking' => 'array',
];
}
Workflow:
'test' => [
'type' => 'workflow',
// 'marking_store' => [
// 'type' => 'multiple_state',
// ],
'supports' => [
Test::class,
],
'events_to_dispatch' => [
'workflow.announce',
],
'places' => [
'a', 'b', 'c',
],
'initial_places' => [
'a',
],
'transitions' => [
't1' => [
'from' => 'a',
'to' => 'b'
],
't2' => [
'from' => 'b',
'to' => 'c'
],
],
];
Event subscriber:
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Workflow\Event\AnnounceEvent;
use ZeroDaHero\LaravelWorkflow\Events\GuardEvent;
class TestSubscriber implements ShouldQueue
{
public function handleAnnounce($type, $event)
{
Log::debug('* Received Event: ' . $type);
Log::debug(gettype($event));
}
public function subscribe($events)
{
$events->listen(
'workflow.test.*',
[TestSubscriber::class, 'handleAnnounce']
);
}
}
And the following testing code:
Log::debug('---');
$test = new Test();
Log::debug('Checking transition t1: ' . $test->workflow_can('t1'));
Log::debug('Current marking: ' . $test->marking);
foreach ($test->workflow_transitions() as $transition) {
Log::debug('Enabled Transition: ' . $transition->getName());
}
Log::debug('Applying transition t1:');
$marking = $test->workflow_apply('t1');
return 0;
What I expect is to receive the event somehow so I can process it.
All of the above was discovered when I am trying to receive Announce events, in order to make automatic transitions when possible. So far I have discovered that they are not fired at all. This is issue 1.
According to current documentation, the handler in subscriber should receive Event as first argument. Actually it receives Event only in the case when subscriber is subscribed to ZeroDaHero Events. If subscribed to Symfony dot-style events (as in the example above), function receives string containing the event name and second parameter of type array, that contains dump of the ZeroDaHero Event. This is issue 2.
I believe this is related to latest changes in Symfony/Workflow 5.2.
here is the debug information from above code:
[2021-01-21 16:22:57] local.DEBUG: ---
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.entered
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard.t1
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: Checking transition t1: 1
[2021-01-21 16:22:57] local.DEBUG: Current marking: a
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard.t1
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: Enabled Transition: t1
[2021-01-21 16:22:57] local.DEBUG: Applying transition t1:
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard.t1
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.leave
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.leave.a
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.transition
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.transition.t1
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.enter
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.enter.b
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.entered
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.entered.b
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.completed
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.completed.t1
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard
[2021-01-21 16:22:57] local.DEBUG: array
[2021-01-21 16:22:57] local.DEBUG: * Received Event: workflow.test.guard.t2
[2021-01-21 16:22:57] local.DEBUG: array
"symfony/workflow": "^5.2",
"zerodahero/laravel-workflow": "^3.2"
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 17 (6 by maintainers)
PS. Could you please look into how to add support for ‘events_to_dispatch’ => [
Already created pull request, maybe you can merge it …
In regard to events, I’ve been digging around and already found out the same thing. I will be exploring further though.
Hi @klimenttoshkov, thanks for the detailed info! That helped me pinpoint the issue very easily.
Looks like I missed the ‘announce’ event when it was added into the Symfony workflow component. I’ll try and find some time this week to add that in.
As for issue 2, there’s not much we can do about that since that’s Laravel’s way of handling events. It’s the same if you listen for specific model or framework events: the “event” is the first argument (which is either the event name, or the event class/object) and the payload is second (which is the data present for the non-class events).
I’ll let you know when I have a new version ready with the announce events dispatching.