{"id":16771517,"url":"https://github.com/trntv/yii2-command-bus","last_synced_at":"2025-05-08T20:57:57.767Z","repository":{"id":57072896,"uuid":"50032833","full_name":"trntv/yii2-command-bus","owner":"trntv","description":"Command Bus for Yii2","archived":false,"fork":false,"pushed_at":"2018-04-23T12:31:56.000Z","size":61,"stargazers_count":57,"open_issues_count":4,"forks_count":18,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-05-08T20:57:48.083Z","etag":null,"topics":["command-bus","yii2"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trntv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-20T13:46:43.000Z","updated_at":"2025-03-15T20:58:14.000Z","dependencies_parsed_at":"2022-08-24T14:54:37.245Z","dependency_job_id":null,"html_url":"https://github.com/trntv/yii2-command-bus","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trntv%2Fyii2-command-bus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trntv%2Fyii2-command-bus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trntv%2Fyii2-command-bus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trntv%2Fyii2-command-bus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trntv","download_url":"https://codeload.github.com/trntv/yii2-command-bus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253149568,"owners_count":21861719,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command-bus","yii2"],"created_at":"2024-10-13T06:28:20.520Z","updated_at":"2025-05-08T20:57:57.749Z","avatar_url":"https://github.com/trntv.png","language":"PHP","readme":"# Yii2 Command Bus\n\nCommand Bus for Yii2\n\n[![Build Status](https://travis-ci.org/trntv/yii2-command-bus.svg?branch=master)](https://travis-ci.org/trntv/yii2-command-bus)\n\n\n## What is Command Bus? \n\u003e The idea of a command bus is that you create command objects that represent what you want your application to do. \n\u003e Then, you toss it into the bus and the bus makes sure that the command object gets to where it needs to go.\n\u003e So, the command goes in -\u003e the bus hands it off to a handler -\u003e and then the handler actually does the job. The command essentially represents a method call to your service layer.\n\n[Shawn McCool ©](http://shawnmc.cool/command-bus)\n\n## Installation\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist trntv/yii2-command-bus\n```\n\nor add\n```\n\"trntv/yii2-command-bus\": \"^1.0\"\n```\nto your composer.json file\n\n## Setting Up\n\n### 1. Command Bus Service\nAfter the installation, first step is to set the command bus component.\n\n```php\nreturn [\n    // ...\n    'components' =\u003e [\n        'commandBus' =\u003e [\n            'class' =\u003e 'trntv\\bus\\CommandBus'\n        ]\n    ],\n];\n```\n\n### 2. Background commands support (optional)\nInstall required package:\n```\nphp composer.phar require symfony/process:^3.0\n```\n\nFor the background commands worker, add a controller and command bus middleware in your config\n\n```php\n'controllerMap' =\u003e [\n    'background-bus' =\u003e [\n        'class' =\u003e 'trntv\\bus\\console\\BackgroundBusController',\n    ]\n],\n\n'components' =\u003e [\n        'commandBus' =\u003e[\n            ...\n            'middlewares' =\u003e [\n                [\n                    'class' =\u003e '\\trntv\\bus\\middlewares\\BackgroundCommandMiddleware',\n                    'backgroundHandlerPath' =\u003e '@console/yii',\n                    'backgroundHandlerRoute' =\u003e 'background-bus/handle',\n                ]                \n            ]\n            ...            \n        ]        \n],\n```\n\nCreate background command\n```php\nclass ReportCommand extends Object implements BackgroundCommand, SelfHandlingCommand\n{\n    use BackgroundCommandTrait;\n    \n    public $someImportantData;\n    \n    public function handle($command) {\n        // do what you need\n    }\n}\n```\nAnd run it asynchronously!\n```php\nYii::$app-\u003ecommandBus-\u003ehandle(new ReportCommand([\n    'async' =\u003e true,\n    'someImportantData' =\u003e [ // data // ]\n]))\n```\n\n### 3. Queued commands support (optional)\n#### 3.1 Install required package:\n\n```\nphp composer.phar require yiisoft/yii2-queue\n```\n#### 3.2 Configure extensions\nIf you need commands to be run in queue, you need to setup middleware and yii2-queue extensions.\n\n```php\n'components' =\u003e [\n    'queue' =\u003e [\n        // queue config\n    ],\n    \n    'commandBus' =\u003e[\n        ...\n        'middlewares' =\u003e [\n            [\n                'class' =\u003e '\\trntv\\bus\\middlewares\\QueuedCommandMiddleware',\n                // 'delay' =\u003e 3, // You can set default delay for all commands here\n            ]                \n        ]\n        ...            \n    ]     \n]\n```\nMore information about yii2-queue config can be found [here](https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/usage.md)\n#### 3.4 Run queue worker \n``yii queue/listen``\nMore information [here](https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#worker-starting-control) \n#### 3.5 Create and run command\n```php\nclass HeavyComputationsCommand extends Object implements QueuedCommand, SelfHandlingCommand\n{\n    use QueuedCommandTrait;\n    \n    public function handle() {\n        // do work here\n    }\n}\n\n$command = new HeavyComputationsCommand();\nYii::$app-\u003ecommandBus-\u003ehandle($command)\n```\n\n### 4. Handlers\nHandlers are objects that will handle command execution\nThere are two possible ways to execute command:\n#### 4.1 External handler \n```php\nreturn [\n    // ...\n    'components' =\u003e [\n        'commandBus' =\u003e [\n            'class' =\u003e 'trntv\\bus\\CommandBus',\n            'locator' =\u003e [\n                'class' =\u003e 'trntv\\bus\\locators\\ClassNameLocator',\n                'handlers' =\u003e [\n                    'app\\commands\\SomeCommand' =\u003e 'app\\handlers\\SomeHandler'\n                ]\n            ]\n        ]\n    ],\n];\n\n// or\n$handler = new SomeHandler;\nYii::$app-\u003ecommandBus-\u003elocator-\u003eaddHandler($handler, 'app\\commands\\SomeCommand');\n// or\nYii::$app-\u003ecommandBus-\u003elocator-\u003eaddHandler('app\\handlers\\SomeHandler', 'app\\commands\\SomeCommand');\n```\n#### 4.1 Self-handling command\n```php\nclass SomeCommand implements SelfHandlingCommand\n{\n    public function handle($command) {\n        // do what you need\n    }\n}\n\n$command = Yii::$app-\u003ecommandBus-\u003ehandle($command);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrntv%2Fyii2-command-bus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrntv%2Fyii2-command-bus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrntv%2Fyii2-command-bus/lists"}