PHP code example of robrogers3 / commandbus

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/ */

    

robrogers3 / commandbus example snippets


    App::instance('Dispatcher', $dispatcher);

    $listeners = getAppListeners();

    foreach ($listeners as $listener) {
        $dispatcher->listen('App.*', $listener);
    }

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;
    }
}

$command = new RegisterUserCommand("Rob Rogers");

/** @var \RobRogers\CommandBus\BaseCommandBus $commandBus */
$commandBus = App::Make('\RobRogers\CommandBus\BaseCommandBus');

$commandBus->execute($command);
 php
    $listeners = array(
        'Acme\SendWelcomeEmail',
        'Acme\AddToLdap',
        'Acme\ConfigurePermissions'
    );
 php
namespace Acme;

class RegisterUserCommand
{
    public $username;

    public function __construct($username)
    {
        $this->username = $username;
    }
}