Aggregate base
composer require phpgears/aggregate
Require composer autoload file
require './vendor/autoload.php';
Aggregate identities are provided by gears/identity, head over there to learn about them
Aggregate roots should implement Gears\Aggregate\AggregateRoot
interface. You can extend from Gears\Aggregate\AbstractAggregateRoot
for simplicity
use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;
class CustomAggregate extends AbstractAggregateRoot
{
public static function instantiate(Identity $identity): self
{
return new self($identity);
}
}
Mind that AbstractAggregateRoot constructor is protected forcing you to create static named constructors methods
Entities can implement Gears\Aggregate\Entity
interface. You can extend from Gears\Aggregate\AbstractEntity
for simplicity
Aggregate roots can record gears/event as operations are performed
use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;
class CustomAggregate extends AbstractAggregateRoot
{
public static function instantiate(Identity $identity): self
{
return new self($identity);
}
public function doSomething(): void
{
// do something
$this->recordEvent(new SomethingHappened());
}
}
These events could be collected afterwards and sent to an event bus such as gears/event
$customAggregate = CustomAggregate::instantiate(
UuidIdentity::fromString('4c4316cb-b48b-44fb-a034-90d789966bac')
);
$customAggregate->doSomething();
foreach ($customAggregate->collectRecordedEvents() as $event) {
/** @var \Gears\Event\EventBus $eventBus */
$eventBus->dispatch($event);
}
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.
See file CONTRIBUTING.md
See file LICENSE included with the source code for a copy of the license terms.