2017 © Pedro Peláez
 

library middleware-container

Psr-15 middleware proxying a Psr-11 container entry

image

ellipse/middleware-container

Psr-15 middleware proxying a Psr-11 container entry

  • Thursday, March 22, 2018
  • by pmall
  • Repository
  • 1 Watchers
  • 0 Stars
  • 97 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 7 % Grown

The README.md

Middleware container

This package provides a Psr-15 middleware proxying a Psr-11 container entry., (*1)

Require php >= 7.0, (*2)

Installation composer require ellipse/middleware-container, (*3)

Run tests ./vendor/bin/kahlan, (*4)

Using container entries as middleware

The class Ellipse\Middleware\ContainerMiddleware takes an implementation of Psr\Container\ContainerInterface and a container id as parameters. Its ->process() method retrieve a middleware from the container using this id and proxy its ->process() method., (*5)

It can be useful in situations the container entry should be resolved at the time the request is processed., (*6)

An Ellipse\Middleware\Exceptions\ContainedMiddlewareTypeException is thrown when the value retrieved from the container is not an object implementing Psr\Http\Server\MiddlewareInterface., (*7)

<?php

namespace App;

use SomePsr11Container;

use Ellipse\Middleware\ContainerMiddleware;

// Get some Psr-11 container.
$container = new SomePsr11Container;

// Add a middleware in the container.
$container->set('some.middleware', function () {

    return new SomeMiddleware;

});

// Create a container middleware with the Psr-11 container and the entry id.
$middleware = new ContainerMiddleware($container, 'some.middleware');

// The middleware ->process() method retrieve the middleware from the container and proxy it.
$response = $middleware->process($request, new SomeRequestHandler);

Example using auto wiring

It can be cumbersome to register every middleware classes in the container. Here is how to auto wire middleware instances using the Ellipse\Container\ReflectionContainer class from the ellipse/container-reflection package., (*8)

<?php

namespace App;

use Psr\Http\Server\MiddlewareInterface;

use SomePsr11Container;

use Ellipse\Container\ReflectionContainer;
use Ellipse\Middleware\ContainerMiddleware;

// Get some Psr-11 container.
$container = new SomePsr11Container;

// Decorate the container with a reflection container.
// Specify the middleware implementations can be auto wired.
$reflection = new ReflectionContainer($container, [
    MiddlewareInterface::class,
]);

// Create a container middleware with the reflection container and a middleware class name.
$middleware = new ContainerMiddleware($reflection, SomeMiddleware::class);

// An instance of SomeMiddleware is built and its ->process() method is proxied.
$response = $middleware->process($request, new SomeRequestHandler);

The Versions

22/03 2018

dev-master

9999999-dev https://github.com/ellipsephp/middleware-container

Psr-15 middleware proxying a Psr-11 container entry

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre Mallinjoud

middleware container resolver factory psr-11 psr-15

19/03 2018

1.0.3

1.0.3.0 https://github.com/ellipsephp/middleware-container

Psr-15 middleware proxying a Psr-11 container entry

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre Mallinjoud

middleware container psr-11 psr-15

18/03 2018

1.0.2

1.0.2.0 https://github.com/ellipsephp/middleware-container

Psr-15 middleware proxying a Psr-11 container entry

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre Mallinjoud

middleware container psr-11 psr-15

04/03 2018

1.0.1

1.0.1.0 https://github.com/ellipsephp/middleware-container

Psr-15 middleware proxying a Psr-11 container entry

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre Mallinjoud

middleware container psr-11 psr-15

30/01 2018

1.0.0

1.0.0.0 https://github.com/ellipsephp/middleware-container

Psr-15 middleware proxying a Psr-11 container entry

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre Mallinjoud

middleware container psr-11 psr-15