germania-kg/aurasession-middleware

1.3.1 2022-03-30 09:46 UTC

This package is auto-updated.

Last update: 2024-10-29 04:53:07 UTC


README

Middleware to inject Aura.Session Segments into PSR-7 ServerRequests.

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

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