From cffc6ccb783a28f053626a8b642350b7f6110d2b Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 23 Dec 2021 14:39:40 +0200 Subject: [PATCH 1/2] Add Source to events --- src/Event/MapperEvent.php | 2 ++ src/EventDrivenCommandGenerator.php | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Event/MapperEvent.php b/src/Event/MapperEvent.php index 29d8946..8ef4eb4 100644 --- a/src/Event/MapperEvent.php +++ b/src/Event/MapperEvent.php @@ -7,6 +7,7 @@ use Cycle\ORM\Heap\Node; use Cycle\ORM\Heap\State; use Cycle\ORM\MapperInterface; +use Cycle\ORM\Select\Source; /** * @internal @@ -21,6 +22,7 @@ public function __construct( public object $entity, public Node $node, public State $state, + public Source $source, public \DateTimeImmutable $timestamp ) { } diff --git a/src/EventDrivenCommandGenerator.php b/src/EventDrivenCommandGenerator.php index 0c51ca8..b8ea1d2 100644 --- a/src/EventDrivenCommandGenerator.php +++ b/src/EventDrivenCommandGenerator.php @@ -33,10 +33,11 @@ public function __construct(SchemaInterface $schema, ContainerInterface $contain protected function storeEntity(ORMInterface $orm, Tuple $tuple, bool $isNew): ?CommandInterface { $role = $tuple->node->getRole(); + $src = $orm->getSource($role); $event = $isNew - ? new OnCreate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp) - : new OnUpdate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp); + ? new OnCreate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp) + : new OnUpdate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp); $event->command = parent::storeEntity($orm, $tuple, $isNew); @@ -55,8 +56,11 @@ protected function generateParentStoreCommand( bool $isNew ): ?CommandInterface { $mapper = $orm->getMapper($parentRole); + $source = $orm->getSource($parentRole); + + $event = + new OnCreate($parentRole, $mapper, $tuple->entity, $tuple->node, $tuple->state, $source, $this->timestamp); - $event = new OnCreate($parentRole, $mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp); $event->command = $isNew ? $mapper->queueCreate($tuple->entity, $tuple->node, $tuple->state) : $mapper->queueUpdate($tuple->entity, $tuple->node, $tuple->state); @@ -69,7 +73,10 @@ protected function generateParentStoreCommand( protected function deleteEntity(ORMInterface $orm, Tuple $tuple): ?CommandInterface { $role = $tuple->node->getRole(); - $event = new OnDelete($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp); + $source = $orm->getSource($role); + + $event = + new OnDelete($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $source, $this->timestamp); $event->command = parent::deleteEntity($orm, $tuple); From 4e901dd568b1f376edb7808cad6355309cf1fb8d Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Mon, 27 Dec 2021 10:23:30 +0300 Subject: [PATCH 2/2] Fix CS; add changelog; fix $source type --- CHANGELOG.md | 18 ++++++++++++++++++ src/Event/MapperEvent.php | 4 ++-- src/EventDrivenCommandGenerator.php | 20 +++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7648735 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,18 @@ +# CHANGELOG + + +v1.1.0 under development +------------------------ +- Add `SourceInterface $source` property to the `MapperEvent` + +v1.0.0 (22.12.2021) +------------------- +- Add behaviors: + - `CreatedAt` + - `OptimisticLock` + - `SoftDelete` + - `UpdatedAt` + - `EventListener` + - `Hook` +- Add `EventDrivenCommandGenerator` with custom event dispatcher. +- Supported events: `OnCreate`, `OnDelete` and `OnUpdate` diff --git a/src/Event/MapperEvent.php b/src/Event/MapperEvent.php index 8ef4eb4..7200860 100644 --- a/src/Event/MapperEvent.php +++ b/src/Event/MapperEvent.php @@ -7,7 +7,7 @@ use Cycle\ORM\Heap\Node; use Cycle\ORM\Heap\State; use Cycle\ORM\MapperInterface; -use Cycle\ORM\Select\Source; +use Cycle\ORM\Select\SourceInterface; /** * @internal @@ -22,7 +22,7 @@ public function __construct( public object $entity, public Node $node, public State $state, - public Source $source, + public SourceInterface $source, public \DateTimeImmutable $timestamp ) { } diff --git a/src/EventDrivenCommandGenerator.php b/src/EventDrivenCommandGenerator.php index b8ea1d2..ef94a7d 100644 --- a/src/EventDrivenCommandGenerator.php +++ b/src/EventDrivenCommandGenerator.php @@ -5,15 +5,15 @@ namespace Cycle\ORM\Entity\Behavior; use Cycle\ORM\Command\CommandInterface; -use Cycle\ORM\ORMInterface; -use Cycle\ORM\SchemaInterface; -use Cycle\ORM\Transaction\CommandGenerator; -use Cycle\ORM\Transaction\Tuple; use Cycle\ORM\Entity\Behavior\Dispatcher\Dispatcher; use Cycle\ORM\Entity\Behavior\Dispatcher\ListenerProvider; use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate; use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnDelete; use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnUpdate; +use Cycle\ORM\ORMInterface; +use Cycle\ORM\SchemaInterface; +use Cycle\ORM\Transaction\CommandGenerator; +use Cycle\ORM\Transaction\Tuple; use Psr\Container\ContainerInterface; use Psr\EventDispatcher\EventDispatcherInterface; @@ -22,7 +22,6 @@ final class EventDrivenCommandGenerator extends CommandGenerator private EventDispatcherInterface $eventDispatcher; private \DateTimeImmutable $timestamp; - // todo: add custom listener interface public function __construct(SchemaInterface $schema, ContainerInterface $container) { $listenerProvider = new ListenerProvider($schema, $container); @@ -75,8 +74,15 @@ protected function deleteEntity(ORMInterface $orm, Tuple $tuple): ?CommandInterf $role = $tuple->node->getRole(); $source = $orm->getSource($role); - $event = - new OnDelete($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $source, $this->timestamp); + $event = new OnDelete( + $role, + $tuple->mapper, + $tuple->entity, + $tuple->node, + $tuple->state, + $source, + $this->timestamp + ); $event->command = parent::deleteEntity($orm, $tuple);