tonis/dispatcher 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tonis/dispatcher

Composer 安装命令:

composer require tonis/dispatcher

包简介

Tonis\Dispatcher is a light-weight, HHVM compatible, and dependency free dispatching library.

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality

Installation

Tonis\Dispatcher can be installed using composer which will setup any autoloading for you.

composer require tonis/dispatcher

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

Dispatching

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

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 Tonis\Dispatcher\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

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

use Tonis\Dispatcher\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

use Tonis\Dispatcher\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

use Tonis\Dispatcher\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

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).

<?php
use Tonis\Dispatcher\Dispatcher;

$d = new Dispatcher();

// assume the TestDispatchable class from above
// instead of this
$d->add('foo', new TestDispatchable());

// you would do this
$d->add('foo', function() {
    return new TestDispatchable();
});

// the output is identical to the output from Dispatchable above
// the only difference is that it's lazy loaded on dispatch() instead.

What's up with ispatch() instead of dispatch()?

Because it's damn cute, that's why! If you prefer, though, you can use dispatch() instead as ispatch() is a simple proxy call to dispatch().

统计信息

  • 总下载量: 169
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-05-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固