Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / StackMiddleware / Session.php
1 <?php
2
3 namespace Drupal\Core\StackMiddleware;
4
5 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\HttpKernel\HttpKernelInterface;
8
9 /**
10  * Wrap session logic around a HTTP request.
11  *
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.
15  */
16 class Session implements HttpKernelInterface {
17
18   use ContainerAwareTrait;
19
20   /**
21    * The wrapped HTTP kernel.
22    *
23    * @var \Symfony\Component\HttpKernel\HttpKernelInterface
24    */
25   protected $httpKernel;
26
27   /**
28    * The session service name.
29    *
30    * @var string
31    */
32   protected $sessionServiceName;
33
34   /**
35    * Constructs a Session stack middleware object.
36    *
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".
41    */
42   public function __construct(HttpKernelInterface $http_kernel, $service_name = 'session') {
43     $this->httpKernel = $http_kernel;
44     $this->sessionServiceName = $service_name;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
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);
53       $session->start();
54       $request->setSession($session);
55     }
56
57     $result = $this->httpKernel->handle($request, $type, $catch);
58
59     if ($type === self::MASTER_REQUEST && $request->hasSession()) {
60       $request->getSession()->save();
61     }
62
63     return $result;
64   }
65
66 }