phpgears / aggregate
Aggregate base
Requires
- php: ^7.1
- phpgears/event: ~0.3.2
- phpgears/identity: ~0.2
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.1
- friendsofphp/php-cs-fixer: ^2.0
- hashids/hashids: ^3.0
- infection/infection: ^0.9
- pascaldevink/shortuuid: ^2.1
- phpmd/phpmd: ^2.0
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ~0.11.12
- phpstan/phpstan-deprecation-rules: ~0.11.2
- phpstan/phpstan-strict-rules: ~0.11.1
- phpunit/phpunit: ^7.0|^8.0
- povils/phpmnd: ^2.0
- roave/security-advisories: dev-master
- sebastian/phpcpd: ^4.0
- squizlabs/php_codesniffer: ^3.0
- thecodingmachine/phpstan-strict-rules: ^0.11.2
Suggests
- hashids/hashids: for hashed UUID based identities
- pascaldevink/shortuuid: for short UUID based identities
This package is auto-updated.
Last update: 2024-12-17 07:33:41 UTC
README
Aggregate
Aggregate base
Installation
Composer
composer require phpgears/aggregate
Usage
Require composer autoload file
require './vendor/autoload.php';
Aggregate identity
Aggregate identities are provided by gears/identity, head over there to learn about them
Aggregate root
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
Entities can implement Gears\Aggregate\Entity
interface. You can extend from Gears\Aggregate\AbstractEntity
for simplicity
Events
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); }
Contributing
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.
See file CONTRIBUTING.md
License
See file LICENSE included with the source code for a copy of the license terms.