{"id":21838269,"url":"https://github.com/kodcube/command-bus","last_synced_at":"2025-06-15T10:08:02.561Z","repository":{"id":57007963,"uuid":"63517474","full_name":"kodcube/command-bus","owner":"kodcube","description":"Command Bus - Middleware Command Bus","archived":false,"fork":false,"pushed_at":"2016-07-17T06:24:41.000Z","size":8,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-03T18:59:45.137Z","etag":null,"topics":["command-bus","middleware","php7"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kodcube.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-17T06:09:04.000Z","updated_at":"2022-02-12T14:02:12.000Z","dependencies_parsed_at":"2022-08-21T14:31:16.644Z","dependency_job_id":null,"html_url":"https://github.com/kodcube/command-bus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodcube%2Fcommand-bus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodcube%2Fcommand-bus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodcube%2Fcommand-bus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodcube%2Fcommand-bus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kodcube","download_url":"https://codeload.github.com/kodcube/command-bus/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodcube%2Fcommand-bus/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259003233,"owners_count":22791031,"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","middleware","php7"],"created_at":"2024-11-27T21:09:42.221Z","updated_at":"2025-06-15T10:08:02.530Z","avatar_url":"https://github.com/kodcube.png","language":"PHP","readme":"# Middleware Based Command Bus\n\nA simple command bus that uses the middleware pattern for processing commands.\n\nIt has been designed to leaverage Dependency Injection with Autowiring support. All of it's dependencies and be configured \u0026 overridden by a dependency injection container\n\n### Main Features \n* All Dependancies are injected at construction\n* All Dependencies are based on interfaces\n* Route Commands differently based on custom middleware\n* Use simple arrays for middleware configuration\n* Uses Dependency Injection Container when loading middleware\n* Uses Dependency Injection Container when loading handlers\n\n### Limitations\n* Does not inject dependencies for methods other than __constructor \n* Does not inject dependencies for setters\n\n### Requirements\n* PHP 7\n* Container-Interop - Dependancy Injection Container\n* GunnaPHP\\Invoker\n\n### Optional\n* KodCube\\DependencyInjection - Dependancy Injection Container\n* KodCube\\MessageBus - Dispatching commands to background processes\n\n\n## Usage\n\n* [Create Command Bus](#create-command-bus)\n* [Dispatch Command](#dispatch-command)\n* [Override Command Handler](#override-command-handler)\n* [Multiple Handlers](#multiple-handlers)\n\n\n\n### Create Command Bus\n\nThis will create a command bus with the default handler detection\n\n``` PHP\n$commandbus = new KodCube\\CommandBus\\CommandBus(\n\t\t\t        new KodCube\\CommandBus\\MiddlewareManager(\n\t\t\t        \tnew KodCube\\DependencyInjection\\Container()\n\t\t\t        )\n              );\n``` \n\nor using dependancy injection container\n\n``` PHP\nDI Config\n[\n\tMiddlewareManagerInterface::class =\u003e MiddlewareManager::class,\n\tContainerInterface::class =\u003e Container::class\n]\n\n$commandbus = $di-\u003eget(KodCube\\CommandBus\\CommandBus::class);\n``` \n\n\n### Dispatch Command\n\nAssuming you have the following command and handler\n\nCommand Class: MyCommand \n\nHandler Class: MyHandler\n\n\n``` PHP\n$command = new MyCommand();\n\n$response = $commandbus($command);\nor \n$response = $commandbus-\u003ehandle($command);\n```\nis the equivalant to \n\n``` PHP\n$command = new MyCommand();\n$handler = new MyHandler();\n\n$response = $handler($command);\n```\n### Override Command Handler\n\nThe default handler middleware allows you to override which handler is responsible for handling the command\n\n``` PHP\n$commandbus = new KodCube\\CommandBus\\CommandBus(\n\t\t\t        new KodCube\\CommandBus\\MiddlewareManager(\n\t\t\t        \tnew KodCube\\DependencyInjection\\Container(),\n\t\t\t        \tnew KodCube\\CommandBus\\Middleware\\Handler(\n\t\t\t        \t\t[\n\t\t\t        \t\t\tMyCommand::class =\u003e MyNewHandler::class\n\t\t\t        \t\t]\n\t\t\t        \t)\n\t\t\t        )\n              );\n``` \nis the equivalant to \n\n``` PHP\n$command = new MyCommand();\n$handler = new MyNewHandler();\n\n$response = $handler($command);\n```\n\nor using dependancy injection container\n\n``` PHP\nDI Config\n[\n\tMiddlewareManagerInterface::class =\u003e MiddlewareManager::class,\n\tContainerInterface::class =\u003e Container::class\n\tHandler::class =\u003e [\n\t\t[\n\t\t\tMyCommand::class =\u003e MyNewHandler::class\n\t\t]\n\t]\n]\n\n$commandbus = $di-\u003eget(KodCube\\CommandBus\\CommandBus::class);\n``` \n\n### Multiple Middlerware Handlers\n\nUsing the middleware capablies we can easily add multiple command handlers, to process some commands differently from others.\n\nIn this example we add a Queueable Middleware Handler, that will look for commands that implement a *CommandQueueInterface::class*.\n\nTheses commands will be routed to a message bus for processing on a background process, were as all other command will be handled by the final middleware handler.\n\n``` PHP\n$commandbus = new KodCube\\CommandBus\\CommandBus(\n                new KodCube\\CommandBus\\MiddlewareManager(\n                    [\n                      KodCube\\CommandBus\\Middleware\\Queue::class,\n                      KodCube\\CommandBus\\Middleware\\Handler::class\n                    ],\n                    new KodCube\\DependencyInjection\\Container(), \n\t\t\t      )\n              );\n``` \nor using dependancy injection container\n\n``` PHP\nDI Config\n[\n\tMiddlewareManagerInterface::class =\u003e MiddlewareManager::class,\n\tMiddlewareManager::class =\u003e [\n\t\tMiddleware\\Queue::class,\n\t\tMiddleware\\Handler::class\n\t],\n\tContainerInterface::class =\u003e Container::class,\n\tHandler::class =\u003e [\n\t\t[\n\t\t\tMyCommand::class =\u003e MyNewHandler::class\n\t\t]\n\t]\n]\n\n$commandbus = $di-\u003eget(KodCube\\CommandBus\\CommandBus::class);\n``` \n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodcube%2Fcommand-bus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkodcube%2Fcommand-bus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodcube%2Fcommand-bus/lists"}