Various additions for domain driven development inside Symfony.
To use this bundle, require it in Composer
composer require geekcell/ddd-bundleExtend from AggregateRoot to record and commit domain events. Domain events must implement the (marker) interface DomainEvent. Events will be dispatched via the currently configured Symfony event dispatcher.
use GeekCell\DDDBundle\Domain\Event\DomainEvent; use GeekCell\DDDBundle\Domain\Model\AggregateRoot; class OrderPlacedEvent implements DomainEvent { ... } class Order extends AggregateRoot { public function save(): void { $this->record(new OrderPlacedEvent()); } ... } $order = new Order(); $order->save(); $order->commit(); // <- Events will be dispatchedHint: If you want to dispatch an event directly, use AggregateRoot::dispatch() instead of AggregateRoot::record().
If you cannot (or don't want to) extend from AggregateRoot, you can alternative use DispatchableTrait to add dispatching capabilities to any class. The former is however the recommended way.
Facades are heavily inspired by Laravel's Facades and are more or less singletons on steroids. They are basically a shortcut to services inside the DIC.
use GeekCell\DDDBundle\Support\Facades\EventDispatcher; EventDispatcher::dispatch($someEvent); // same as: $container->get('event_dispatcher')->dispatch($someEvent);You can create your own facades by extending from Facade and implementing the Facade::getFacadeAccessor() method to return the DIC service alias.
use GeekCell\DDDBundle\Support\Facades\Facade; class Logger extends Facade { protected static function getFacadeAccessor(): string { return 'logger'; } }Although facades are better testable than regular singletons, it is highly recommended to only use them sparringly and always prefer normal dependency injection when possible.