PHPackages                             juanchosl/requestlistener - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. juanchosl/requestlistener

ActiveLibrary[HTTP &amp; Networking](/categories/http)

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

1.0.0(5mo ago)11731MITPHPPHP ^8.1CI passing

Since Feb 18Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/JuanchoSL/RequestListener)[ Packagist](https://packagist.org/packages/juanchosl/requestlistener)[ Docs](https://github.com/JuanchoSL/RequestListener)[ RSS](/packages/juanchosl-requestlistener/feed)WikiDiscussions 1.0 Synced today

READMEChangelog (1)Dependencies (12)Versions (5)Used By (1)

RequestListener
===============

[](#requestlistener)

Description
-----------

[](#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
-------

[](#install)

```
composer require juanchosl/requestlistener
```

Engines
-------

[](#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

[](#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

[](#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
-----------

[](#application)

### Middlewares

[](#middlewares)

#### PRE middlewares

[](#pre-middlewares)

##### Enabled by default

[](#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

[](#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, accord 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

[](#user-defined-pre-middlewares)

You can create and use Middlewares that implementing the PSR-15 Interfaces, according the [Psr\\Http\\Server\\MiddlewareInterface](https://www.php-fig.org/psr/psr-15/)

```
