Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\StackMiddleware;
|
Chris@0
|
4
|
Chris@0
|
5 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
Chris@0
|
6 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
7 use Symfony\Component\HttpKernel\HttpKernelInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Wrap session logic around a HTTP request.
|
Chris@0
|
11 *
|
Chris@0
|
12 * Note, the session service is not injected into this class in order to prevent
|
Chris@0
|
13 * premature initialization of session storage (database). Instead the session
|
Chris@0
|
14 * service is retrieved from the container only when handling the request.
|
Chris@0
|
15 */
|
Chris@0
|
16 class Session implements HttpKernelInterface {
|
Chris@0
|
17
|
Chris@0
|
18 use ContainerAwareTrait;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The wrapped HTTP kernel.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $httpKernel;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The session service name.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var string
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $sessionServiceName;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Constructs a Session stack middleware object.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
|
Chris@0
|
38 * The decorated kernel.
|
Chris@0
|
39 * @param string $service_name
|
Chris@0
|
40 * The name of the session service, defaults to "session".
|
Chris@0
|
41 */
|
Chris@0
|
42 public function __construct(HttpKernelInterface $http_kernel, $service_name = 'session') {
|
Chris@0
|
43 $this->httpKernel = $http_kernel;
|
Chris@0
|
44 $this->sessionServiceName = $service_name;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * {@inheritdoc}
|
Chris@0
|
49 */
|
Chris@0
|
50 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
|
Chris@0
|
51 if ($type === self::MASTER_REQUEST && PHP_SAPI !== 'cli') {
|
Chris@0
|
52 $session = $this->container->get($this->sessionServiceName);
|
Chris@0
|
53 $session->start();
|
Chris@0
|
54 $request->setSession($session);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 $result = $this->httpKernel->handle($request, $type, $catch);
|
Chris@0
|
58
|
Chris@0
|
59 if ($type === self::MASTER_REQUEST && $request->hasSession()) {
|
Chris@0
|
60 $request->getSession()->save();
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 return $result;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 }
|