swag-industries / doctrine-domain-events
Installs: 11 485
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 4
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^1.8|^2.0
- doctrine/orm: ^2.18.3|^3.0
- symfony/event-dispatcher: ^6.4|^7.0
Requires (Dev)
- doctrine/orm: ^2.18.3
- friendsofphp/php-cs-fixer: ^3.49.0
- monolog/monolog: ^2.8.0 || ^3.2.0
- phpspec/prophecy-phpunit: ^2.0.1
- phpunit/phpunit: ^9.5 || ^10
- symfony/asset: ^6.4 || ^7.0
- symfony/debug-pack: ^1.0
- symfony/form: ^6.4 || ^7.0
- symfony/framework-bundle: ^6.4 || ^7.0
- symfony/property-access: ^6.4 || ^7.0
- symfony/serializer: ^6.4 || ^7.0
- symfony/var-dumper: ^6.4 || ^7.0
- symfony/yaml: ^6.4 || ^7.0
Conflicts
- doctrine/orm: <2.6.3
This package is auto-updated.
Last update: 2024-12-05 23:47:43 UTC
README
This library is design to help you to build your application with a Domain Design Development approach.
It is well integrated with:
- Symfony >= 5.5 (for >=4.4 compatibility, install the version 2.2 of Doctrine domain events)
- ApiPlatform >= 2.1
- Doctrine >= 2.5
- PHP >= 7.4
But you can use it with any PHP project.
Here are some slides that explain how we get there.
Features
Domain Events:
- Domain event dispatcher
- Injection of the dispatcher in Doctrine entities
- Symfony serializer integration
- Learn how do more with our cookbooks
Drawbacks
This library is build to allow you to use Doctrine models as Domain model. This has some cost: you can't instantiate domain model by hand anymore. This means that you need a factory for any of the usage of your domain model.
This component provides the implementation for Symfony serializer and Doctrine. For your own
needs you should use the class (service if you use the bundle) Biig\Component\Domain\Model\Instantiator\Instantiator
.
Installation
composer require swag-industries/doctrine-domain-events
Basic usage
class YourModel extends DomainModel { public const CREATION = 'creation'; public function __construct() { $this->dispatch(new DomainEvent($this), self::CREATION); } }
class DomainRule implements DomainRuleInterface { public function on() { return YourModel::CREATION; } public function execute(DomainEvent $event) { // Do Something on your model creation } }
As your model needs a dispatcher you need to call the setDispatcher()
method any time you create a new instance of your model. To avoid doing this manually you can use the Instantiator
that the library provides.
It doesn't use the constructor to add the dispatcher because in PHP you can create objects without the constructor. For instance, that's what Doctrine does.
use Biig\Component\Domain\Model\Instantiator\Instantiator; use Doctrine\ORM\EntityManager; class SomeController { public function index(Instantiator $instantiator, EntityManager $entityManager) { $model = $instantiator->instantiate(YourModel::class); $entityManager->persist($model); $entityManager->flush(); } }
Integration to Symfony
Use the bundle :
<?php // config/bundles.php return [ // ... Biig\Component\Domain\Integration\Symfony\DomainBundle::class => ['all' => true], ];
Learn more about Symfony Integration