websoftwares/middleware 问题修复 & 功能扩展

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

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

websoftwares/middleware

Composer 安装命令:

composer require websoftwares/middleware

包简介

This package lets u manage middleware for HTTP a request and response that implement the PSR-7 HTTP message interfaces.

README 文档

README

#Middleware (v0.0.*) This package lets u manage middleware for a HTTP request and response that implement the PSR-7 HTTP message interfaces Psr\Http\Message\ServerRequestInterface and Psr\Http\Message\ResponseInterface.

Build Status Scrutinizer Code Quality Code Coverage

Installing via Composer (recommended)

Install composer in your project:

curl -s http://getcomposer.org/installer | php

Create a composer.json file in your project root:

{
    "require": {
		"websoftwares/middleware": ~0.0.1"
    }
}

Install via composer

php composer.phar install

Usage

Basic usage of the MiddlewareRunner class.

use Websoftwares\Middleware\MiddlewareRunner;

$middleware = new MiddlewareRunner;

// Some middleware object that is callable through invoke or a closure 
// for consistency u could implement the `Websoftwares\MiddlewareInterface`.

// Invokable object
$throttleMiddleware = new ThrotteObject

// request + middelewareOne decoration <= objects are passed by reference
$middelewareOne = function($request, $response) {
    // Decorate the foo property
    $request->foo = $request->foo + 1;
};

// response + middlewareTwo decoration <= objects are passed by reference
$middlewareTwo = function($request, $response) {
    // / Decorate the bar property
    $response->bar = $response->bar . ' World';
};

$middleware->add($throttleMiddleware);
$middleware->add($middelewareOne);
$middleware->add($middlewareTwo);
...
// Add more middleware
...

$m = $middleware;

// Call
$m($request, $response);

Routing example with external package

Their are many excellent PHP router packages and in time some will be made compatible with PSR-7. In this basic example we will show u how to use the MiddlewareRunner class in conjunction with the latest development version of the Aura Router package.

use Websoftwares\Middleware\MiddlewareRunner;
use Aura\Router\RouterContainer;

$routerContainer = new RouterContainer;
$map = $routerContainer->getMap();
$matcher = $routerContainer->getMatcher();

$middleware = new MiddlewareRunner;

// response + middlewareOne decoration <= objects are passed by reference
$middlewareOne = function ($request, $response) {
    // / Decorate the bar property
    $response->bar = $response->bar.' World';
};

$routeIndexAction = function($request, $response) {
    // Awesome sauce
    return $response;
};

// Add middleware
$middleware->add($middlewareTwo);

...
// Add more middleware
...

// Add route as last one
$middleware->add($routeIndexAction);

$map->get('index.read', '/',$middleware); // <-- middleware becomes the handler

// We have a matching route
$route = $matcher->match($request);
$handler = $route->handler;

// Call
$handler($request, $response);

Adapters

At the time of writing PSR-7 is almost on the horizon released :-) and their are many well written community supported HTTP orientated packages but most packages are not yet compliant.

To avoid mass rewrites of all these great packages or waiting for the author and or community to update them or holding out on the advantage of new compliant packages we can make use of the Adapter pattern to make them for example suitable for PSR-7 middleware.

Adapter RequestAuthenticatorAdapter example

The package acquia/http-hmac-php is an implementation of the HTTP HMAC Spec in PHP We want to validate the signature throw an exception or continue the middleware stack if it is a valid signature.

use Websoftwares\Middleware\MiddlewareRunner;
use Acquia\Hmac\RequestSigner;
use Acquia\Hmac\RequestAuthenticator;
use Websoftwares\Middleware\Adapter\RequestAuthenticatorAdapter;

$middleware = new MiddlewareRunner;

// response + middlewareOne decoration <= objects are passed by reference
$middlewareOne = function ($request, $response) {
    // / Decorate the bar property
    $response->bar = $response->bar.' World';
};

// Add middleware
$middleware->add($middlewareOne);

...
// Add more middleware
...


$authenticator = new RequestAuthenticator(new RequestSigner(), '+15 minutes');

// $keyLoader implements \Acquia\Hmac\KeyLoaderInterface
$authenticatorMiddleware = new RequestAuthenticatorAdapter($authenticator, $keyLoader);

$middleware->add($authenticatorMiddleware);

// Call
$m = $middleware;

$m($request, $response);

Changelog

  • v0.0.11: Updated psr-7 psr/http-message to 1.0 and renamed phly/http with zendframework/zend-diactoros
  • v0.0.10: Logic to exit on response added
  • v0.0.9: Added abstract adapter and first implementation "acquia/http-hmac-php" package

Testing

In the tests folder u can find several tests.

Acknowledgement

Inspired by all the great middleware packages

License

The MIT License (MIT).

websoftwares/middleware 适用场景与选型建议

websoftwares/middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「response」 「middleware」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 websoftwares/middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 websoftwares/middleware 我们能提供哪些服务?
定制开发 / 二次开发

基于 websoftwares/middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-27