2017 © Pedro PelΓ‘ez
 

library php-simple-middlewares

Simple middlewares manager compatible with the PSR-7

image

alexpts/php-simple-middlewares

Simple middlewares manager compatible with the PSR-7

  • Saturday, March 3, 2018
  • by alexpts
  • Repository
  • 1 Watchers
  • 2 Stars
  • 99 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

php-simple-middlewares

SensioLabsInsight, (*1)

Build Status Test Coverage Code Climate Scrutinizer Code Quality, (*2)

New PSR-15 compatible middleware manager, (*3)

(deprecated) Simple middlewares compatible with the PSR-7, (*4)

MiddlewaresManager позволяСт Π½Π°ΡΡ‚Ρ€Π°ΠΈΠ²Π°Ρ‚ΡŒ ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ выполнСния ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½ΠΎΠ³ΠΎ уровня. ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ middleware ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ Π½Π° Π²Ρ…ΠΎΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ запроса $request, ΠΏΡ€ΠΈ ΠΆΠ΅Π»Π°Π½ΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ‚ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ с Π½ΠΈΠΌ ΠΈΠ»ΠΈ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ Π½ΠΎΠ²Ρ‹ΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ $request ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ Π΅Π³ΠΎ Π² ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ middleware, оТидая ΠΎΡ‚ Π½Π΅Π³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ response. Π—Π°Ρ‚Π΅ΠΌ ΠΎΠ½ ΠΌΠΎΠΆΠ΅Ρ‚ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ с ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠΌ response ΠΈΠ»ΠΈ просто ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ Π΅Π³ΠΎ ΠΏΠ΅Ρ€Π΄Ρ‹Π΄ΡƒΡ‰Π΅ΠΌΡƒ middleware., (*5)

Milddleware, ΠΌΠΎΠΆΠ΅Ρ‚ Π½Π΅ Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ middleware, Π° Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Π² Ρ€Π°Π½Π΅Π΅ Π²Ρ‹Π·Π²Π°Π½Π½Ρ‹ΠΉ middleware. Π’.Π΅. ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ middleware ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ ΡƒΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ 2 Ρ€Π°Π·Π°. Π’ ΠΌΠΎΠΌΠ΅Π½Ρ‚ ΠΏΡ€ΠΎΡ…ΠΎΠ΄Π° ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° $request ΠΈ Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° $response ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎ, (*6)

Для обСспСчСния гибкости ΠΈ встраивомости ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π°, middleware ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΊΠΈΡ‡ΠΈ Π½Π΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½Ρ‹ Π½ΠΈΠΊΠ°ΠΊΠΈΠΌ интСрфСйсом. ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠΌ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ любой callable Ρ‚ΠΈΠΏ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½ΠΎ 2 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° ServerRequestInterface $request, callable $next. Для Π±ΠΎΠ»Π΅Π΅ Ρ„ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Ρ‚ΡŒ Ρ„ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ интСрфСйс MiddlewareInterface., (*7)

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹ΠΉ Ρ‚ΠΈΠΏ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° $response Ρ‚Π°ΠΊΠΆΠ΅ Π½ΠΈΠΊΠ°ΠΊ Π½Π΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½. Π’Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ ΠΈΠ· ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° любой Ρ‚ΠΈΠΏ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ массив ΠΈΠ»ΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Ρ‚ΠΈΠΏΠ° ResponseInterface (psr-7). Π€ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½ΠΎ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚ΡŒ Π½Π° свой Π²Ρ‹Π±ΠΎΡ€ Π²Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ ΡƒΠΊΠ°Π·Π°Π² Ρ‚ΠΈΠΏ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΠΎΠ³ΠΎ значСния Π² php 7., (*8)

Installation

$ composer require alexpts/php-simple-middlewares, (*9)

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ, (*10)

use Psr\Http\Message\ServerRequestInterface;
use PTS\Middleware\MiddlewareManager;

$middlewareManager = new MiddlewareManager();
$middlewareManager->push(function($request, $next){
    /* optionally modify the request */
    $request = $request->...;

    /* optionally skip the $next middleware and return early */
    if (...) {
        return $response;
    }

    /* optionally invoke the $next middleware and get back a new response */
    $response = $next($request);

    /* optionally modify the Response if desired */
    $response = $response->...;

    return $response;
});

НСсолько MiddlewareInterface ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠ², (*11)

use Psr\Http\Message\ServerRequestInterface;
use PTS\Middleware\MiddlewareManager;
use PTS\Middleware\MiddlewareInterface;

class MiddlewareA implements MiddlewareInterface
{
    public function __invoke(ServerRequestInterface $request, callable $next)
    {
        $method = $request->getMethod();
        if (method !== 'GET') {
            return new JsonResponse(['status' => 405], 405);
        }

        $response = $next($request);
        /* optionally modify the Response if desired */
        $response = $response->...;

        return $response;
    }
}

class FrontController implements MiddlewareInterface
{
    public function __invoke(ServerRequestInterface $request, callable $next)
    {
        /* ... some work and create response */ 
        return $response;
    }
}

$middlewareManager = new MiddlewareManager();
$middlewareManager->push(new MiddlewareA);
$middlewareManager->push(new FrontController);

/* ... PSR-7 $request */
$response = $middlewareManager($request);

ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ ошибок/ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ

Если Ρ€Π°ΡΡΠΌΠ°Ρ‚Ρ€ΠΈΠ²Π°Ρ‚ΡŒ middleware ΠΊΠ°ΠΊ нСзависимыС прилоТСния, Ρ‚ΠΎ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ PSR-7 ΠΎΡ‚Π²Π΅Ρ‚ Π² случаС ошибки/ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ. Π’Π°ΠΊΠΎΠΉ ΠΎΡ‚Π²Π΅Ρ‚ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΏΡ€ΠΎΠΉΡ‚ΠΈ всю Ρ†Π΅ΠΏΠΎΡ‡ΠΊΡƒ middleware Π² ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΌ порядкС., (*12)

$middlewareManager = new MiddlewareManager();
$middlewareManager->push(new MiddlewareA);
$middlewareManager->push(new FrontController, function (\Throwable $ex) {
    return new JsonResponse(['error' => $ex->getMessage()], 500);
});

$response = $middlewareManager($request);

The Versions

03/03 2018

dev-master

9999999-dev https://github.com/alexpts/php-simple-middlewares

Simple middlewares manager compatible with the PSR-7

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

middleware psr-7 router

12/06 2017

1.1.0

1.1.0.0 https://github.com/alexpts/php-simple-middlewares

Simple middlewares manager compatible with the PSR-7

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

middleware psr-7

04/01 2016

1.0.0

1.0.0.0 https://github.com/alexpts/php-simple-middlewares

Simple middlewares manager compatible with the PSR-7

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

psr-7 router