1<?php
2
3namespace Drupal\session_test\Controller;
4
5use Drupal\Core\Controller\ControllerBase;
6use Drupal\session_test\Session\TestSessionBag;
7use Symfony\Component\HttpFoundation\JsonResponse;
8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpFoundation\Response;
10
11/**
12 * Controller providing page callbacks for the action admin interface.
13 */
14class SessionTestController extends ControllerBase {
15
16  /**
17   * Prints the stored session value to the screen.
18   *
19   * @return string
20   *   A notification message.
21   */
22  public function get() {
23    return empty($_SESSION['session_test_value'])
24      ? []
25      : ['#markup' => $this->t('The current value of the stored session variable is: %val', ['%val' => $_SESSION['session_test_value']])];
26  }
27
28  /**
29   * Prints the stored session value to the screen.
30   *
31   * @param \Symfony\Component\HttpFoundation\Request $request
32   *   The incoming request.
33   *
34   * @return string
35   *   A notification message.
36   */
37  public function getFromSessionObject(Request $request) {
38    $value = $request->getSession()->get("session_test_key");
39    return empty($value)
40      ? []
41      : ['#markup' => $this->t('The current value of the stored session variable is: %val', ['%val' => $value])];
42  }
43
44  /**
45   * Print the current session ID.
46   *
47   * @param \Symfony\Component\HttpFoundation\Request $request
48   *   The incoming request.
49   *
50   * @return string
51   *   A notification message with session ID.
52   */
53  public function getId(Request $request) {
54    // Set a value in $_SESSION, so that SessionManager::save() will start
55    // a session.
56    $_SESSION['test'] = 'test';
57
58    $request->getSession()->save();
59
60    return ['#markup' => 'session_id:' . session_id() . "\n"];
61  }
62
63  /**
64   * Print the current session ID as read from the cookie.
65   *
66   * @param \Symfony\Component\HttpFoundation\Request $request
67   *   The request object.
68   *
69   * @return string
70   *   A notification message with session ID.
71   */
72  public function getIdFromCookie(Request $request) {
73    return ['#markup' => 'session_id:' . $request->cookies->get(session_name()) . "\n", '#cache' => ['contexts' => ['cookies:' . session_name()]]];
74  }
75
76  /**
77   * Stores a value in $_SESSION['session_test_value'].
78   *
79   * @param string $test_value
80   *   A session value.
81   *
82   * @return string
83   *   A notification message.
84   */
85  public function set($test_value) {
86    $_SESSION['session_test_value'] = $test_value;
87
88    return ['#markup' => $this->t('The current value of the stored session variable has been set to %val', ['%val' => $test_value])];
89  }
90
91  /**
92   * Turns off session saving and then tries to save a value
93   * anyway.
94   *
95   * @param string $test_value
96   *   A session value.
97   *
98   * @return string
99   *   A notification message.
100   */
101  public function noSet($test_value) {
102    \Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
103    $this->set($test_value);
104    return ['#markup' => $this->t('session saving was disabled, and then %val was set', ['%val' => $test_value])];
105  }
106
107  /**
108   * Sets a message to me displayed on the following page.
109   *
110   * @return string
111   *   A notification message.
112   */
113  public function setMessage() {
114    $this->messenger()->addStatus($this->t('This is a dummy message.'));
115    return new Response($this->t('A message was set.'));
116    // Do not return anything, so the current request does not result in a themed
117    // page with messages. The message will be displayed in the following request
118    // instead.
119  }
120
121  /**
122   * Sets a message but call drupal_save_session(FALSE).
123   *
124   * @return string
125   *   A notification message.
126   */
127  public function setMessageButDoNotSave() {
128    \Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
129    $this->setMessage();
130    return ['#markup' => ''];
131  }
132
133  /**
134   * Only available if current user is logged in.
135   *
136   * @return string
137   *   A notification message.
138   */
139  public function isLoggedIn() {
140    return ['#markup' => $this->t('User is logged in.')];
141  }
142
143  /**
144   * Returns the trace recorded by test proxy session handlers as JSON.
145   *
146   * @param \Symfony\Component\HttpFoundation\Request $request
147   *   The incoming request.
148   *
149   * @return \Symfony\Component\HttpFoundation\JsonResponse
150   *   The response.
151   */
152  public function traceHandler(Request $request) {
153    // Start a session if necessary, set a value and then save and close it.
154    $request->getSession()->start();
155    if (empty($_SESSION['trace-handler'])) {
156      $_SESSION['trace-handler'] = 1;
157    }
158    else {
159      $_SESSION['trace-handler']++;
160    }
161    $request->getSession()->save();
162
163    // Collect traces and return them in JSON format.
164    $trace = \Drupal::service('session_test.session_handler_proxy_trace')->getArrayCopy();
165
166    return new JsonResponse($trace);
167  }
168
169  /**
170   * Returns the values stored in the active session and the user ID.
171   *
172   * @param \Symfony\Component\HttpFoundation\Request $request
173   *   The request object.
174   *
175   * @return \Symfony\Component\HttpFoundation\JsonResponse
176   *   A response object containing the session values and the user ID.
177   */
178  public function getSession(Request $request) {
179    return new JsonResponse(['session' => $request->getSession()->all(), 'user' => $this->currentUser()->id()]);
180  }
181
182  /**
183   * Sets a test value on the session.
184   *
185   * @param \Symfony\Component\HttpFoundation\Request $request
186   *   The request object.
187   * @param string $test_value
188   *   A value to set on the session.
189   *
190   * @return \Symfony\Component\HttpFoundation\JsonResponse
191   *   A response object containing the session values and the user ID.
192   */
193  public function setSession(Request $request, $test_value) {
194    $session = $request->getSession();
195    $session->set('test_value', $test_value);
196    return new JsonResponse(['session' => $session->all(), 'user' => $this->currentUser()->id()]);
197  }
198
199  /**
200   * Sets the test flag in the session test bag.
201   *
202   * @param \Symfony\Component\HttpFoundation\Request $request
203   *   The request object.
204   *
205   * @return \Symfony\Component\HttpFoundation\Response
206   *   The response object.
207   */
208  public function setSessionBagFlag(Request $request) {
209    /** @var \Drupal\session_test\Session\TestSessionBag */
210    $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
211    $bag->setFlag();
212    return new Response();
213  }
214
215  /**
216   * Clears the test flag from the session test bag.
217   *
218   * @param \Symfony\Component\HttpFoundation\Request $request
219   *   The request object.
220   *
221   * @return \Symfony\Component\HttpFoundation\Response
222   *   The response object.
223   */
224  public function clearSessionBagFlag(Request $request) {
225    /** @var \Drupal\session_test\Session\TestSessionBag */
226    $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
227    $bag->clearFlag();
228    return new Response();
229  }
230
231  /**
232   * Prints a message if the flag in the session bag is set.
233   *
234   * @param \Symfony\Component\HttpFoundation\Request $request
235   *   The request object.
236   *
237   * @return \Symfony\Component\HttpFoundation\Response
238   *   The response object.
239   */
240  public function hasSessionBagFlag(Request $request) {
241    /** @var \Drupal\session_test\Session\TestSessionBag */
242    $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
243    return new Response(empty($bag->hasFlag())
244      ? $this->t('Flag is absent from session bag')
245      : $this->t('Flag is present in session bag')
246    );
247  }
248
249}
250