session-interop / middleware.async
This package contains a zend's middleware to be able to inject a Session Object into the request
Installs: 52 633
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 2
Open Issues: 1
Requires
- php: ^7.1
- container-interop/container-interop: ^1.0
- container-interop/service-provider: ~0.3.0
- psr/http-message: ^1.0
- session-interop/session-factory-interop: ^1.0
- session-interop/session-interop: ^3.0
- session-interop/session-manager-interop: ^1.0
- session-interop/utils.arraysession.savable: ^1.0
- session-interop/utils.defaultmanager: ^1.0
- thecodingmachine/middleware-list-universal-module: ~1.0
This package is not auto-updated.
Last update: 2024-11-09 20:39:42 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:
- 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.
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
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
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"));