承接 vaibhavpandeyvpz/vidyut 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

vaibhavpandeyvpz/vidyut

Composer 安装命令:

composer require vaibhavpandeyvpz/vidyut

包简介

No frills PSR-7 request handler based on PSR-15 specification.

README 文档

README

Latest Version Downloads PHP Version License Build Status

No frills PSR-7 request handler based on PSR-15 specification.

Vidyut (विद्युत्) - Sanskrit for "Electricity", representing the flow of requests through the middleware pipeline.

Features

  • PSR-15 Compliant - Full implementation of the PSR-15 middleware specification
  • PSR-7 Compatible - Works with any PSR-7 HTTP message implementation
  • Flexible Middleware - Supports both MiddlewareInterface instances and callables
  • Fluent Interface - Chain middleware using the pipe() method
  • Zero Dependencies - Only requires PSR interfaces (PSR-7, PSR-15)
  • Modern PHP - Built for PHP 8.2+ with modern language features
  • 100% Test Coverage - Fully tested with comprehensive test suite

Installation

Install via Composer:

composer require vaibhavpandeyvpz/vidyut

Note: You'll also need a PSR-7 implementation. We recommend sandesh:

composer require vaibhavpandeyvpz/sandesh

Quick Start

<?php

use Vidyut\Pipeline;
use Sandesh\ServerRequestFactory;
use Sandesh\ResponseFactory;

// Create a new pipeline
$pipeline = new Pipeline();

// Add middleware using callables
$pipeline->pipe(function ($request, $delegate) {
    if ($request->getUri()->getPath() === '/login') {
        $response = (new ResponseFactory())->createResponse();
        $response->getBody()->write('Login Page');
        return $response;
    }
    return $delegate->handle($request);
});

// Add a final handler (404)
$pipeline->pipe(function () {
    $response = (new ResponseFactory())->createResponse();
    $response->getBody()->write('Page not found');
    return $response->withStatus(404);
});

// Handle a request
$request = (new ServerRequestFactory())
    ->createServerRequest($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER);
$response = $pipeline->handle($request);

Usage

Basic Pipeline

The Pipeline class implements the PSR-15 RequestHandlerInterface and allows you to chain middleware:

use Vidyut\Pipeline;

$pipeline = new Pipeline();

// Middleware can be callables
$pipeline->pipe(function ($request, $delegate) {
    // Process request
    $response = $delegate->handle($request);
    // Modify response
    return $response->withHeader('X-Custom', 'value');
});

// Or MiddlewareInterface instances
$pipeline->pipe(new MyMiddleware());

// Handle requests
$response = $pipeline->handle($request);

Constructor Injection

You can also pass middleware directly to the constructor:

$pipeline = new Pipeline([
    function ($request, $delegate) {
        // First middleware
        return $delegate->handle($request);
    },
    function ($request, $delegate) {
        // Second middleware
        return $delegate->handle($request);
    },
    function () {
        // Final handler
        return (new ResponseFactory())->createResponse(200);
    },
]);

Middleware Types

Callable Middleware

Callables must accept two parameters:

  • ServerRequestInterface $request
  • RequestHandlerInterface $delegate

And return a ResponseInterface:

$pipeline->pipe(function (ServerRequestInterface $request, RequestHandlerInterface $delegate): ResponseInterface {
    // Your middleware logic
    return $delegate->handle($request);
});

MiddlewareInterface Instances

You can use any class implementing Psr\Http\Server\MiddlewareInterface:

class AuthenticationMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // Check authentication
        if (!$this->isAuthenticated($request)) {
            return (new ResponseFactory())->createResponse(401);
        }
        return $handler->handle($request);
    }
}

$pipeline->pipe(new AuthenticationMiddleware());

Array Callables

You can also use array callables:

$middleware = new class {
    public function handle(ServerRequestInterface $request, RequestHandlerInterface $delegate): ResponseInterface
    {
        return $delegate->handle($request);
    }
};

$pipeline->pipe([$middleware, 'handle']);

Request Modification

Middleware can modify the request before passing it to the next handler:

$pipeline->pipe(function ($request, $delegate) {
    // Add custom attribute
    $modifiedRequest = $request->withAttribute('user', $this->getUser($request));
    return $delegate->handle($modifiedRequest);
});

Response Modification

Middleware can modify the response after receiving it from the next handler:

$pipeline->pipe(function ($request, $delegate) {
    $response = $delegate->handle($request);
    // Add custom header
    return $response->withHeader('X-Processed-By', 'MyMiddleware');
});

Early Termination

Middleware can stop the pipeline by returning a response without calling the delegate:

$pipeline->pipe(function () {
    // Return immediately, next middleware won't be called
    return (new ResponseFactory())->createResponse(403, 'Forbidden');
});

Fluent Interface

The pipe() method returns the pipeline instance, allowing method chaining:

$pipeline = new Pipeline()
    ->pipe($middleware1)
    ->pipe($middleware2)
    ->pipe($middleware3);

Requirements

  • PHP 8.2 or higher
  • A PSR-7 implementation (e.g., sandesh)

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues, questions, or contributions, please visit the GitHub repository.

vaibhavpandeyvpz/vidyut 适用场景与选型建议

vaibhavpandeyvpz/vidyut 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 597 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-17