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