The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Plugins

Introduction

Author Source Code Travis CI Scrutinizer Code Quality Build Status MIT License SensioLabsInsight

What is Tactician?

Tactician is a command bus library. It tries to make using the command pattern in your application easy and flexible.

You can use Tactician for all types of command inputs but it especially targets service layers.

What is a Command Bus?

The term is mostly used when we combine the Command pattern with a service layer. Its job is to take a Command object (which describes what the user wants to do) and match it to a Handler (which executes it). This can help structure your code neatly.

In practice, it looks like this:

// You build a simple message object like this:
class PurchaseProductCommand
{
    protected $productId;

    protected $userId;
    
    // ...and constructor to assign those properties...
}

// And a Handler class that expects it:
class PurchaseProductHandler
{
    public function handle(PurchaseProductCommand $command)
    {
        // use command to update your models, etc
    }
}

// And then in your controllers, you can fill in the command using your favorite
// form or serializer library, then drop it in the CommandBus and you're done!
$command = new PurchaseProductCommand(42, 29);
$commandBus->handle($command);

That’s it. Tactician is the $commandBus part, doing all the plumbing of finding the handler and calling the right method. You know, the boring stuff. Commands can be any plain old PHP object and Handlers are usually other objects, but can be anything you’d like to configure.

One of the cool things about Tactician (and command buses in general) is that they’re really easy to extend with new features by adding middleware. Tactician aims to provide plugin packages that cover common tasks, like logging and database transactions. That way you don’t have to put it in every handler and it immediately applies to your entire application.

When should I use it?

Tactician is a great fit if you’ve got a service layer. If you’re not sure what a service layer is, Martin Fowler’s PoEAA is a good starting point. Tactician’s author also did a talk on the subject.

Commands really help capture user intent. They’re also a great stand-in for the models when it comes to forms or serializer libraries that expect getter/setter objects.

The command bus itself is really easy to decorate with extra behaviors, like locking or database transactions so it’s very easy to extend with plugins.

Finally, if you’re writing a standalone library that uses the command pattern internally, Tactician is really easy to tweak and can save you the hassle of building it yourself.

If any of that sounds familiar and helpful, Tactician might be right for you! :)

When should I not use it?

If you’ve got a very small app that doesn’t need a service layer, then Tactician won’t offer much to you.

If you’re already using a tool that provides a command bus (like Broadway), you’re probably okay there too.

Questions?

Tactician was created by Ross Tuck. Find him on Twitter at @rosstuck.