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 29d8946..7200860 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\SourceInterface; /** * @internal @@ -21,6 +22,7 @@ public function __construct( public object $entity, public Node $node, public State $state, + public SourceInterface $source, public \DateTimeImmutable $timestamp ) { } diff --git a/src/EventDrivenCommandGenerator.php b/src/EventDrivenCommandGenerator.php index 0c51ca8..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); @@ -33,10 +32,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 +55,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 +72,17 @@ 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);