Collection of useful PSR-15 Single Pass and Double Pass middleware we use in our apps
$ composer require germania-kg/middleware
Writes the HTTP Response's status code and reason to a PSR-3 Logger after $next has finished, using Psr\Log\LoggerInterface::info method. While this middleware is PSR-15 compliant, here a Slim3 example:
<?php
use Germania\Middleware\LogHttpStatusMiddleware;
$app = new Slim\App;
$logger = new \Monolog\Logger;
$middleware = new LogHttpStatusMiddleware( $logger);
$app->add( $middleware );
Class LogHttpStatusMiddleware also implements Psr\Log\LoggerAwareInterface
and additionally uses Germania\Middleware\LogLevelTrait
, so configure logging like this:
$middleware->setLogger($monolog)
->setLogLevel( \Psr\Log\LogLevel::INFO )
While this middleware is PSR-15 compliant, here a Slim3 example:
<?php
use Germania\Middleware\EmailExceptionMiddleware;
$app = new Slim\App;
$mailer_factory = function() {
return Swift_Mailer::newInstance( ... );
};
$message_factory = function() {
return Swift_Message::newInstance();
};
$middleware = new EmailExceptionMiddleware("My APP", $mailer_factory, $message_factory);
$app->add( $middleware );
<?php
use Germania\Middleware\EmailExceptionMiddleware;
$middleware = new EmailExceptionMiddleware("My APP", $mailer_factory, $message_factory);
try {
throw new \Exception("Huh?");
}
catch (\Exception $e) {
echo $middleware->render( $e );
}
Logs the time taken from instantiation to the time when the next middlewares have been executed. It uses the info() method described in PSR-3 LoggerInterface . While this middleware is PSR-15 compliant, here a Slim3 example:
<?php
use Germania\Middleware\ScriptRuntimeMiddleware;
$app = new Slim\App;
$logger = new \Monolog\Logger;
$app->add( new ScriptRuntimeMiddleware($logger) );
Class ScriptRuntimeMiddleware also implements Psr\Log\LoggerAwareInterface
and additionally uses Germania\Middleware\LogLevelTrait
, so configure logging like this:
$middleware->setLogger($monolog)
->setLogLevel( \Psr\Log\LogLevel::INFO )
Logs information about exceptions thrown during next middlewares execution. It uses the warning() method described in PSR-3 LoggerInterface. While this middleware is PSR-15 compliant, here a Slim3 example:
<?php
use Germania\Middleware\LogExceptionMiddleware;
$app = new Slim\App;
$logger = new \Monolog\Logger;
$app->add( new LogExceptionMiddleware($logger) );
Class LogExceptionMiddleware also implements Psr\Log\LoggerAwareInterface
and additionally uses Germania\Middleware\LogLevelTrait
, so configure logging like this:
$middleware->setLogger($monolog)
->setLogLevel( \Psr\Log\LogLevel::INFO )
Clone that repo, dive into directory and install Composer dependencies.
# Clone and install
$ git clone https://github.com/GermaniaKG/Middleware.git <directory>
$ cd <directory>
$ composer install
Either copy phpunit.xml.dist
to phpunit.xml
and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test
# or
$ vendor/bin/phpunit