session-interop/middleware.async

This package contains a zend's middleware to be able to inject a Session Object into the request

This package is not auto-updated.

Last update: 2024-12-21 21:25:49 UTC


README

This middleware inject a SessionInterface as an attribute of a psr7's request's. The attribute's name is defined as Introp\Session\SessionInterface::class. It does not reconfigure the session if $_SESSION exists. This middleware work in two steps:

  1. This middleware open the session (by calling session_start) if needed to copy it to create an instance extending the ArraySession then close it immediatly (by calling session_abort) if it was not started before, to preserve the session state. Once the session copied, it is injected in the PSR7's request and then followings middleware are called.
  2. Once every following middleware has been executed, news / removed / modified session values (on the object) is wrote in $_SESSION and then the session is persisted. Once again, it ensure to reopen the session or to let it close depending on the previous session's state.

Warning: Persisted mean there is a manual call to session_write_close, that imply all $_SESSION to be wrote.

Recommanded

We recommands to place this middleware as soon as possible in the pipe of your application, this way every following middleware will be able to use the request's session.

Required

This middleware is designed to be used as a zend-stratigility middleware.

Usage

If you want to change the configuration, you must provide a SessionConfigurationInterface implementation as the factory parameter. Current server's configuration is used by using the package DefaultSessionConfiguration

Following example are assuming you use

###Basic Usage:

  // Without container

  $mySessionConfiguration = null;
  // If you have custom configuration:
  $mySessionConfiguration = new MySessionConfiguration();

  $sessionMiddleware = \Interop\Session\Middleware\Async\AsyncSessionMiddlewareFactory::createFromConfiguration($mySessionConfiguration);
  // $mySessionConfiguration is optional
  $app->pipe($sessionMiddleware);
  // OR using container
  $factory = new \Interop\Session\Middleware\Async\AsyncSessionMiddlewareFactory();
  $container->set(Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class, $factory->__invoke($container));
  //....
  $app->pipe(
    $container->get(\Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class)
  );

Now in every following Middleware:

$session = $request->getAttribute(\Interop\Session\SessionInterface::class);
$session->set("foo", "baz");
//...
echo $session->get("foo"); // print baz

Using service providers:

service-provider. This following example use Simplex If the provider find an instance of SessionConfigurationInterface it will be used. To be possible the container must inject the instance inside the container using the name Interop\Session\Configuration\SessionConfigurationInterface. This name is got by using:

use Interop\Session\Configuration\SessionConfigurationInterface;
//.....
SessionConfigurationInterface::class
$container->register(
  new \Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider()
);
/// .............
$app->pipe(
  $container->get(\Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class)
)

Now in every following Middleware:

$session = $request->getAttribute(\Interop\Session\SessionInterface::class);
$session->set("foo", "baz");
echo $session->get("foo"); // print baz

Using Aura.DI

$factory = "\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddlewareFactory";
$name = "\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddleware";
$container->set($factory, $container->lazyNew($factory));
$container->set($name, $container->lazyGetCall($factory, '__invoke', $container));

/// .......

$app->pipe($container->get("\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddleware"));