PHP code example of gbprod / domain-event

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

    

gbprod / domain-event example snippets




namespace GBProd\Acme\Event;

use GBProd\DomainEvent\DomainEvent;

class SomethingHappenedEvent implements DomainEvent
{
    private $id;
    
    public function __construct($id)
    {
        $this->id = $id;
    }
    
    public function getAggregateId()
    {
        return $id;
    }
}



namespace GBProd\Acme\Entity;

use GBProd\DomainEvent\EventProvider;
use GBProd\DomainEvent\EventProviderTrait;

final class MyEntity implements EventProvider
{
    use EventProviderTrait;
    
    public function doSomething()
    {
        $this->raise(
            new SomethingHappenedEvent($this->id)
        );
    }
}



namespace GBProd\Acme\Repository;

use GBProd\DomainEvent\EventProvider;

class MyEntityRepository
{
    public function save(MyEntity $entity)
    {
        $this->persist($entity);
        
        $this->dispatcher->dispatch($entity);
    }
}