juanchosl / requestlistener
Little methods collection in order to create an APP listener, that can be able to receive, process and response to http request and cli commands
Requires
- php: ^8.1
- ext-mbstring: *
- fig/http-message-util: 1.1.*
- juanchosl/datamanipulation: ^1.0
- juanchosl/datatransfer: ^1.0
- juanchosl/exceptions: 1.0.*
- juanchosl/httpdata: ^1.0
- juanchosl/logger: 1.1.*
- psr/container: 2.0.*
- psr/http-server-handler: 1.0.*
- psr/http-server-middleware: 1.0.*
- psr/log: 3.0.*
Requires (Dev)
- phpstan/phpstan: 1.10.*
- phpunit/phpunit: 9.6.*
Suggests
- juanchosl/logger: ^1.1
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-04 19:50:27 UTC
README
Description
This library groups the definitions of the different elements involved in data transmissions such as HTTP, applying the interfaces defined in PSRS 7 and 17, adding the extra objects used and ensuring compliance with the requirements that allow to give the necessary stability To our application, being able to implement for third -party bookstores without the need to adapt our business logic to a new implementation that could modify its consumption or internal structure.
Install
composer require juanchosl/requestlistener
Engines
The library accept two types of uses
- http requests
- console commands
Both systems parse the SERVER globals and create a ServerRequestInterface compatible element in order to unify his use across all the work flow. The user needs to prepare the response format according to the requested type. You can create you own Engine parser implementing the JuanchoSL\Contracts\EnginesInterface, only needs:
- static parse method, passing the vars to parse. Returns an EngineInterface entity
- getRequest, to retrieve the parsed ServerRequest
- sendMessage, to perform and send the compatible ResponseInterface message
HTTP requests
Create a request according to the PSR-7, the request is builded using PHP superglobals, for body contents, if content-type header is setted as application/x-www-form-urlencoded or multipart/form-data, the superglobal POST is used, for other media-types, the 'php://input' contents is readed and putted as body and parsed as a DataTranster entity and putted into parsedBody
Console commands
Create a request according to the PSR-7, the request is builded using SERVER['args'] superglobal for fill Query params and STDIN for body contents, the arguments need to have the next format: The parameter name needs to start with --, then can assign values from:
- concat with an equals sign (--name=value)
- put the value after parameter (--name value)
- if is a void parameter that don't need value, just write the parameter name (--void_parameter)
- if is a multiple values parameter, use the name all times that you need pass a value or write the name and value multiple times as a single value
- --multiple=value1 --multiple=value2
- --multiple value1 --multiple value2
- --multiple value1 value2
Application
Middlewares
PRE middlewares
Enabled by default
- ValidRouteMiddleware: Check than the selected target is defined and exists
- OptionsMethodMiddleware: Return a response with the available METHODs for the selected target
- TraceMethodMiddleware: Check if the TRACE method is available for the selected target
- HeadMethodMiddleware: If the request is a HEAD method request, convert to GET, process it and remove the body before return to client
Available for use
- ValidMethodMiddleware: Check if the requested method is available, if a HEAD method is selected, we convert to a GET before check it, and remove the body from response if it is availble
- ValidMediaTypeMiddleware: Check if any accepted media-type from the request is available
- CacheMiddleware: Can enable this available middleware providing a PSR-16 SimpleCacheInterface in order to use it
- OutputCompressionMiddleware: Using this available middleware, can compress or encodig response bodies, according the Accept-Encoding client header, in br(brotli), zstd, deflate or gzip
- RateLimitMiddleware: In order to count the UNAUTHORIZED responses and block the client using SESSION for N seconds, can indicate the num of failed counts and the banned seconds
User Defined PRE middlewares
You can create and use Middlewares that implementing the PSR-15 Interfaces, according the Psr\Http\Server\MiddlewareInterface
<?php declare(strict_types=1); namespace Src\Infrastructure\Middlewares; use Fig\Http\Message\StatusCodeInterface; use JuanchoSL\HttpData\Factories\ResponseFactory; use JuanchoSL\Validators\Types\Strings\StringValidations; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; class AuthorizationMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $ip = filter_input(INPUT_SERVER, 'SERVER_ADDR'); if (!(new StringValidations)->isNotEmpty()->isIpV4()->isValueContaining('192.168.0.1')->getResult((string)$ip)) { return (new ResponseFactory)->createResponse(StatusCodeInterface::STATUS_UNAUTHORIZED); } return $handler->handle($request); } }
Other available Middlewares
- CacheMiddleware: Use any compatible PSR-16 library in order to check, load and save web caches
- OutputCompressionMiddleware: Compress http responses using native libraries (deflate, gzip, zstd and brotli)
- DebugXhprofMiddleware
Request handlers
According the PSR-15, you can create and execute your own RequestHandlers
<?php declare(strict_types=1); namespace JuanchoSL\RequestListener\Commands; use Fig\Http\Message\StatusCodeInterface; use JuanchoSL\DataTransfer\Factories\DataConverterFactory; use JuanchoSL\DataTransfer\Factories\DataTransferFactory; use JuanchoSL\HttpData\Factories\ResponseFactory; use JuanchoSL\HttpData\Factories\StreamFactory; use JuanchoSL\HttpHeaders\ContentType; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; class ConvertHandler implements RequestHandlerInterface { public function handle(ServerRequestInterface $request): ResponseInterface { $body_contents = DataTransferFactory::byMimeType((string) $request->getBody(), $request->getHeader('content-type')); $content_type = ($request->hasHeader('accept')) ? $request->getHeader('accept') : ContentType::get($request->getQueryParams()['format']); $body = DataConverterFactory::asMimeType($body_contents, $content_type); return (new ResponseFactory)->createResponse(StatusCodeInterface::STATUS_OK) ->withAddedHeader('content-type', $content_type) ->withBody((new StreamFactory)->createStream($body)); } }
Use cases
The Application system, group the routing, methods access, and callables to be executed when the rules has been accomplished. Into the entrypoint, you need to prepare endpoints and his rules to be executed.
When you extend the UseCases provided class, a configure method is required, in order to set the valid parameters, performing an autovalidation. Actually we have available the validations in two levels, requiring and type.
The requiring mode can be:
- InputArgument::REQUIRED -> the lib check than the parameter is provided by the user
- InputArgument::OPTIONAL -> if is provided, knowing it, the lib can check the desired type
The type validations can be for:
- InputOption::VOID = 'void' ->the lib checkif is present, no value needs to be present
- InputOption::BOOL = 'bool' -> a value as string is accepted, true, "true", 1, "1", "On" for compare as bool true, and false, "false",0,"0","Off" as bool false
- InputOption::SINGLE = 'single' -> only one value is accepted
- InputOption::SINGLE_INT = 'single_int' -> only one value is accepted, and needs to be an integer value (or a string with only an integer value)
- InputOption::SINGLE_NUMBER = 'single_number' -> only one value is accepted, and needs to be a number value (or a string with only a numeric value)
- InputOption::MULTI = 'multiple' -> N values are accepted
- InputOption::MULTI_INT = 'multiple_int' -> few values are accepted, and needs to be integers values (or strings with only integers values)
- InputOption::MULTI_NUMBER = 'multiple_number' -> few values are accepted, and needs to be numeric values (or strings with only numeric values)
The callables can be:
- A Handler implementing the PSR-15 interface
- A command, extending the UseCases provided class and implementing an __invoke method with the params:
- ServerRequestInterface
- ResponseInterface
- A callable with format
[ClassName::class, 'method_to_call']
<?php use JuanchoSL\Logger\Logger; use JuanchoSL\Logger\Composers\TextComposer; use JuanchoSL\Logger\Repositories\FileRepository; use JuanchoSL\RequestListener\Application; use JuanchoSL\RequestListener\Handlers\MyErrorHandler; use JuanchoSL\RequestListener\Middlewares\AuthenticationMiddleware; use JuanchoSL\RequestListener\Middlewares\OutputCompressionMiddleware; date_default_timezone_set("Europe/Madrid"); include_once dirname(__FILE__, 2) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; $logger = new Logger((new FileRepository(implode(DIRECTORY_SEPARATOR, ['..', 'logs', 'error.log'])))->setComposer(new TextComposer)); $errorHandler = (new MyErrorHandler)->setLogErrors(true)->setLogErrorsDetails(true)->setDisplayErrorsDetails(true); $errorHandler->setLogger($logger); $app = new Application(); $app->setErrorHandler($errorHandler); $app->addMiddleware(new AuthenticationMiddleware); $app->post('/convert', ConvertCommand::class); $app->put('/convert', ConvertHandler::class); $app->addMiddleware(new OutputCompressionMiddleware); $app->run(); //call to run, performs an exit in order to process shutdown_functions and exit code from console use
ExampleCommand
<?php declare(strict_types=1); namespace JuanchoSL\RequestListener\Commands; use Fig\Http\Message\StatusCodeInterface; use JuanchoSL\DataTransfer\Factories\DataConverterFactory; use JuanchoSL\DataTransfer\Factories\DataTransferFactory; use JuanchoSL\HttpData\Factories\StreamFactory; use JuanchoSL\HttpHeaders\ContentType; use JuanchoSL\RequestListener\Enums\InputArgument; use JuanchoSL\RequestListener\Enums\InputOption; use JuanchoSL\RequestListener\UseCases; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; class ConvertCommand extends UseCases { protected function configure(): void { $this->addArgument('format', InputArgument::OPTIONAL, InputOption::SINGLE); } public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { $body_contents = DataTransferFactory::byMimeType((string) $request->getBody(), $request->getHeader('content-type')); $content_type = ($request->hasHeader('accept')) ? $request->getHeader('accept') : ContentType::get($request->getQueryParams()['format']); $body = DataConverterFactory::asMimeType($body_contents, $content_type); return $response ->withStatus(StatusCodeInterface::STATUS_OK) ->withAddedHeader('content-type', $content_type) ->withBody((new StreamFactory)->createStream($body)); } }