1. Go to this page and download the library: Download robrogers3/commandbus 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/ */
namespace Acme;
use RobRogers\CommandBus\Eventing\EventListener;
class SendWelcomeEmail extends EventListener
{
public function whenUserHasRegistered(UserWasRegistered $event)
{
dump( 'Sending mail to ' . $event->user->username);
}
}
namespace Acme;
use RobRogers\CommandBus\CommandHandler;
class UserRegisterCommandHandler implements CommandHandler
{
/**
* @var EventDispatcher
*/
private $dispatcher;
/**a
* @var EventGenerator
*/
private $eventGenerator;
/**
* @param EventDispatcher $dispatcher
* @param EventGenerator $eventGenerator
*/
public function __construct(EventDispatcher $dispatcher, EventGenerator $eventGenerator)
{
$this->dispatcher = $dispatcher;
$this->eventGenerator = $eventGenerator;
}
public function handle(/* user registered command */ $command)
{
$event = new Acme\UserHasRegistered($command);
$this->eventGenerator->register($event); //you can register many events
}
}
namespace Acme;
/**
* I am THE EVENT
* the command data get's shoved inside me. like $this->user->username
*/
class UserHasRegisteredEvent
{
public $user;
/** @var UserRegister $user */
public function __construct($user)
{
$this->user = $user;
}
}