bruno-barros/w.eloquent-bus

w.eloquent 命令总线模式。

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

This package is auto-updated.

Last update: 2024-09-29 03:39:46 UTC


README

w.eloquent 项目提供命令总线。

Build Status

安装

更新你的 composer: "bruno-barros/w.eloquent-bus": "dev-master"

设置服务提供者: 'Weloquent\Bus\BusServiceProvider'

运行: composer update

如何使用

创建你的命令和处理程序。

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

使用 trait 并调用 execute 方法。

class MyClass {

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

或者如果存在 GET 或 POST 数组中的 commander,则由 commander 处理输入;

class MyClass {

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

装饰器

你可以装饰命令对象(DTO)。

查看

  • 装饰器必须实现 Weloquent\Bus\Contracts\CommandHandler

  • 装饰器应返回命令对象。可以是原始的或修改后的。

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