A Command Bus for PHP 5.3
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
README.md
composer.json
phpunit.xml

README.md

Build Status #Command#

This is a simple command bus for use with PHP 5.3

Use

<?php
    namespace Command\Command;

    class MyHandlerCommand
    {
        public $someParam;

        public function __construct($someParam)
        {
            $this->someParam = $someParam;
        }
    }
<?php
    namespace Command\Handler;

    class MyHandlerHandler implements HandlerInterface, ApplicationInterface
    {
        protected $app;
        
        public function handle($object)
        {
            echo $app['db']->persist($object->someParam);
        }
        
        public function setApplication($app)
        {
            $this->app = $app;
        }
    }
    $command = new Command\Command(new Command\Resolver);
    $command->setApplication($app);
    $command->dispatch(new MyHandlerCommand($someParam));

This will resolve MyHandlerHandler which implements HandlerInterface, and provides access to your application container.

Self handling

Alternatively you might not want to separate your DTOs from your commands. In this case, you can just implement the self handling interface which will call the handle method.

<?php
namespace Command\Command;
    
class MyHandlerCommand implements SelfHandling
{
    protected $someParam;

    public function __construct($someParam)
    {
        $this->someParam = $someParam;
    }
         
    public function handle()
    {
        return $this->someParam * 2;
    }
}

Finally, you can get access to the application by implementing ApplicationInterface

<?php
class TestCommand implements SelfHandling, ApplicationInterface
{
    private $name;
    private $application;

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

    public function handle()
    {
        $app = $this->application;
        var_dump($app($this->name));
    }

    /**
     * @param $application
     * @return void
     */
    public function setApplication($application)
    {
        $this->application = $application;
    }

}
    $resolver = new \Command\Resolver\SelfHandlingResolver();
    $resolver->setApplication(function($a){ return $a*2;});

    $command = new Command\Command($resolver);
    $command->dispatch(new TestCommand(2));