3 namespace Drupal\Core\StackMiddleware;
5 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\HttpKernel\HttpKernelInterface;
10 * Wrap session logic around a HTTP request.
12 * Note, the session service is not injected into this class in order to prevent
13 * premature initialization of session storage (database). Instead the session
14 * service is retrieved from the container only when handling the request.
16 class Session implements HttpKernelInterface {
18 use ContainerAwareTrait;
21 * The wrapped HTTP kernel.
23 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
25 protected $httpKernel;
28 * The session service name.
32 protected $sessionServiceName;
35 * Constructs a Session stack middleware object.
37 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
38 * The decorated kernel.
39 * @param string $service_name
40 * The name of the session service, defaults to "session".
42 public function __construct(HttpKernelInterface $http_kernel, $service_name = 'session') {
43 $this->httpKernel = $http_kernel;
44 $this->sessionServiceName = $service_name;
50 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
51 if ($type === self::MASTER_REQUEST && PHP_SAPI !== 'cli') {
52 $session = $this->container->get($this->sessionServiceName);
54 $request->setSession($session);
57 $result = $this->httpKernel->handle($request, $type, $catch);
59 if ($type === self::MASTER_REQUEST && $request->hasSession()) {
60 $request->getSession()->save();