The domain event dispatcher is a special dispatcher in your application that dispatch only domain events.
To make a new rule (its a listener) you should implement the DomainRuleInterface
.
<?php
use Biig\Component\Domain\Rule\DomainRuleInterface;
use Biig\Component\Domain\Event\DomainEvent;
$dispatcher->addRule(new class implements DomainRuleInterface {
public function execute(DomainEvent $event) {
// add some specific behavior
}
public function on() {
return 'on.event';
}
});
This is useful for post flush processing.
<?php
use Biig\Component\Domain\Event\DomainEvent;
use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface;
$dispatcher->addRule(new class implements PostPersistDomainRuleInterface {
public function execute(DomainEvent $event) {
// add some specific behavior
}
public function after() {
return 'on.event'; // You have to specify the model
}
});
If you use the Symfony Bundle with auto-configuration of your services. You don't have anything to do.
If you don't auto-discover your services and don't enable auto-configuration, then you will need to add the tag:
My\Domain\Rule:
tags:
- { name: biig_domain.rule }
If you don't want to use the given interface or want more control on the configuration you still can configure your service by hand:
My\Domain\Rule:
tags:
# You may add many tags to add many listeners to your business rule
- { name: biig_domain.rule, event: 'your.event.name', method: 'execute', priority: 0 }
Notice: the priority field is optional.
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']