Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions src/Events/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Workflow;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent as SymfonyGuardEvent;

/**
* @method \Symfony\Component\Workflow\Marking getMarking()
Expand All @@ -17,7 +18,7 @@ abstract class BaseEvent extends Event
{
public function __serialize(): array
{
return [
$result = [
'base_event_class' => get_class($this),
'subject' => $this->getSubject(),
'marking' => $this->getMarking(),
Expand All @@ -26,29 +27,43 @@ public function __serialize(): array
'name' => $this->getWorkflowName(),
],
];
if (!$this instanceof GuardEvent) {
$result['context'] = $this->getContext();
}
Comment on lines +30 to +32
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than the awkward type-checking here, I think this should be done on the child class instead.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a trait in the base class (HasContextTrait) could you simply check the base class is using that trait and if it is, then attach the context?


return $result;
}

public function __unserialize(array $data): void
{
$workflowName = $data['workflow']['name'] ?? null;
parent::__construct(
$data['subject'],
$data['marking'],
$data['transition'],
Workflow::get($data['subject'], $workflowName)
);

$arguments = [
...$data,
'workflow' => Workflow::get($data['subject'], $workflowName),
];
if (!$this instanceof GuardEvent) {
$arguments['context'] = $data['context'];
}

parent::__construct(...$arguments);
}

/**
* Creates a new instance from the base Symfony event
*/
public static function newFromBase(Event $symfonyEvent)
{
return new static(
$symfonyEvent->getSubject(),
$symfonyEvent->getMarking(),
$symfonyEvent->getTransition(),
$symfonyEvent->getWorkflow()
);
$arguments = [
'subject' => $symfonyEvent->getSubject(),
'marking' => $symfonyEvent->getMarking(),
'transition' => $symfonyEvent->getTransition(),
'workflow' => $symfonyEvent->getWorkflow(),
];
if (!$symfonyEvent instanceof SymfonyGuardEvent) {
$arguments['context'] = $symfonyEvent->getContext();
}

return new static(...$arguments);
}
}
7 changes: 1 addition & 6 deletions src/Events/GuardEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ public function __unserialize(array $data): void
*/
public static function newFromBase(Event $symfonyEvent)
{
$instance = new static(
$symfonyEvent->getSubject(),
$symfonyEvent->getMarking(),
$symfonyEvent->getTransition(),
$symfonyEvent->getWorkflow()
);
$instance = parent::newFromBase($symfonyEvent);

$instance->symfonyProxyEvent = $symfonyEvent;

Expand Down