Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CommandBus

Minimalist implementation of CommandBus pattern for test purpose only

According to (it remembers remotelly to)

https://tactician.thephpleague.com/

Install

Insert official repository in your composer.json

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/proyectotau/command-bus.git"
        }
    ],

Require it by composer

   composer -vvv require proyectotau/command-bus:dev-master

Usage

Create a CommandHandler class

class CommandHandler {
	
	function handler($cmd){
		// run your task here
		// you can get public var from cmd if it is an object
	}
}

Make a CommandBus class

	$cmdbus = new CommandBus();

And bind command to that command handler

	$cmdbus->bind('MyCommand', $handler);

Command can be an object with parameters

	$cmdobj = new CommandObject(true, 1, []);
	$cmdbus->bind($cmdobj, $handler);

Finally, dispatch command

	$cmdbus->dispatch('MyCommand');

or

	$cmdbus->dispatch($cmdobj);

As a result, handler method will be invoke receiving command as an argument. If it is an object, you could get constructor's params. Let command be an object like this:

class CommandObject {
	public $param1;
	public $param2;
	public $param3;
	
	function __constructor($param1, $param2, $param3) {
		$this->param1 = $param1;
		$this->param2 = $param2;
		$this->param3 = $param3;
	}
}

You can pick up them

function handler($cmd){
		$this->param1 = $cmd->param1;
		$this->param2 = $cmd->param2;
		$this->param3 = $cmd->param3;
	}

Tests

You can run tests like this

vendor/bin/phpunit --color --testdox tests/CommandBusTest.php

About

Minimalist implementation of CommandBus pattern for test purpose only

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages