PHP code example of sbooker / domain-events

1. Go to this page and download the library: Download sbooker/domain-events 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/ */

    

sbooker / domain-events example snippets




namespace Domain;

use Sbooker\DomainEvents\DomainEntity;
use Sbooker\DomainEvents\DomainEvent;
use Sbooker\DomainEvents\DomainEventCollector;

class SomethingOccurred extends  DomainEvent {}

class SomeAggregateRoot implements DomainEntity
{
    use DomainEventCollector;
    
    public function doSomethingOfDomainLogic()
    {
        // do
        $this->publish(new SomethingOccurred());
    }
}

class SomeAggregateRoot implements DomainEntity
{
    use DomainEventCollector;
    
    ...
}

$publisher = new class implements Publihser { ... }

$aggregateRoot = new SomeAggregateRoot();
$aggregateRoot->dispatchEvents($publisher)


class Entity implements DomainEntity { ... }

class SomeAggregateRoot implements DomainEntity
{
    use DomainEventCollector { dispatchEvents as doDispatchEvents; }
    
    private Entity $entity;
   
    ...
    
    public function dispatchEvents(Publisher $publisher): void
    {
        $this->doDispatchEvents($publisher);
        $this->entity->dispatchEvents($publisher);
    }
}