middlewares / utils
Common utils for PSR-15 middleware packages
Installs: 2 537 250
Dependents: 95
Suggesters: 0
Security: 0
Stars: 51
Watchers: 4
Forks: 12
Open Issues: 1
Requires
- php: >=8.1
- psr/container: ^1.0 || ^2.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.41
- guzzlehttp/psr7: ^2.6
- laminas/laminas-diactoros: ^3.3
- nyholm/psr7: ^1.8
- oscarotero/php-cs-fixer-config: ^2.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- slim/psr7: ^1.6
- squizlabs/php_codesniffer: ^3.8
- sunrise/http-message: ^3.0
This package is auto-updated.
Last update: 2024-10-17 14:43:49 UTC
README
Common utilities used by the middlewares' packages:
Installation
This package is installable and autoloadable via Composer as middlewares/utils.
composer require middlewares/utils
Factory
Used to create PSR-7 and PSR-17 instances. Detects automatically Diactoros, Guzzle, Slim, Nyholm/psr7 and Sunrise but you can register a different factory using the psr/http-factory interface.
use Middlewares\Utils\Factory; use Middlewares\Utils\FactoryDiscovery; // Create PSR-7 instances $request = Factory::createRequest('GET', '/'); $serverRequest = Factory::createServerRequest('GET', '/'); $response = Factory::createResponse(200); $stream = Factory::createStream('Hello world'); $uri = Factory::createUri('http://example.com'); $uploadedFile = Factory::createUploadedFile($stream); // Get PSR-17 instances (factories) $requestFactory = Factory::getRequestFactory(); $serverRequestFactory = Factory::getServerRequestFactory(); $responseFactory = Factory::getResponseFactory(); $streamFactory = Factory::getStreamFactory(); $uriFactory = Factory::getUriFactory(); $uploadedFileFactory = Factory::getUploadedFileFactory(); // By default, use the FactoryDiscovery class that detects diactoros, guzzle, slim, nyholm and sunrise (in this order of priority), // but you can change it and add other libraries Factory::setFactory(new FactoryDiscovery( 'MyApp\Psr17Factory', FactoryDiscovery::SLIM, FactoryDiscovery::GUZZLE, FactoryDiscovery::DIACTOROS )); //And also register directly an initialized factory Factory::getFactory()->setResponseFactory(new FooResponseFactory()); $fooResponse = Factory::createResponse();
Dispatcher
Minimalist PSR-15 compatible dispatcher. Used for testing purposes.
use Middlewares\Utils\Dispatcher; $response = Dispatcher::run([ new Middleware1(), new Middleware2(), new Middleware3(), function ($request, $next) { $response = $next->handle($request); return $response->withHeader('X-Foo', 'Bar'); } ]);
CallableHandler
To resolve and execute a callable. It can be used as a middleware, server request handler or a callable:
use Middlewares\Utils\CallableHandler; $callable = new CallableHandler(function () { return 'Hello world'; }); $response = $callable(); echo $response->getBody(); //Hello world
HttpErrorException
General purpose exception used to represent HTTP errors.
use Middlewares\Utils\HttpErrorException; try { $context = ['problem' => 'Something bad happened']; throw HttpErrorException::create(500, $context); } catch (HttpErrorException $exception) { $context = $exception->getContext(); }
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.