2017 © Pedro Peláez
 

yii2-extension yii2-command-bus

Yii2 Command Bus extension

image

trntv/yii2-command-bus

Yii2 Command Bus extension

  • Monday, April 23, 2018
  • by trntv
  • Repository
  • 11 Watchers
  • 48 Stars
  • 68,515 Installations
  • PHP
  • 9 Dependents
  • 1 Suggesters
  • 9 Forks
  • 3 Open issues
  • 16 Versions
  • 12 % Grown

The README.md

Yii2 Command Bus

Command Bus for Yii2, (*1)

Build Status, (*2)

What is Command Bus?

The idea of a command bus is that you create command objects that represent what you want your application to do. Then, you toss it into the bus and the bus makes sure that the command object gets to where it needs to go. So, the command goes in -> the bus hands it off to a handler -> and then the handler actually does the job. The command essentially represents a method call to your service layer., (*3)

Shawn McCool ©, (*4)

Installation

The preferred way to install this extension is through composer., (*5)

Either run, (*6)

php composer.phar require --prefer-dist trntv/yii2-command-bus

or add, (*7)

"trntv/yii2-command-bus": "^1.0"

to your composer.json file, (*8)

Setting Up

1. Command Bus Service

After the installation, first step is to set the command bus component., (*9)

return [
    // ...
    'components' => [
        'commandBus' => [
            'class' => 'trntv\bus\CommandBus'
        ]
    ],
];

2. Background commands support (optional)

Install required package:, (*10)

php composer.phar require symfony/process:^3.0

For the background commands worker, add a controller and command bus middleware in your config, (*11)

'controllerMap' => [
    'background-bus' => [
        'class' => 'trntv\bus\console\BackgroundBusController',
    ]
],

'components' => [
        'commandBus' =>[
            ...
            'middlewares' => [
                [
                    'class' => '\trntv\bus\middlewares\BackgroundCommandMiddleware',
                    'backgroundHandlerPath' => '@console/yii',
                    'backgroundHandlerRoute' => 'background-bus/handle',
                ]                
            ]
            ...            
        ]        
],

Create background command, (*12)

class ReportCommand extends Object implements BackgroundCommand, SelfHandlingCommand
{
    use BackgroundCommandTrait;

    public $someImportantData;

    public function handle($command) {
        // do what you need
    }
}

And run it asynchronously!, (*13)

Yii::$app->commandBus->handle(new ReportCommand([
    'async' => true,
    'someImportantData' => [ // data // ]
]))

3. Queued commands support (optional)

3.1 Install required package:

php composer.phar require yiisoft/yii2-queue

3.2 Configure extensions

If you need commands to be run in queue, you need to setup middleware and yii2-queue extensions., (*14)

'components' => [
    'queue' => [
        // queue config
    ],

    'commandBus' =>[
        ...
        'middlewares' => [
            [
                'class' => '\trntv\bus\middlewares\QueuedCommandMiddleware',
                // 'delay' => 3, // You can set default delay for all commands here
            ]                
        ]
        ...            
    ]     
]

More information about yii2-queue config can be found here, (*15)

3.4 Run queue worker

yii queue/listen More information here, (*16)

3.5 Create and run command

class HeavyComputationsCommand extends Object implements QueuedCommand, SelfHandlingCommand
{
    use QueuedCommandTrait;

    public function handle() {
        // do work here
    }
}

$command = new HeavyComputationsCommand();
Yii::$app->commandBus->handle($command)

4. Handlers

Handlers are objects that will handle command execution There are two possible ways to execute command:, (*17)

4.1 External handler

return [
    // ...
    'components' => [
        'commandBus' => [
            'class' => 'trntv\bus\CommandBus',
            'locator' => [
                'class' => 'trntv\bus\locators\ClassNameLocator',
                'handlers' => [
                    'app\commands\SomeCommand' => 'app\handlers\SomeHandler'
                ]
            ]
        ]
    ],
];

// or
$handler = new SomeHandler;
Yii::$app->commandBus->locator->addHandler($handler, 'app\commands\SomeCommand');
// or
Yii::$app->commandBus->locator->addHandler('app\handlers\SomeHandler', 'app\commands\SomeCommand');

4.1 Self-handling command

class SomeCommand implements SelfHandlingCommand
{
    public function handle($command) {
        // do what you need
    }
}

$command = Yii::$app->commandBus->handle($command);

The Versions

23/04 2018

dev-master

9999999-dev

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache extension command bus yii2 redis queue active-record session

23/04 2018

3.2.0

3.2.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

extension command bus yii2 queue

21/01 2018

3.1.0

3.1.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

extension command bus yii2 queue

27/08 2017

dev-yii2-queue-support

dev-yii2-queue-support

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

extension command bus yii2 queue

27/08 2017

3.0.0

3.0.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

extension command bus yii2 queue

20/01 2017

2.1.3

2.1.3.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

11/05 2016

2.1.0

2.1.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

23/04 2016

2.0.1

2.0.1.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

06/04 2016

2.0.0

2.0.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

05/04 2016

1.2.3

1.2.3.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

02/02 2016

1.2.2

1.2.2.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

28/01 2016

1.2.1

1.2.1.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

28/01 2016

1.2.0

1.2.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

28/01 2016

1.1.1

1.1.1.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

27/01 2016

1.1.0

1.1.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session

25/01 2016

1.0.0

1.0.0.0

Yii2 Command Bus extension

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache yii2 redis active-record session