germania-kg / aurasession-middleware
1.3.1
2022-03-30 09:46 UTC
Requires
- php: ^5.6|^7.0
- aura/session: ^2.1
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.0
Requires (Dev)
- php: ^7.1
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^5.7|^6.0|^7.0
- pimple/pimple: ^3.0
- slim/slim: ^3.0
Suggests
- pimple/pimple: This package provides a ServiceProvider for the famous Pimple Dependency Injection Container.
README
Middleware to inject Aura.Session Segments into PSR-7 ServerRequests.
Installation with Composer
$ composer require germania-kg/aurasession-middleware
Usage
<?php use Germania\AuraSessionMiddleware\AuraSessionSegmentMiddleware; // Create session and segment, cf. Aura.Session docs $session_factory = new \Aura\Session\SessionFactory; $session = $session_factory->newInstance($_COOKIE); $segment = $session->getSegment('Vendor\Package\ClassName'); // Optional with PSR-3 Logger $mw = new AuraSessionSegmentMiddleware( $segment ); $mw = new AuraSessionSegmentMiddleware( $segment, $logger );
Inside your routes
<?php $app = new \Slim\App(); $app->get('/books/{id}', function ($request, $response, $args) { // This is your Aura.Session segment $session = $request->getAttribute("session"); ... });
Set PSR7 Request attribute name
Per default, the SessionSegment is stored as the PSR7 Request attribute named session
.
You can set a custom name right after instantiation, i.e. before the middleware is invoked:
<?php $mw = new AuraSessionSegmentMiddleware( $segment ); // "session" per default echo $mv->getRequestAttributeName( ); // Choose another one... $mv->setRequestAttributeName( "custom_name" ); // Inside route: $session = $request->getAttribute("custom_name");
Pimple Service Provider
<?php use Germania\AuraSessionMiddleware\PimpleServiceProvider; use Aura\Session\SegmentInterface; use Psr\Log\LoggerInterface; // have your Pimple DIC ready, and optionally a PSR3 Logger: $sp = new PimpleServiceProvider("session", "request_attr"); $sp = new PimpleServiceProvider("session", "request_attr", $psr3_logger); $sp->register( $dic ); // Grab your services $yes = $dic['Session'] instanceOf SegmentInterface; $yes = $dic['Session.Logger'] instanceOf LoggerInterface; $yes = is_callable( $dic['Session.Middleware'] );
Use with Slim Framework:
<?php $app = new \Slim\App; $app->add( $dic['Session.Middleware'] );
Development
$ git clone https://github.com/GermaniaKG/AuraSessionMiddleware.git
$ cd AuraSessionMiddleware
$ 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