PHP code example of gbprod / domain-event-bundle

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


public function registerBundles()
{
    $bundles = array(
        new GBProd\DomainEventBundle\DomainEventBundle(),
    );
}



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\AcmeBundle\Repository;

use GBProd\DomainEvent\EventProvider;
use GBProd\DomainEvent\Dispatcher;

class MyEntityRepository
{
    public function __construct(EntityManager $em, DomainEventDispatcher $dispatcher)
    {
        $this->em         = $em;
        $this->dispatcher = $dispatcher;
    }
    
    public function save(MyEntity $entity)
    {
        $this->em->persist($entity);
        $this->em->flush();
        
        $this->dispatcher->dispatch($entity);
    }
}



namespace GBProd\AcmeBundle\Listener;

use Symfony\Component\EventDispatcher\Event;

class MyListener
{
    public function onSomethingHappened(Event $event) 
    {
        $domainEvent = $event->getDomainEvent();
        
        // Use it now
    }
}