Skip to content
Merged
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
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
2 changes: 2 additions & 0 deletions src/Event/MapperEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cycle\ORM\Heap\Node;
use Cycle\ORM\Heap\State;
use Cycle\ORM\MapperInterface;
use Cycle\ORM\Select\SourceInterface;

/**
* @internal
Expand All @@ -21,6 +22,7 @@ public function __construct(
public object $entity,
public Node $node,
public State $state,
public SourceInterface $source,
public \DateTimeImmutable $timestamp
) {
}
Expand Down
31 changes: 22 additions & 9 deletions src/EventDrivenCommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);

Expand Down