PHP code example of dsantang / domain-events-doctrine
1. Go to this page and download the library: Download dsantang/domain-events-doctrine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dsantang / domain-events-doctrine example snippets
use Dsantang\DomainEventsDoctrine\EventsRecorder\DoctrineEventsRecorder;
use Dsantang\DomainEventsDoctrine\Dispatcher\DoctrineEventsDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Doctrine\Common\EventManager;
// ...
$evm = new EventManager();
$dispatcher = new EventDispatcher();
// You can register here your domain event listeners to your dispatcher...
$tracker = new EventsTracker();
// Make sure they share the same `EventsTracker` instance.
$evm->addEventListener([Events::onFlush], new DoctrineEventsRecorder($tracker));
$evm->addEventListener([Events::postFlush], new DoctrineEventsDispatcher($tracker, $dispatcher));
// Remember to correctly wire this EventManager to your ORM.
use Dsantang\DomainEvents\DomainEvent;
use Dsantang\DomainEventsDoctrine\Outbox\Converter;
use Dsantang\DomainEventsDoctrine\Outbox\OutboxEntry;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
final class YourOutboxConverter implements Converter
{
public function convert(DomainEvent $domainEvent) : OutboxEntry
{
return new YourOutboxEntry($domainEvent);
}
}
final class YourOutboxEntry implements OutboxEntry
{
/** @var DomainEvent */
private $domainEvent;
public function __construct(DomainEvent $domainEvent)
{
$this->domainEvent = $domainEvent;
}
public function getName() : string
{
return 'OrderDispatched';
}
public function getAggregateId() : UuidInterface
{
return Uuid::fromString('d1702762-548b-11e9-8647-d663bd873d93');
}
public function getAggregateType() : string
{
return 'Order';
}
public function getPayloadType() : string
{
return 'OrderStructure';
}
public function getMessageKey() : string
{
return 'd663bd873d93';
}
public function getMessageRoute() : string
{
return 'aggregate.order';
}
public function getMessageType() : string
{
return 'OrderCreated';
}
public function getPayload() : string
{
return json_encode($this->domainEvent)
}
public function getSchemaVersion() : int
{
return 1;
}
}
namespace YourNamespace;
use Doctrine\ORM\Mapping as ORM;
use Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass;
/**
* @ORM\Entity()
* @ORM\Table
*/
class YourOutboxEntity extends OutboxMappedSuperclass
{
/**
* @ORM\Column(type="string")
*
* @var string
*/
private $someAdditionalField;
}
use Dsantang\DomainEventsDoctrine\Outbox\MapBased;
use Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass;
// Your class must extend OutboxMappedSuperclass
$yourOutboxEntity = new YourOutboxEntity();
$mapBased = new MapBased($yourOutboxEntity);
$mapBased->addConverter('YouNamespace\YourDomainEvent', new YourOutboxConverter());
// Always use with OnFlush event
$evm->addEventListener([Events::onFlush], $mapBased);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.