• Stars
    star
    51
  • Rank 549,428 (Top 12 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 7 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Common utils used by PSR-15 middlewares

middlewares/utils

Latest Version on Packagist Software License Testing Total Downloads

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.

More Repositories

1

psr15-middlewares

Collection of PSR-15 middlewares
398
star
2

awesome-psr15-middlewares

A curated list of awesome PSR-15 HTTP Middleware resources
317
star
3

fast-route

PSR-15 middleware to use FastRoute
PHP
95
star
4

request-handler

PSR-15 middleware to execute request handlers
PHP
44
star
5

negotiation

PSR-15 middleware to implement content negotiation
PHP
44
star
6

http-authentication

PSR-15 middleware to implement Basic and Digest Http authentication
PHP
37
star
7

whoops

PSR-15 middleware to use Whoops as error handler
PHP
32
star
8

payload

PSR-15 middleware to parse the body of the request with support for json, csv and url-encode
PHP
30
star
9

trailing-slash

PSR-15 middleware to normalize the trailing slash of the uri path
PHP
27
star
10

proxy

PSR-15 middleware to forward requests and return responses
PHP
21
star
11

access-log

PSR-15 middleware to generate access logs
PHP
20
star
12

minifier

Middleware to minify Html, CSS and Javascript responses
PHP
17
star
13

cache

PSR-15 middleware with various cache utilities
PHP
16
star
14

client-ip

PSR-15 middleware to detect the client ip and save it as a request attribute.
PHP
16
star
15

debugbar

PSR-15 middleware to insert PHP DebugBar automatically in html responses
PHP
16
star
16

response-time

PSR-15 middleware to save the response time into the X-Response-Time header
PHP
14
star
17

error-handler

PSR-15 middleware to handle http errors
PHP
14
star
18

https

PSR-15 middleware to redirect to https and adds the Strict-Transport-Security header
PHP
14
star
19

php-session

PSR-15 middleware to create a php session using the request data
PHP
13
star
20

csp

PSR-15 middleware to add the Content-Security-Policy header to the response
PHP
13
star
21

honeypot

PSR-15 middleware to implement a honeypot spam prevention
PHP
13
star
22

referrer-spam

PSR-15 middleware to block referrer spammers
PHP
12
star
23

cors

PSR-15 middleware to implement Cross-Origin Resource Sharing (CORS)
PHP
12
star
24

firewall

Middleware to provide IP filtering
PHP
11
star
25

geolocation

PSR-15 middleware to geolocate the client using the ip address
PHP
11
star
26

image-manipulation

PSR-15 middleware to manipulate images on-demand
PHP
10
star
27

filesystem

PSR-15 middleware to write or read responses from files
PHP
10
star
28

aura-router

PSR-15 middleware to use Aura.Router
PHP
8
star
29

skeleton

A skeleton repository for PSR-15 middleware packages
PHP
7
star
30

encoder

PSR-15 middleware to encode the response body to gzip or deflate
PHP
6
star
31

recaptcha

PSR-15 middleware to use Google reCAPTCHA for spam prevention
PHP
6
star
32

redirect

Middleware to redirect old urls to new urls SEO friendly
PHP
6
star
33

aura-session

PSR-15 middleware to manage sessions using Aura.Session
PHP
6
star
34

uuid

PSR-15 middleware to generate an UUID and save it in the X-Uuid header
PHP
5
star
35

shutdown

PSR-15 middleware to display a 503 maintenance page
PHP
5
star
36

csv-payload

PSR-15 middleware to parse the CSV body of the request
PHP
5
star
37

ideas

Need a Middleware or looking for ideas?
5
star
38

json-exception-handler

Middleware to catch exceptions and output them as JSON
PHP
5
star
39

reporting-logger

PSR-15 middleware to log server-side reportings
PHP
5
star
40

method-override

PSR-15 middleware to override the request method using the X-Http-Method-Override header
PHP
5
star
41

error-response

Middleware to create responses with error status code (4xx-5xx)
PHP
5
star
42

robots

PSR-15 middleware to enable/disable the robots of the search engines
PHP
4
star
43

emitter

Middleware to send (or emit) a PSR-7 response object using header() and echo
PHP
4
star
44

www

PSR-15 middleware to add or remove the www subdomain
PHP
4
star
45

base-path

PSR-15 middleware to remove the prefix from the uri path of the request
PHP
4
star
46

base-path-router

PSR-15 middleware to assign request handlers based on path prefixes
PHP
4
star
47

content-length

PSR-15 middleware to inject the Content-Length header into the response
PHP
3
star
48

lowercase

PSR-15 middleware to convert the URI path to lowercase
PHP
2
star