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:
- This middleware open the session (by calling
session_start
) if needed to copy it to create an instance extending theArraySession
then close it immediatly (by callingsession_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. - 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.
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.
This middleware is designed to be used as a zend-stratigility middleware.
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
zend-expressive
and a container compatible with- A
container-interop
as dependencies container
###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
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
$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"));