All Projects → trntv → yii2-command-bus

trntv / yii2-command-bus

Licence: BSD-3-Clause license
Command Bus for Yii2

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-command-bus

yii2-linkable-behavior
Yii2 behavior to help creating urls easier
Stars: ✭ 12 (-78.57%)
Mutual labels:  yii2
yii2-league-oauth2-server
Yii 2.0 implementation of PHP league OAuth2 server interfaces
Stars: ✭ 29 (-48.21%)
Mutual labels:  yii2
yii2-sweet-submit
sweet sumit using sweetalert
Stars: ✭ 26 (-53.57%)
Mutual labels:  yii2
yii2-rollbar
Rollbar for Yii2
Stars: ✭ 36 (-35.71%)
Mutual labels:  yii2
service-skeleton
Microservice skeleton based on yii2 framework.
Stars: ✭ 14 (-75%)
Mutual labels:  yii2
cqrs
A foundational package for Command Query Responsibility Segregation (CQRS) compatible with Laravel.
Stars: ✭ 37 (-33.93%)
Mutual labels:  command-bus
leadshop
Leadshop是一款提供持续更新迭代服务的轻量级、高性能开源电商系统,前后端分离(uniapp +电商专用yii2.0),可视化DIY拖拽装修,旨在打造极致的用户体验! 支持微信小程序,公众号等
Stars: ✭ 168 (+200%)
Mutual labels:  yii2
yii2-elasticsearch
Elasticsearch client based on official Elasticsearch PHP library
Stars: ✭ 14 (-75%)
Mutual labels:  yii2
yii2-array-query
Yii2 component that allows for searching/filtering the elements of an array.
Stars: ✭ 34 (-39.29%)
Mutual labels:  yii2
yii2-datetime-widgets
Datetime widgets for Yii2
Stars: ✭ 22 (-60.71%)
Mutual labels:  yii2
php-framework-benchmark
php framework benchmark (include laravel、symfony、silex、lumen、slim、yii2、tastphp etc)
Stars: ✭ 17 (-69.64%)
Mutual labels:  yii2
examination
Yii2-basic 考试系统
Stars: ✭ 76 (+35.71%)
Mutual labels:  yii2
yii2-toastr
Yii2 - Javascript Toast Notifications
Stars: ✭ 25 (-55.36%)
Mutual labels:  yii2
yii2-content-tools
ContentTools editor implementation for Yii 2
Stars: ✭ 79 (+41.07%)
Mutual labels:  yii2
yii2-mariadb
MariaDB Driver for Yii2
Stars: ✭ 24 (-57.14%)
Mutual labels:  yii2
phd5-app
💜 Universal web application built upon Docker, PHP & Yii 2.0 Framework
Stars: ✭ 71 (+26.79%)
Mutual labels:  yii2
luya-bootstrap4
Bootstrap4 Assets and Helper classes like ActiveForm for LUYA and Yii2.
Stars: ✭ 18 (-67.86%)
Mutual labels:  yii2
yii2-ion-slider
Easily customizable range slider with skins support.
Stars: ✭ 21 (-62.5%)
Mutual labels:  yii2
background-translation-i18n
Based on the YII2 module to translate JSON formatted translation files on the web
Stars: ✭ 11 (-80.36%)
Mutual labels:  yii2
yii2-imagick
Class for working with Imagick
Stars: ✭ 17 (-69.64%)
Mutual labels:  yii2

Yii2 Command Bus

Command Bus for Yii2

Build Status

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.

Shawn McCool ©

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to your composer.json file

Setting Up

1. Command Bus Service

After the installation, first step is to set the command bus component.

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

2. Background commands support (optional)

Install required package:

php composer.phar require symfony/process:^3.0

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

'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

class ReportCommand extends Object implements BackgroundCommand, SelfHandlingCommand
{
    use BackgroundCommandTrait;
    
    public $someImportantData;
    
    public function handle($command) {
        // do what you need
    }
}

And run it asynchronously!

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.

'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

3.4 Run queue worker

yii queue/listen More information here

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:

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);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].