biig-io / domaincomponent Goto Github PK
View Code? Open in Web Editor NEWDomain Events with Doctrine
License: MIT License
Domain Events with Doctrine
License: MIT License
Today the DomainEventDispatcher is not traced in any way. It would be great to have information about triggered domain events. I suggest to add an extension for the webprofiler of Symfony (in the bundle)
@Nek- I can't seem to get it to work with Symfony 4.1.
I've added the following config/packages/biig_domain.yaml
:
biig_domain:
# It modifies the DoctrineBundle configuration to register a new
# ClassMetadataInfo class so the instantiator now set the domain event
# dispatcher to your models automatically
override_doctrine_instantiator: true
# By default it will override the doctrine instantiator only for
# the "default" entity manager of your application. You can specify
# many entity managers if you want.
entity_managers: []
# Post persist events are not activated by default, you need to enable the post persist listeners
persist_listeners:
# As doctrine supports many connections, you need to enable your connections one by one
doctrine: ['default']
My Listener:
<?php
namespace App\Application\EventListeners;
use Biig\Component\Domain\Event\DomainEvent;
use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface;
class VerificationCreatedListener implements PostPersistDomainRuleInterface {
/**
* Returns an array of event or a string it listen on.
*
* @return array|string
*/
public function after() {
return 'vc.created';
}
/**
* @param DomainEvent $event
*/
public function execute( DomainEvent $event ) {
var_dump( $event ); die;
}
}
My Entity:
<?php
namespace App\Application\Entity;
use App\Application\Entity\VerificationCode\MobileNumber;
use App\Application\Entity\VerificationCode\SecretCode;
use Biig\Component\Domain\Event\DomainEvent;
use Biig\Component\Domain\Model\DomainModel;
class VerificationCode extends DomainModel {
/**
* @var int
*/
private $id;
/**
* @var MobileNumber
*/
private $mobileNumber;
/**
* @var SecretCode
*/
private $code;
public function __construct( $id, MobileNumber $mobileNumber, SecretCode $code ) {
$this->id = $id;
$this->mobileNumber = $mobileNumber;
$this->code = $code;
$this->dispatch('vc.created', new DomainEvent($this));
}
public function id() {
return $this->id;
}
public function mobileNumber(): MobileNumber {
return $this->mobileNumber;
}
public function code(): SecretCode {
return $this->code;
}
public function shouldSendNewCode(): bool {
return !$this->code->isSent() || $this->code->isExpired();
}
}
The execute
method is never called. It seems the instantiator is not being set. Am I missing something in the configuration?
Things to do:
CHANGELOG.md
This is due before the 17th April.
We decorate serializer. That means that we add behavior. Because of this working with something else than ModelInterface objects will fail with the serializer. (luckily we have an option to remove this feature ๐ผ )
Things to do:
This is due before the 28th February.
Luckily nobody had this problem until now ๐
The DomainExtension is not tested for entity_manager
configuration. It attempt to retrieve the following:
[
"entity_managers": ['default', 'another']
]
But in reality it's more something like that:
[
[
"entity_managers": ['default', 'another']
]
]
Something may be wrong in this configuration. Luckily the default case will work. This really needs a unit test.
Currently to have a good code in rules, we need to check if the event is an instance of the event we expect. (required at least by phpstan)
We could improve the situation by replacing DomainEvent by DomainEventInterface, because we can use children interface as type afterwards.
usage defined here: https://github.com/biig-io/DomainComponent/blob/master/docs/domain_event_dispatcher.md#symfony-integration potentially fail because no event is specified
This is not normal (because the rule is actually a subscriber and return the event it's bound on) and should be fix.
Related lines:
This issue is complicated to debug but I have it on a project.
If you want to test many cases so you have it, you can and I hope you will find it. I'm still trying to make a reproducer at this state.
This line looks suspicious:
It should probably be the following:
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
Things to do:
CHANGELOG.md
composer.json
(so the dev-master
become the 1.3.x-dev
branch)This is due before the 9th March.
Some people may want to extend from another class than DomainModel
, using an interface and providing a trait for the content of the class would be a nice idea :) .
We are aware than Symfony 4.3 changed the API of the dispatcher you know today, this impact us directly. We will not provide a fix that would look like that (ugly, isn't it ?).
What we are going to do is release a new major version of the domain component compatible only with Symfony >= 4.3.
This will obviously come before Symfony 5.0. If you really want a change on the current version, PR are open.
Some issues are super interesting to fix here but require to break the compatibility. I suggest going on a v3 of the library.
Also, v3 may remove any bc layer for Symfony 4 and ensure a clean code base.
In Melodiia, it exists a class named DomainObjectsDataMapper that build objects. Adding a decorator that adds the dispatcher inside the object would be a nice improvement.
Delayed events happen in the case you want it to happen after a flush. In some case you may want to implement both DomainRuleInterface
and PostPersistRuleInterface
, in that case having a possibility to know in what case you are is interesting.
To do: add a new method isDelayed
to the domain event.
For PostPersistRules to work, we need to configure the domain component. It's not a feature available by default.
This is documented in the reference but it's not enough:
It makes no sense to extends the domain event dispatcher and is a pain for maintainance.
Things to do:
CHANGELOG.md
composer.json
(so the dev-master
become the 1.4.x-dev
branch)This is due before the 6th April.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.