-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Xerkus/feature/ar-traits
Extract aggregate root into traits to make it easier to avoid domain extending infrastructure
- Loading branch information
Showing
7 changed files
with
411 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/event-sourcing. | ||
* (c) 2014-2017 prooph software GmbH <[email protected]> | ||
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\EventSourcing\Aggregate; | ||
|
||
use Prooph\EventSourcing\AggregateChanged; | ||
|
||
trait EventProducerTrait | ||
{ | ||
/** | ||
* Current version | ||
* | ||
* @var int | ||
*/ | ||
protected $version = 0; | ||
|
||
/** | ||
* List of events that are not committed to the EventStore | ||
* | ||
* @var AggregateChanged[] | ||
*/ | ||
protected $recordedEvents = []; | ||
|
||
/** | ||
* Get pending events and reset stack | ||
* | ||
* @return AggregateChanged[] | ||
*/ | ||
protected function popRecordedEvents(): array | ||
{ | ||
$pendingEvents = $this->recordedEvents; | ||
|
||
$this->recordedEvents = []; | ||
|
||
return $pendingEvents; | ||
} | ||
|
||
/** | ||
* Record an aggregate changed event | ||
*/ | ||
protected function recordThat(AggregateChanged $event): void | ||
{ | ||
$this->version += 1; | ||
|
||
$this->recordedEvents[] = $event->withVersion($this->version); | ||
|
||
$this->apply($event); | ||
} | ||
|
||
abstract protected function aggregateId(): string; | ||
|
||
/** | ||
* Apply given event | ||
*/ | ||
abstract protected function apply(AggregateChanged $event): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/event-sourcing. | ||
* (c) 2014-2017 prooph software GmbH <[email protected]> | ||
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\EventSourcing\Aggregate; | ||
|
||
use Iterator; | ||
use Prooph\EventSourcing\AggregateChanged; | ||
use RuntimeException; | ||
|
||
trait EventSourcedTrait | ||
{ | ||
/** | ||
* Current version | ||
* | ||
* @var int | ||
*/ | ||
protected $version = 0; | ||
|
||
/** | ||
* @throws RuntimeException | ||
*/ | ||
protected static function reconstituteFromHistory(Iterator $historyEvents): self | ||
{ | ||
$instance = new static(); | ||
$instance->replay($historyEvents); | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* Replay past events | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
protected function replay(Iterator $historyEvents): void | ||
{ | ||
foreach ($historyEvents as $pastEvent) { | ||
/** @var AggregateChanged $pastEvent */ | ||
$this->version = $pastEvent->version(); | ||
|
||
$this->apply($pastEvent); | ||
} | ||
} | ||
|
||
abstract protected function aggregateId(): string; | ||
|
||
/** | ||
* Apply given event | ||
*/ | ||
abstract protected function apply(AggregateChanged $event): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/EventStoreIntegration/ClosureAggregateTranslator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/event-sourcing. | ||
* (c) 2014-2017 prooph software GmbH <[email protected]> | ||
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\EventSourcing\EventStoreIntegration; | ||
|
||
use Iterator; | ||
use Prooph\Common\Messaging\Message; | ||
use Prooph\EventSourcing\Aggregate\AggregateTranslator as EventStoreAggregateTranslator; | ||
use Prooph\EventSourcing\Aggregate\AggregateType; | ||
use RuntimeException; | ||
|
||
final class ClosureAggregateTranslator implements EventStoreAggregateTranslator | ||
{ | ||
protected $aggregateIdExtractor; | ||
protected $aggregateReconstructor; | ||
protected $pendingEventsExtractor; | ||
protected $replayStreamEvents; | ||
protected $versionExtractor; | ||
|
||
/** | ||
* @param object $eventSourcedAggregateRoot | ||
* | ||
* @return int | ||
*/ | ||
public function extractAggregateVersion($eventSourcedAggregateRoot): int | ||
{ | ||
if (null === $this->versionExtractor) { | ||
$this->versionExtractor = function (): int { | ||
return $this->version; | ||
}; | ||
} | ||
|
||
return $this->versionExtractor->call($eventSourcedAggregateRoot); | ||
} | ||
|
||
/** | ||
* @param object $anEventSourcedAggregateRoot | ||
* | ||
* @return string | ||
*/ | ||
public function extractAggregateId($anEventSourcedAggregateRoot): string | ||
{ | ||
if (null === $this->aggregateIdExtractor) { | ||
$this->aggregateIdExtractor = function (): string { | ||
return $this->aggregateId(); | ||
}; | ||
} | ||
|
||
return $this->aggregateIdExtractor->call($anEventSourcedAggregateRoot); | ||
} | ||
|
||
/** | ||
* @param AggregateType $aggregateType | ||
* @param Iterator $historyEvents | ||
* | ||
* @return object reconstructed AggregateRoot | ||
*/ | ||
public function reconstituteAggregateFromHistory(AggregateType $aggregateType, Iterator $historyEvents) | ||
{ | ||
if (null === $this->aggregateReconstructor) { | ||
$this->aggregateReconstructor = function ($historyEvents) { | ||
return static::reconstituteFromHistory($historyEvents); | ||
}; | ||
} | ||
|
||
$arClass = $aggregateType->toString(); | ||
|
||
if (! class_exists($arClass)) { | ||
throw new RuntimeException( | ||
sprintf('Aggregate root class %s cannot be found', $arClass) | ||
); | ||
} | ||
|
||
return ($this->aggregateReconstructor->bindTo(null, $arClass))($historyEvents); | ||
} | ||
|
||
/** | ||
* @param object $anEventSourcedAggregateRoot | ||
* | ||
* @return Message[] | ||
*/ | ||
public function extractPendingStreamEvents($anEventSourcedAggregateRoot): array | ||
{ | ||
if (null === $this->pendingEventsExtractor) { | ||
$this->pendingEventsExtractor = function (): array { | ||
return $this->popRecordedEvents(); | ||
}; | ||
} | ||
|
||
return $this->pendingEventsExtractor->call($anEventSourcedAggregateRoot); | ||
} | ||
|
||
/** | ||
* @param object $anEventSourcedAggregateRoot | ||
* @param Iterator $events | ||
* | ||
* @return void | ||
*/ | ||
public function replayStreamEvents($anEventSourcedAggregateRoot, Iterator $events): void | ||
{ | ||
if (null === $this->replayStreamEvents) { | ||
$this->replayStreamEvents = function ($events): void { | ||
$this->replay($events); | ||
}; | ||
} | ||
$this->replayStreamEvents->call($anEventSourcedAggregateRoot, $events); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.