bruno-barros/w.eloquent-bus

w.eloquent Command Bus Pattern.

dev-master 2015-01-14 03:16 UTC

This package is auto-updated.

Last update: 2024-03-29 02:25:32 UTC


README

Command Bus for w.eloquent project.

Build Status

Installation

Update your composer: "bruno-barros/w.eloquent-bus": "dev-master"

Set the service provider: 'Weloquent\Bus\BusServiceProvider'

Run: composer update

How to use it

Create your command and handler.

// The command. Just a DTO.
class DoSomethingCommand {
	
	public $property = 'default value';
	
}

// On same folder, the handler.
class DoSomethingCommandHandler implements Weloquent\Bus\Contracts\CommandHandler {
	
	public function handler($command){
		
		// do whatever you need
	
	}
	
}

Use the trait and call the execute method.

class MyClass {

	use Weloquent\Bus\CommanderTrait;
	
	public function myMethod()
	{
		$input = ['property' => 'some value'];
					
		$this->execute(new Your\Namespace\DoSomething, $input);
	
	}
}

Or leave the commander handle the input if it exists on GET or POST array;

class MyClass {

	use Weloquent\Bus\CommanderTrait;
	
	// From a POST
	public function myMethod()
	{						
		$this->execute(new Your\Namespace\DoSomething);
	
	}
}

Decorators

You can decorate the command object (DTO).

Look:

  • The decorator must implements Weloquent\Bus\Contracts\CommandHandler.

  • The decorator should return the command object. Can be the original, or modified one.

// Decorator
class DecoratorCommandHandler implements Weloquent\Bus\Contracts\CommandHandler {
        
        public function handler($command)
        {	
            // apply any modification...
            
            return $command;    		
        }
    }


// Decorating
class MyClass {

	use Weloquent\Bus\CommanderTrait;
	
	// From a POST
	public function myMethod()
	{						
		$decorator = ['Your\Namespace\DecoratorCommandHandler'];
	
		$this->execute(new Your\Namespace\DoSomething, null, $decorator);
	
	}
}