arnapou / psr-http
Library - PSR-7, PSR-15, PSR-17, PSR-18.
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0
- arnapou/psr-event: ^1.0
- nyholm/psr7: ^1.8
- nyholm/psr7-server: ^1.1
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 || ^2.0
- psr/http-server-handler: ^1.0
- symfony/http-client: ^5.4 || ^6.4
Requires (Dev)
- arnapou/dto: ^2.0 || ^3.0 || ^4.0
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
README
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
Date | Ref | 8.4 | 8.3 | 8.2 |
---|---|---|---|---|
24/11/2024 | 1.1.x, main | × | × | × |
09/09/2024 | 1.0.x | × | × |