PHP code example of n1215 / http-context

1. Go to this page and download the library: Download n1215/http-context library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

n1215 / http-context example snippets


interface HttpContextInterface
{
    public function getRequest() : ServerRequestInterface;

    public function getResponse() : ResponseInterface;

    public function isTerminated(): bool;

    public function withRequest(ServerRequestInterface $request): HttpContextInterface;

    public function withResponse(ResponseInterface $response): HttpContextInterface;

    public function withIsTerminated(bool $isTerminated): HttpContextInterface;

    public function handledBy(HttpHandlerInterface $handler): HttpContextInterface;

}

interface HttpHandlerInterface
{
    public function __invoke(HttpContextInterface $context) : HttpContextInterface;
}

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use N1215\Http\Context\HttpContextInterface;
use N1215\Http\Context\HttpHandlerInterface;

class HttpHandler implements HttpHandlerInterface {

    public function __invoke(HttpContextInterface $context): HttpContextInterface
    {
        //do stuff
        $context->getResponse()->getBody()->write('Hello, world!');
        return $context;
    }

}

/**
 * @var ServerRequestInterface $request
 */
$request = ServerRequestFactory::fromGlobals();

/**
 * @var ResponseInterface $response
 */
$response = new Response();

$context = new HttpContext($request, $response); // implements HttpContextInterface

$handler = new HttpHandler();

$newContext = $handler->__invoke($context); // or $handler($context);

$newResponse = $newContext->getResponse();

$context = new HttpContext($request, $response);

$first = new FirstHttpHandler(); // implements HttpHandlerInterface
$second = new SecondHttpHandler(); // implements HttpHandlerInterface

$newContext = $second($first($context));

$context = new HttpContext($request, $response);

$newContext = $context
    ->handledBy(new FirstHttpHandler());
    ->handledBy(new SecondHttpHandler());

class HandlerPipeline implements HttpHandlerInterface {

    /**
     * @var HttpHandlerInterface[]
     */
    private $handlers = [];


    public function __construct(array $handlers = []) {
        $this->handlers = $handlers;
    }

    public function __invoke(HttpContextInterface $context) : HttpContextInterface
    {
        foreach($this->handlers as $handler) {
            if($context->isTerminated()) {
                return $context;
            }
            $context = $handler->__invoke($context);
        }

        return $context;
    }
}

$context = new HttpContext($request, $response);

$pipeline = new HandlerPipeline([
    new FirstHttpHandler(),
    new SecondHttpHandler(),
]);

$newContext = $pipeline($context);