Skip to content

GermaniaKG/Middleware

Repository files navigation


Germania KG · Middleware

Collection of useful PSR-15 Single Pass and Double Pass middleware we use in our apps

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation with Composer

$ composer require germania-kg/middleware

LogHttpStatusMiddleware

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 )

EmailExceptionMiddleware

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 );

Bonus: Display exception information

<?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 );
}

ScriptRuntimeMiddleware

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 )

LogExceptionMiddleware

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 )

Development

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

Unit tests

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages