# Class HttpSoft\Runner\ServerRequestRunner

Runs the Psr\Http\Message\ServerRequestInterface implementation and emits the client an instance of Psr\Http\Message\ResponseInterface.

Source code on GitHub.

use HttpSoft\Runner\ServerRequestRunner;

/**
 * @var HttpSoft\Runner\MiddlewarePipelineInterface $pipeline
 * @var HttpSoft\Emitter\EmitterInterface $emitter
 * @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);

// Create an instance with arguments

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

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

// Run without handler

$pipeline->pipe(new class implements Psr\Http\Server\MiddlewareInterface {
    public function process(
        Psr\Http\Message\ServerRequestInterface $request, 
        Psr\Http\Server\RequestHandlerInterface $handler
    ): Psr\Http\Message\ResponseInterface {
        $response = new HttpSoft\Message\Response();
        $response->getBody()->write('Hello world!');
        return $response;
    }
});
$runner = new ServerRequestRunner($pipeline);
$runner->run($request); // Output result: 'Hello world!'

# Public methods

public function __construct(
    HttpSoft\Runner\MiddlewarePipelineInterface $pipeline = null,
    HttpSoft\Emitter\EmitterInterface $emitter = null
);

If $pipeline was not specified or is null, the HttpSoft\Runner\MiddlewarePipeline will be used.

If $emitter was not specified or is null, the HttpSoft\Emitter\SapiEmitter will be used.

# run

Runs the Psr\Http\Message\ServerRequestInterface implementation and emits the client an instance of Psr\Http\Message\ResponseInterface.

public function run(
    Psr\Http\Message\ServerRequestInterface $request, 
    Psr\Http\Server\RequestHandlerInterface $defaultHandler = null
): void;

If $defaultHandler was specified, the HttpSoft\Runner\MiddlewarePipelineInterface::process() method will be called, otherwise the HttpSoft\Runner\MiddlewarePipelineInterface::handle() method will be called.