arnapou/psr-http

Library - PSR-7, PSR-15, PSR-17, PSR-18.

v1.0.0 2024-09-09 19:33 UTC

This package is auto-updated.

Last update: 2024-09-09 17:56:33 UTC


README

pipeline coverage

KISS (Keep It Simple Stupid) PSR (PHP Standards Recommendations) classes.

Installation

composer require arnapou/psr-http

packagist 👉️ arnapou/psr-http

When it is worth to use this library

  • you need simple decorators, proxies, adapters, ... about PSR's
  • you need simple implementations covering the basics

Example PSR-7 Http Message

We don't have our own PSR-7 classes, only decorators.

The project uses nyholm/psr7 which a super lightweight PSR-7 implementation, strict and fast.

Reinventing the wheel here does not bring any simplicity.

Decorator for send (and more)

$response = new \Arnapou\Psr\Psr7HttpMessage\Response($decoratedPsr7Response);
$response->withHeader(\Arnapou\Psr\Psr7HttpMessage\Header\CacheControl::sharedMaxAge(600))
$response->send();

Example PSR-15 Http Handlers

The way we can implement the PSR is very wide.

I basically built a "route handler" which uses internally my PSR-14 Event Dispatcher.

I should not have to tell you that this is enough simple for small projects without hundreds of routes.

If you need performance in your big project, you probably don't need this stuff.

This is very simple and 100% PSR dependant.

$handler = new \Arnapou\Psr\Psr15HttpHandlers\HttpRouteHandler();

// Add routes
$handler
    ->addRoute(
        '/users/{id}', 
        function (int $id) {
            return ['user'=> $id]
        }
    )
    ->setRequirement('id', '\d+');

// Get the server Request (here from global by lazyness)
$request = (new \Arnapou\Psr\Psr17HttpFactories\HttpFactory())->createServerRequestFromGlobals();

// Handle and get the response, a NoResponseFound will be thrown if no match
try {
    $response = $handler->handle($request);
} catch (\Arnapou\Psr\Psr15HttpHandlers\Exception\NoResponseFound) {
    $response = new \Arnapou\Psr\Psr7HttpMessage\HtmlResponse('Not Found', 404);
}

// Use the response decorator to send it
(new \Arnapou\Psr\Psr7HttpMessage\Response($response))->send();

Example PSR-17 Http Factory

The project uses nyholm/psr7 which a super lightweight PSR-7 implementation, strict and fast.

Reinventing the wheel here does not bring any simplicity.

Our PSR-17 class is a composition over nyholm/psr7 factory.

$factory = new \Arnapou\Psr\Psr17HttpFactories\HttpFactory();
$serverRequest = $factory->createServerRequestFromGlobals();

Example PSR-18 Http Client

The project uses symfony/http-client which is one of the best client while not being too heavy like other whales.\ Reinventing the wheel here does not bring any simplicity.

Our PSR-18 class is a composition over symfony/http-client and nyholm/psr7 factory.

$client = new \Arnapou\Psr\Psr18HttpClient\HttpClient();
$request = $client->createRequest('GET', 'https://arnapou.net/ip');
$response = $client->sendRequest($request);
echo $response->getBody();

Php versions

DateRef8.38.2
09/09/20241.0.x, main××