Skip to content

Commit 7913228

Browse files
msmakouzroxblnfk
andauthored
Add Source to events (#18)
Co-authored-by: roxblnfk <[email protected]>
1 parent d4aebdd commit 7913228

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# CHANGELOG
2+
3+
4+
v1.1.0 under development
5+
------------------------
6+
- Add `SourceInterface $source` property to the `MapperEvent`
7+
8+
v1.0.0 (22.12.2021)
9+
-------------------
10+
- Add behaviors:
11+
- `CreatedAt`
12+
- `OptimisticLock`
13+
- `SoftDelete`
14+
- `UpdatedAt`
15+
- `EventListener`
16+
- `Hook`
17+
- Add `EventDrivenCommandGenerator` with custom event dispatcher.
18+
- Supported events: `OnCreate`, `OnDelete` and `OnUpdate`

src/Event/MapperEvent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cycle\ORM\Heap\Node;
88
use Cycle\ORM\Heap\State;
99
use Cycle\ORM\MapperInterface;
10+
use Cycle\ORM\Select\SourceInterface;
1011

1112
/**
1213
* @internal
@@ -21,6 +22,7 @@ public function __construct(
2122
public object $entity,
2223
public Node $node,
2324
public State $state,
25+
public SourceInterface $source,
2426
public \DateTimeImmutable $timestamp
2527
) {
2628
}

src/EventDrivenCommandGenerator.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
namespace Cycle\ORM\Entity\Behavior;
66

77
use Cycle\ORM\Command\CommandInterface;
8-
use Cycle\ORM\ORMInterface;
9-
use Cycle\ORM\SchemaInterface;
10-
use Cycle\ORM\Transaction\CommandGenerator;
11-
use Cycle\ORM\Transaction\Tuple;
128
use Cycle\ORM\Entity\Behavior\Dispatcher\Dispatcher;
139
use Cycle\ORM\Entity\Behavior\Dispatcher\ListenerProvider;
1410
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate;
1511
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnDelete;
1612
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnUpdate;
13+
use Cycle\ORM\ORMInterface;
14+
use Cycle\ORM\SchemaInterface;
15+
use Cycle\ORM\Transaction\CommandGenerator;
16+
use Cycle\ORM\Transaction\Tuple;
1717
use Psr\Container\ContainerInterface;
1818
use Psr\EventDispatcher\EventDispatcherInterface;
1919

@@ -22,7 +22,6 @@ final class EventDrivenCommandGenerator extends CommandGenerator
2222
private EventDispatcherInterface $eventDispatcher;
2323
private \DateTimeImmutable $timestamp;
2424

25-
// todo: add custom listener interface
2625
public function __construct(SchemaInterface $schema, ContainerInterface $container)
2726
{
2827
$listenerProvider = new ListenerProvider($schema, $container);
@@ -33,10 +32,11 @@ public function __construct(SchemaInterface $schema, ContainerInterface $contain
3332
protected function storeEntity(ORMInterface $orm, Tuple $tuple, bool $isNew): ?CommandInterface
3433
{
3534
$role = $tuple->node->getRole();
35+
$src = $orm->getSource($role);
3636

3737
$event = $isNew
38-
? new OnCreate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp)
39-
: new OnUpdate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp);
38+
? new OnCreate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp)
39+
: new OnUpdate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp);
4040

4141
$event->command = parent::storeEntity($orm, $tuple, $isNew);
4242

@@ -55,8 +55,11 @@ protected function generateParentStoreCommand(
5555
bool $isNew
5656
): ?CommandInterface {
5757
$mapper = $orm->getMapper($parentRole);
58+
$source = $orm->getSource($parentRole);
59+
60+
$event =
61+
new OnCreate($parentRole, $mapper, $tuple->entity, $tuple->node, $tuple->state, $source, $this->timestamp);
5862

59-
$event = new OnCreate($parentRole, $mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp);
6063
$event->command = $isNew
6164
? $mapper->queueCreate($tuple->entity, $tuple->node, $tuple->state)
6265
: $mapper->queueUpdate($tuple->entity, $tuple->node, $tuple->state);
@@ -69,7 +72,17 @@ protected function generateParentStoreCommand(
6972
protected function deleteEntity(ORMInterface $orm, Tuple $tuple): ?CommandInterface
7073
{
7174
$role = $tuple->node->getRole();
72-
$event = new OnDelete($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $this->timestamp);
75+
$source = $orm->getSource($role);
76+
77+
$event = new OnDelete(
78+
$role,
79+
$tuple->mapper,
80+
$tuple->entity,
81+
$tuple->node,
82+
$tuple->state,
83+
$source,
84+
$this->timestamp
85+
);
7386

7487
$event->command = parent::deleteEntity($orm, $tuple);
7588

0 commit comments

Comments
 (0)