germania-kg / middleware
Collection of PSR-15 and PSR-7-style middleware
Requires
- php: ^7.0|^8.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^8.0|^9.0
Suggests
- swiftmailer/swiftmailer: The PHP Mailer of choice
This package is auto-updated.
Last update: 2024-12-19 23:06:19 UTC
README
Germania KG ยท Middleware
Collection of useful PSR-15 Single Pass and Double Pass middleware we use in our apps
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