PHPackages                             robrogers3/commandbus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. robrogers3/commandbus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

robrogers3/commandbus
=====================

commandbus for php53

1.0.1(9y ago)117MITPHPPHP &gt;=5.3.3CI failing

Since Dec 21Pushed 6y ago1 watchersCompare

[ Source](https://github.com/robrogers3/commandbus)[ Packagist](https://packagist.org/packages/robrogers3/commandbus)[ Docs](https://github.com/robrogers3/commandbus)[ RSS](/packages/robrogers3-commandbus/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

CommandBus
==========

[](#commandbus)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c88a327e161542e2a2f7e513fc923730c029122da248fd85e230709e5c13af8f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f62726f67657273332f636f6d6d616e646275732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robrogers3/commandbus)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/9c14f2d534c16dbb4082e61e27f6af25cd6f691dbcca2f20f7b91d4f217be2fd/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f726f62726f67657273332f636f6d6d616e646275732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/robrogers3/commandbus)[![Total Downloads](https://camo.githubusercontent.com/a7ff167e84aaee1ec2ae873fe299821005fa06ab539c488d240d8ac32bb27fb3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f62726f67657273332f636f6d6d616e646275732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robrogers3/commandbus)

This a CommandBus implementation for php5.3. It's based on illuminate/events. This though will require a few other packages: illuminate/support, illuminate/contracts, and illuminate/container.

A CommandBus allows you to leverage commands and domain events in your php projects.

Essentially, the value add is to replace lines and lines of procedural style code in a class or method, with classes that do one thing. These classes are loosely coupled together through eventing initiated by the CommandBus and CommandHandlers.

The usage shown below is the best explanation.

Install
-------

[](#install)

Via Composer

```
$ composer require robrogers3/commandbus
```

Usage
-----

[](#usage)

### Requirements:

[](#requirements)

You have an app which uses illimunate packages. Especially illimunate/events.

### Your Goal:

[](#your-goal)

Have a way to accomplish some task which takes many subtasks. Perhaps currently you have the subtasks inside one class or even just one method. Some kind of God Object.

### The Solution:

[](#the-solution)

Use a command bus to kick off (dispatch) the necessary classes to complete accomplish what was done in one place.

### Initial Setup:

[](#initial-setup)

You need to create a boot method or function that initialize the illuminate Container (IOC). This method should:

- Register the container.
- Create an INSTANCE of the illimunate/event dispatcher.
- Register all the listeners for your events. \*\* The listeners can be fully written out, like 'Acme\\UserHasRegistered'. Or better, 'Acme.\*' The latter can catch all events prefixed by Acme

1. Define your listeners

```
    $listeners = array(
        'Acme\SendWelcomeEmail',
        'Acme\AddToLdap',
        'Acme\ConfigurePermissions'
    );
```

2. In your boot method, register them:

```
    App::instance('Dispatcher', $dispatcher);

    $listeners = getAppListeners();

    foreach ($listeners as $listener) {
        $dispatcher->listen('App.*', $listener);
    }
```

### Get to Work

[](#get-to-work)

1. Create your command. It's a simple class, a DTO of sorts. This is what gets thrown into the commandbus. Here is a simple one

```
namespace Acme;

class RegisterUserCommand
{
    public $username;

    public function __construct($username)
    {
        $this->username = $username;
    }
}
```

2. Next, lets create a listener, like:

```
namespace Acme;

use RobRogers\CommandBus\Eventing\EventListener;

class SendWelcomeEmail extends EventListener
{

    public function whenUserHasRegistered(UserWasRegistered $event)
    {
        dump( 'Sending mail to ' . $event->user->username);
    }
}
```

3. Create your command handler. The commandbus calls the handle method on this class.

```
namespace Acme;

use RobRogers\CommandBus\CommandHandler;

class UserRegisterCommandHandler implements CommandHandler
{
    /**
     * @var EventDispatcher
     */
    private $dispatcher;
    /**a
     * @var EventGenerator
     */
    private $eventGenerator;

    /**
     * @param EventDispatcher $dispatcher
     * @param EventGenerator $eventGenerator
     */
    public function __construct(EventDispatcher $dispatcher, EventGenerator $eventGenerator)
    {
        $this->dispatcher = $dispatcher;

        $this->eventGenerator = $eventGenerator;
    }

    public function handle(/* user registered command */ $command)
    {
        $event = new Acme\UserHasRegistered($command);
        $this->eventGenerator->register($event); //you can register many events
    }
}
```

4. Finally create the 'event' we are listening for. This also a DTO, which contains the above UserRegister $command.

```
namespace Acme;

/**
* I am  THE EVENT
* the command data get's shoved inside me. like $this->user->username
*/
class UserHasRegisteredEvent
{
    public $user;

    /** @var UserRegister $user */
    public function __construct($user)
    {
        $this->user = $user;
    }
}
```

5. Use it:

```
$command = new RegisterUserCommand("Rob Rogers");

/** @var \RobRogers\CommandBus\BaseCommandBus $commandBus */
$commandBus = App::Make('\RobRogers\CommandBus\BaseCommandBus');

$commandBus->execute($command);
```

6. See what happens:

If you have registered the three listeners above, they all get fired.

- send a welcome email to the user.
- add them to ldap (or wherever)
- configure their system permissions

Bottom line in your controller, or wherever. You just need 3 lines:

- instantiate the command.
- make the command bus.
- call the execute method.

For the three tasks above, it's not a big deal to call them directly. But if you have many many things to do, then it cleans things up rather nicely.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Rob Rogers](https://github.com/robrogers3)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

3429d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ed5d501555b869b634cf8ae93fef690391122bd9e3c2624dafd095cc907cf90a?d=identicon)[robrogers3](/maintainers/robrogers3)

---

Top Contributors

[![robrogers3](https://avatars.githubusercontent.com/u/2775002?v=4)](https://github.com/robrogers3 "robrogers3 (9 commits)")

---

Tags

robrogers3agnostic-commandbus

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/robrogers3-commandbus/health.svg)

```
[![Health](https://phpackages.com/badges/robrogers3-commandbus/health.svg)](https://phpackages.com/packages/robrogers3-commandbus)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
