Skip to content

httpsoft/http-runner

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

HTTP Runner

License Latest Stable Version Total Downloads GitHub Build Status GitHub Static Analysis Status Scrutinizer Code Coverage Scrutinizer Code Quality

This package is responsible for running PSR-7 components and building PSR-15 middleware pipelines.

Documentation

Installation

This package requires PHP version 7.4 or later.

composer require httpsoft/http-runner

Usage

use HttpSoft\Runner\MiddlewarePipeline;
use HttpSoft\Runner\MiddlewareResolver;
use HttpSoft\Runner\ServerRequestRunner;
use Psr\Http\Message\ResponseInterface;

/**
 * @var Psr\Http\Message\ServerRequestInterface $request
 * @var Psr\Http\Server\RequestHandlerInterface $handler
 * @var Psr\Http\Server\MiddlewareInterface $siteMiddleware
 * @var Psr\Http\Server\MiddlewareInterface $userMiddleware
 */

// Basic usage

$runner = new ServerRequestRunner();
$runner->run($request, $handler);

// Using `MiddlewarePipeline`

$pipeline = new MiddlewarePipeline();
$pipeline->pipe($siteMiddleware);
$pipeline->pipe($userMiddleware, '/user');

$runner = new ServerRequestRunner($pipeline);
$runner->run($request, $handler);

// Using `MiddlewareResolver`

$resolver = new MiddlewareResolver();
$handlerMiddleware = $resolver->resolve(function (): ResponseInterface {
    $response = new HttpSoft\Message\Response();
    $response->getBody()->write('Hello world!');
    return $response;
});

$pipeline = new MiddlewarePipeline();
$pipeline->pipe($siteMiddleware);
$pipeline->pipe($userMiddleware, '/user');
$pipeline->pipe($handlerMiddleware);

$runner = new ServerRequestRunner($pipeline);
$runner->run($request); // Output result: 'Hello world!'