PHPackages                             studioignis/cmd - 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. studioignis/cmd

ActiveLibrary

studioignis/cmd
===============

Add command support to your app. Inspired by laracasts/commander.

1.3.1(9y ago)2361MITPHPPHP &gt;=5.5.0

Since Sep 6Pushed 9y ago1 watchersCompare

[ Source](https://github.com/StudioIgnis/Cmd)[ Packagist](https://packagist.org/packages/studioignis/cmd)[ RSS](/packages/studioignis-cmd/feed)WikiDiscussions master Synced 1mo ago

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

[![Build Status](https://camo.githubusercontent.com/fccd33e5921cd9e953e030c7d8a2d2dbc526b66dbc6f74e3c71d103f4e44662f/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f53747564696f49676e69732f636d642e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/StudioIgnis/cmd)[![Packagist Version](https://camo.githubusercontent.com/8c66240b73fece1db9b733e6689cfc49ab6ed1bca35b921dea56bd52cf3e37f3/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73747564696f69676e69732f636d642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/studioignis/cmd)[![Packagist Downloads](https://camo.githubusercontent.com/54e5140592b7975f81c033a2d98c603cea7de21e074aaa0ee3d20ae4bafbf7a4/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73747564696f69676e69732f636d642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/studioignis/cmd)[![Packagist License](https://camo.githubusercontent.com/78a9e44e452e5f6214acc89afb37252069c43e75c2218ca000d040e5d2f7283d/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73747564696f69676e69732f636d642e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/78a9e44e452e5f6214acc89afb37252069c43e75c2218ca000d040e5d2f7283d/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73747564696f69676e69732f636d642e7376673f7374796c653d666c61742d737175617265)

StudioIgnis Cmd
===============

[](#studioignis-cmd)

Easily implement command based architecture in your project.

*Inspired by laracasts/commander*

Installation
------------

[](#installation)

Install through composer by adding the package to your composer.json

```
{
    "require": {
        "studioignis/cmd": "~1.0"
    }
}
```

Or using the command:

```
$ composer require studioignis/cmd:~1.0
```

Usage
-----

[](#usage)

It is really simple, you just need the command bus instance, then you can start executing commands! You can bootstrap the instance manually or use included Laravel service provider.

### Manual bootstrapping

[](#manual-bootstrapping)

You'll need an implementation of `\StudioIgnis\Cmd\Support\Container` and `\StudioIgnis\Cmd\NameInflector`.

You'll have to create your own container implementation, since this will depend on your framework. You can take a look at the included Laravel implementation to see how it can be implemented, it's extremely simple.

For the name inflector, on the other hand, you can use the one included `\StudioIgnis\Cmd\DefaultNameInflector`, or you can create your own.

```
$container = new StudioIgnis\Cmd\CommandBus(
    new Acme\Foo\Container($whatever),
    new StudioIgnis\Cmd\DefaultNameInflector
);
```

### Using Laravel

[](#using-laravel)

If you are using the Laravel framework, you can take advantage of the included service provider. You will need to add it to the `providers` array in your `app/config/app.php` file:

```
    'providers' => [
        // ...
        'StudioIgnis\Cmd\Laravel\ServiceProvider',
    ],
```

Now you can just use dependency injection to get the command bus, like so:

```
use StudioIgnis\Cmd\Bus;

class FooController
{
    public function __construct(Bus $commandBus)
    {
        // ...
    }
}
```

### Handlers

[](#handlers)

Handlers are as complex as you need them to be, they just need to implement the `StudioIgnis\Cmd\Handler` interface.

This interface defines only one method, `handle(Command $command)`, that as you can see, receives the corresponding command instance.

Let's see an (oversimplified) example:

```
namespace Acme\User\Handler;

use StudioIgnis\Cmd\Command;
use StudioIgnis\Cmd\Handler;
use Acme\User\Command\SignUp as SignUpCommand;

class SignUp implements Handler
{
    public function handle(UserRepository $users)
    {
        $this->users = $users;
    }

    public function handle(Command $command)
    {
        /** @var SignUpCommand $command */

        $this->users->add($command->name, $command->email, $command->password);
    }
}
```

#### Automatic handler resolving

[](#automatic-handler-resolving)

There's no need to register handlers for commands, as the default name inflector will take care of it as long as the namespaces are predictable.

The inflector takes the command class name and replaces all occurences of "Command" with "Handler", so for example `Acme\User\Command\CreateUser` will result in a handler `Acme\User\Handler\CreateUser`.

You don't have to follow this namespace convention, since all it does is word replacement, just be aware of what would the handler counterpart of a command will end up being.

#### Set handlers manually

[](#set-handlers-manually)

Another, less magical but sometimes preferred, way is by manually registering handlers per command:

```
$bus->setHandler(
    'Acme\User\Command\CreateUser',
    'Acme\User\Command\CreateUserHandler'
);
```

This way you don't have to predict where would a handler end up, you just tell the bus where to find it.

As you can see in the example above, we are passing the handler's FQN string. By doing this, the command bus will use the container to resolve the handler, thus, lazy-loading it. Another way to lazy-load a handler is by passing a closure that returns a handler instance. Or you can just pass an already instantiated handler.

### Commands

[](#commands)

Command classes are simple DTOs, but they must extend from the abstract `StudioIgnis\Cmd\Command`. This abstract base class ease things up a bit.

Your commands should define an array of attributes that you'll be able to get automatically, without defining getters.

This base command class also ensures the command can be converted to an array and serialized to json.

Here's an example:

```
namespace Acme\User\Command;

use StudioIgnis\cmd\Command;

class SignUp extends Command
{
    public function __construct($name, $email, $password)
    {
        $this->attributes = compact('name', 'email', 'password');
    }
}
```

Now you can access the attributes like this:

```
// One by one
$name = $signUpCommand->name;
$email = $signUpCommand->email;
$password = $signUpCommand->password;

// As an array
$signUpCommand->toArray();

// As a json string
$json = $signUpCommand->toJson();
$json = (string) $signUpCommand;
$json = json_encode($signUpCommand);
```

#### Executing commands

[](#executing-commands)

One you have your command bus instance ready and your handlers registered (or not!), executing commands is really easy. Let's use the command we defined above.

```
$command = new \Acme\User\Command\SignUp(
    'JohnDoe',
    'john.doe@example.com,
    'password!'
);

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

Traits
------

[](#traits)

There are two traits you can use to make command execution a little shorter. One is generic, the other one is exclusively for Laravel.

### StudioIgnis\\Cmd\\CmdTrait

[](#studioigniscmdcmdtrait)

This trait adds the `cmd($commandName, array $input)` mehtod. It's purpose is to initialize a command a little easier by passing the command class name and an array of inputs that will be used as the command parameters:

```
$input = [
    'name' => $this->input->get('name'),
    'email' => $this->input->get('email'),
    'password' => $this->input->get('password'),
];

$this->cmd('Acme\User\Command\SignUp', $input);
```

### StudioIgnis\\Cmd\\Laravel\\CmdTrait

[](#studioigniscmdlaravelcmdtrait)

This trait uses the previous one so you can just add this trait.
It adds three new methods: `execute($commandName, array $input = null)`, `getInput(array $input = null)` and `getCommandBus()`.

The idea is the same, but it also executes the command by calling the other trait's `cmd()` method.

`getInput(array $input = null)` will return Laravel's `Input::all()` if no `$input` is given.

`getCommandBus()` will return `App::make('StudioIgnis\Cmd\Bus')`;

Both of these methods can be overriden if you don't want to use the "facade" invocation.

License
-------

[](#license)

Copyright (c) 2014 Luciano Longo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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 ~143 days

Recently: every ~214 days

Total

7

Last Release

3412d ago

Major Versions

0.1.1 → 1.02014-09-07

PHP version history (2 changes)0.1PHP &gt;=5.4.0

1.0PHP &gt;=5.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/019c54e641913619a9ac66163a02515fa8187062c7128d877903cbc4acfd5391?d=identicon)[Cosmicist](/maintainers/Cosmicist)

---

Top Contributors

[![Cosmicist](https://avatars.githubusercontent.com/u/1039580?v=4)](https://github.com/Cosmicist "Cosmicist (29 commits)")

### Embed Badge

![Health badge](/badges/studioignis-cmd/health.svg)

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

###  Alternatives

[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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