PHP code example of studioignis / evt

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

    

studioignis / evt example snippets


$dispatcher = new StudioIgnis\Evt\EventDispatcher;
$dispatcher->addListener(
    'UserWasRegistered',
    new SendEmailUponUserRegistration($mailer)
);

$dispatcher->dispatch([new Acme\Events\UserWasRegistered($name, $email)]);

namespace Acme\Events;

use StudioIgnis\Evt\Event;
use StudioIgnis\Evt\Traits\EventName;

class UserWasRegistered implements Event
{
    use EventName;
    
    private $name;
    
    private $email;
    
    public function __construct($name, $email)
    {
        $this->name = $name;
        $this->email = $email;
    }
    
    public function getName()
    {
        return $this->name;
    }
    
    public function getEmail()
    {
        return $this->email;
    }
}

namespace Acme\Events;

use StudioIgnis\Evt\EventListener;

class SendEmailUponUserRegistration implements EventListener
{
    private $mailer;
    
    public function __construct($mailer)
    {
        // Your hypothetical mailer class
        $this->mailer = $mailer;
    }
    
    public function handle(Event $event)
    {
        $this->mailer->sendWelcomeEmail($event->getEmail(), $event->getName());
    }
}

$dispatcher->addListener('SomeEvent', 'Acme\Events\SomeListener');
// or
$dispatcher->addListener('SomeEvent', 'listeners.some_event_listener');

$dispatcher = new Dispatcher($container);

namespace Acme;

use StudioIgnis\Evt\Traits\HasDomainEvents;

class User implements EventListener
{
    use HasDomainEvents;
    
    public function __construct($name, $email)
    {
        // Init your params, invariants, etc
        
        $this->raise(new Events\UserWasRegistered($name, $email));
    }
}

$dispatcher->dispatch($user->releaseEvents());

'StudioIgnis\Evt\Laravel\ServiceProvider'

class SomeController extends Controller
{
    private $dispatcher;
    
    public function __construct(Dispatcher $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }
}

return [
    /**
     * Which concrete dispatcher implementation to map to
     * StudioIgnis\Evt\Dispatcher contract.
     */
    'dispatcher' => 'StudioIgnis\Evt\EventDispatcher',
];

shell
$ php artisan config:publish studioignis/evt