binsoul/net-http-dispatcher

使用命名参数调用可调用对象或对象方法的分发器

dev-master / 1.0.x-dev 2016-04-27 17:32 UTC

This package is auto-updated.

Last update: 2024-09-14 22:39:46 UTC


README

Latest Version on Packagist Software License Total Downloads

此包允许使用上下文中提供的命名参数将任意名称映射到可分派对象。然后调用可分派对象以返回响应。它们可以由中间件包围以修改传入的请求或输出的响应。

安装

通过 composer

$ composer require binsoul/net-http-dispatcher

响应者

响应者可以是一个闭包,一个具有 __invoke 方法的对象,或一个具有多个方法的对象。它可以接收请求对象、上下文对象或上下文中的任何值作为参数,并必须返回一个响应。

分发器通过反射解析参数。如果无法解析参数且不是可选的,将抛出异常。

例如,响应者 "blog.edit" 将接收以下参数

$dispatcher->addResponder(
    'blog.edit',
    function ($id, RequestInterface $request, Context $context, $optional = 'foo')
    {
        // $id = 1
        // $request = object
        // $context = object
        // $optional = 'foo'
        
        return new Response(...);
    }
);

$dispatcher->handle($request, ['responder' => 'blog.edit', 'id' => 1]);

中间件

中间件可以是一个闭包或一个具有 __invoke 方法的对象。它必须接受一个请求、一个上下文和下一个中间件作为参数,并必须返回一个响应。

例如,中间件可能会执行或不执行各种可选过程

$middleware = function (RequestInterface $request, Context $context, callable $next) {
    // optionally return a response early
    if (...) {
        return new Response(...);
    }

    // optionally modify the request
    $request = $request->withUri(..);

    // optionally modify the context
    $context = $context->withParameter(...);

    // invoke the $next middleware
    $response = $next($request, $context);

    // optionally modify the response
    $response = $response->withStatus(...);

    // always return a response
    return $response;
};

分发器

分发器使用上下文数组提供的信息来查找响应者。默认键是 "responder",表示应调用哪个响应者,以及 "method",表示应调用响应者的哪个方法。

可以配置使用的数组键。以下示例将响应者映射到数组键 "controller",将方法映射到 "action"。

class BlogController
{
    public function index()
    {
        return new Response(...);
    }
}

$dispatcher->addResponder('blog', new BlogController());

$dispatcher->defineParameters('controller', 'action');

// will invoke BlogController->index()
$dispatcher->handle($request, ['controller' => 'blog', 'action' => 'index']); 

响应者可以注册为闭包、具体对象或字符串。如果将响应者注册为字符串,则使用提供的工厂以懒加载方式构建响应者。

$dispatcher->addResponder('blog', 'BlogResponder');
$dispatcher->setFactory($factory);

// will call $factory->buildResponder('BlogResponder');
$dispatcher->handle($request, ['responder' => 'blog']); 

注册中间件也适用相同的规则。

$dispatcher->addMiddleware('AuthMiddleware')
$dispatcher->setFactory($factory);

// will call $factory->buildMiddleware('AuthMiddleware');
$dispatcher->handle($request, ['responder' => 'blog']); 

中间件被添加到围绕最终调用响应者的内部队列中。

例如,如果队列构建如下

$dispatcher->addResponder('blog', 'BlogResponder');

$dispatcher->addMiddleware('Foo');
$dispatcher->addMiddleware('Bar');
$dispatcher->addMiddleware('Baz');

$dispatcher->handle($request, ['controller' => 'blog']); 

请求和响应将通过中间件路径如下所示

Foo is 1st on the way in
    Bar is 2nd on the way in
        Baz is 3rd on the way in
            BlogResponder is invoked        
        Baz is 1st on the way out
    Bar is 2nd on the way out
Foo is 3rd on the way out

测试

$ composer test

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件