PHPackages                             spiffy/spiffy-dispatch - 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. spiffy/spiffy-dispatch

AbandonedLibrary

spiffy/spiffy-dispatch
======================

Spiffy\\Dispatch is a light-weight, HHVM compatible, and dependency free dispatching library.

1.0.0-alpha(11y ago)15861BSD-3-ClausePHPPHP &gt;=5.4

Since Jul 15Pushed 11y ago2 watchersCompare

[ Source](https://github.com/spiffyjr/spiffy-dispatch)[ Packagist](https://packagist.org/packages/spiffy/spiffy-dispatch)[ RSS](/packages/spiffy-spiffy-dispatch/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (2)Used By (1)

Spiffy\\Dispatch
================

[](#spiffydispatch)

[![Build Status](https://camo.githubusercontent.com/189f8afd20fd9097b0ebe5b670b47946943b0cf9b1196bfa01d5b7366cb55235/68747470733a2f2f7472617669732d63692e6f72672f7370696666796a722f7370696666792d64697370617463682e737667)](https://travis-ci.org/spiffyjr/spiffy-dispatch)[![Code Coverage](https://camo.githubusercontent.com/1a7d300c04c4e2733dbe723b8c63056603c1f100b57536b1072172abec35a780/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7370696666796a722f7370696666792d64697370617463682f6261646765732f636f7665726167652e706e673f733d33663630366632366632353539376537653431623336613335663233383130373534663865333464)](https://scrutinizer-ci.com/g/spiffyjr/spiffy-dispatch/)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/09a3de3527ebc3b17b9ded630a0412447b4acc9f1b853a9e80c89b2ecdee1238/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7370696666796a722f7370696666792d64697370617463682f6261646765732f7175616c6974792d73636f72652e706e673f733d66313263366166376666633961326436646136646465633332633239353366333638356337666337)](https://scrutinizer-ci.com/g/spiffyjr/spiffy-dispatch/)

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

[](#installation)

Spiffy\\Dispatch can be installed using composer which will setup any autoloading for you.

`composer require spiffy/spiffy-dispatch`

Additionally, you can download or clone the repository and setup your own autoloading.

Dispatching
-----------

[](#dispatching)

Dispatching can be done via the `Dispatchable` interface, a `Closure`, `callable`, or `invokable`.

### Dispatchable

[](#dispatchable)

The `Dispatchable` interface has a single `dispatch` method that accepts an array of parameters. There is no reflection involved in dispatching this way so it is the most performant.

```
use Spiffy\Dispatch\Dispatcher;

class TestDispatchable implements Dispatchable
{
    public function dispatch(array $params) { return 'foo'; }
}

$d = new Dispatcher();
$d->add('foo', new TestDispatchable());

// outputs 'foo'
echo $d->ispatch('foo');

// the second argument will be passed to the $params array of the dispatch() method
echo $d->ispatch('foo', ['name' => 'bar']);
```

### Closure

[](#closure)

Dispatching via a closure is useful for micro-frameworks or for quickly setting up prototypes.

```
use Spiffy\Dispatch\Dispatcher();

$d = new Dispatcher();
$d->add('foo', function() { return 'foo'; });

// outputs 'foo'
echo $d->ispatch('foo');

// this closure has a default value of 'baz' for the $slug parameter
$d->add('bar', function($id, $slug = 'baz') {
    return $id . ': ' . $slug;
)};

// this uses the default value for $slug
// outputs '1: baz'
echo $d->ispatch('bar', ['id' => 1]);

// this overwrites the default value
// outputs '1: booze'
echo $d->ispatch('bar', ['id' => 1, 'slug' => 'booze']);
```

### Invokable

[](#invokable)

```
use Spiffy\Dispatch\Dispatcher;

class TestInvokable implements Dispatchable
{
    public function __invoke($id, $slug = 'baz')
    {
        return $id . ': ' . $slug;
    }
}

$d = new Dispatcher();
$d->add('foo', new TestInvokable());

// this uses the default value for $slug
// outputs '1: baz'
echo $d->ispatch('bar', ['id' => 1]);

// this overwrites the default value
// outputs '1: booze'
echo $d->ispatch('bar', ['id' => 1, 'slug' => 'booze']);
```

### Callable

[](#callable)

```
use Spiffy\Dispatch\Dispatcher;

class TestCallable implements Dispatchable
{
    public function outputId($id)
    {
        return $id;
    }

    public static function outputStaticId($id)
    {
        return $id;
    }
}

$test = new TestCallable();

$d = new Dispatcher();
$d->add('foo', [$test, 'outputId']);

// output is '1'
$d->ispatch('foo', ['id' => 1]);

$d->add('bar', 'TestCallable::outputStaticId');

// output is '2'
$d->ispatch('bar', ['id' => 2]);
```

Lazy-loading and recursion
--------------------------

[](#lazy-loading-and-recursion)

The dispatcher will continue to dispatch as long as a dispatchable is returned. This allows you to lazy-load objects by using a closure (which is dispatchable).

```
