Commit 61de4962 authored by webchick's avatar webchick

Issue #1987572 by mrded, vijaycs85, Sean Buscay: Convert session_test()...

Issue #1987572 by mrded, vijaycs85, Sean Buscay: Convert session_test() callbacks to a new style controller.
parent 44e53a92
<?php
/**
* @file
* Contains \Drupal\session_test\Controller\SessionTestController.
*/
namespace Drupal\session_test\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller providing page callbacks for the action admin interface.
*/
class SessionTestController extends ControllerBase {
/**
* Prints the stored session value to the screen.
*
* @return string
* A notification message.
*/
public function get() {
return empty($_SESSION['session_test_value'])
? ""
: $this->t('The current value of the stored session variable is: %val', array('%val' => $_SESSION['session_test_value']));
}
/**
* Print the current session ID.
*
* @return string
* A notification message with session ID.
*/
public function getId() {
// Set a value in $_SESSION, so that drupal_session_commit() will start
// a session.
$_SESSION['test'] = 'test';
drupal_session_commit();
return 'session_id:' . session_id() . "\n";
}
/**
* Print the current session ID as read from the cookie.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*
* @return string
* A notification message with session ID.
*/
public function getIdFromCookie(Request $request) {
return 'session_id:' . $request->cookies->get(session_name()) . "\n";
}
/**
* Stores a value in $_SESSION['session_test_value'].
*
* @param string $test_value
* A session value.
*
* @return string
* A notification message.
*/
public function set($test_value) {
$_SESSION['session_test_value'] = $test_value;
return $this->t('The current value of the stored session variable has been set to %val', array('%val' => $test_value));
}
/**
* Turns off session saving and then tries to save a value
* anyway.
*
* @param string $test_value
* A session value.
*
* @return string
* A notification message.
*/
public function noSet($test_value) {
drupal_save_session(FALSE);
$this->set($test_value);
return $this->t('session saving was disabled, and then %val was set', array('%val' => $test_value));
}
/**
* Sets a message to me displayed on the following page.
*
* @return string
* A notification message.
*/
public function setMessage() {
drupal_set_message($this->t('This is a dummy message.'));
return new Response($this->t('A message was set.'));
// Do not return anything, so the current request does not result in a themed
// page with messages. The message will be displayed in the following request
// instead.
}
/**
* Sets a message but call drupal_save_session(FALSE).
*
* @return string
* A notification message.
*/
public function setMessageButDontSave() {
drupal_save_session(FALSE);
$this->setMessage();
}
/**
* Stores a value in $_SESSION['session_test_value'] without
* having started the session in advance.
*
* @return string
* A notification message.
*/
public function setNotStarted() {
if (!drupal_session_will_start()) {
$this->set($this->t('Session was not started'));
}
}
/**
* Only available if current user is logged in.
*
* @return string
* A notification message.
*/
public function isLoggedIn() {
return $this->t('User is logged in.');
}
}
<?php
/**
* Implements hook_menu().
*/
function session_test_menu() {
$items['session-test/get'] = array(
'title' => 'Session value',
'page callback' => '_session_test_get',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/id'] = array(
'title' => 'Session ID',
'page callback' => '_session_test_id',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/id-from-cookie'] = array(
'title' => 'Session ID from cookie',
'page callback' => '_session_test_id_from_cookie',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/set/%'] = array(
'title' => 'Set session value',
'page callback' => '_session_test_set',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/no-set/%'] = array(
'title' => 'Set session value but do not save session',
'page callback' => '_session_test_no_set',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/set-message'] = array(
'title' => 'Set message',
'page callback' => '_session_test_set_message',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/set-message-but-dont-save'] = array(
'title' => 'Set message but do not save session',
'page callback' => '_session_test_set_message_but_dont_save',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/set-not-started'] = array(
'title' => 'Set message when session is not started',
'page callback' => '_session_test_set_not_started',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['session-test/is-logged-in'] = array(
'title' => 'Check if user is logged in',
'page callback' => '_session_test_is_logged_in',
'access callback' => 'user_is_logged_in',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback, prints the stored session value to the screen.
*/
function _session_test_get() {
if (!empty($_SESSION['session_test_value'])) {
return t('The current value of the stored session variable is: %val', array('%val' => $_SESSION['session_test_value']));
}
else {
return "";
}
}
/**
* Page callback, stores a value in $_SESSION['session_test_value'].
*/
function _session_test_set($value) {
$_SESSION['session_test_value'] = $value;
return t('The current value of the stored session variable has been set to %val', array('%val' => $value));
}
/**
* Menu callback: turns off session saving and then tries to save a value
* anyway.
*/
function _session_test_no_set($value) {
drupal_save_session(FALSE);
_session_test_set($value);
return t('session saving was disabled, and then %val was set', array('%val' => $value));
}
/**
* Menu callback: print the current session ID.
*/
function _session_test_id() {
// Set a value in $_SESSION, so that drupal_session_commit() will start
// a session.
$_SESSION['test'] = 'test';
drupal_session_commit();
return 'session_id:' . session_id() . "\n";
}
/**
* Menu callback: print the current session ID as read from the cookie.
*/
function _session_test_id_from_cookie() {
return 'session_id:' . $_COOKIE[session_name()] . "\n";
}
/**
* Menu callback, sets a message to me displayed on the following page.
*/
function _session_test_set_message() {
drupal_set_message(t('This is a dummy message.'));
print t('A message was set.');
// Do not return anything, so the current request does not result in a themed
// page with messages. The message will be displayed in the following request
// instead.
}
/**
* Menu callback, sets a message but call drupal_save_session(FALSE).
*/
function _session_test_set_message_but_dont_save() {
drupal_save_session(FALSE);
_session_test_set_message();
}
/**
* Menu callback, stores a value in $_SESSION['session_test_value'] without
* having started the session in advance.
*/
function _session_test_set_not_started() {
if (!drupal_session_will_start()) {
$_SESSION['session_test_value'] = t('Session was not started');
}
}
/**
* Implements hook_user_login().
*/
......@@ -160,10 +17,3 @@ function session_test_user_login($account) {
function session_test_form_user_login_form_alter(&$form) {
$form['#https'] = TRUE;
}
/**
* Menu callback, only available if current user is logged in.
*/
function _session_test_is_logged_in() {
return t('User is logged in.');
}
session_test_get:
pattern: '/session-test/get'
defaults:
_title: 'Session value'
_controller: '\Drupal\session_test\Controller\SessionTestController::get'
requirements:
_permission: 'access content'
session_test_id:
pattern: '/session-test/id'
defaults:
_title: 'Session ID'
_controller: '\Drupal\session_test\Controller\SessionTestController::getId'
requirements:
_permission: 'access content'
session_test_id_from_cookie:
pattern: '/session-test/id-from-cookie'
defaults:
_title: 'Session ID from cookie'
_controller: '\Drupal\session_test\Controller\SessionTestController::getIdFromCookie'
requirements:
_permission: 'access content'
session_test_set:
pattern: '/session-test/set/{test_value}'
defaults:
_title: 'Set session value'
_controller: '\Drupal\session_test\Controller\SessionTestController::set'
options:
converters:
test_value: '\s+'
requirements:
_permission: 'access content'
session_test_no_set:
pattern: '/session-test/no-set/{test_value}'
defaults:
_title: 'Set session value but do not save session'
_controller: '\Drupal\session_test\Controller\SessionTestController::noSet'
options:
converters:
test_value: '\s+'
requirements:
_permission: 'access content'
session_test_set_message:
pattern: '/session-test/set-message'
defaults:
_title: 'Set message'
_controller: '\Drupal\session_test\Controller\SessionTestController::setMessage'
requirements:
_permission: 'access content'
session_test_set_message_but_dont_save:
pattern: '/session-test/set-message-but-dont-save'
defaults:
_title: 'Set message but do not save session'
_controller: '\Drupal\session_test\Controller\SessionTestController::setMessageButDontSave'
requirements:
_permission: 'access content'
session_test_set_not_started:
pattern: '/session-test/set-not-started'
defaults:
_title: 'Set message when session is not started'
_controller: '\Drupal\session_test\Controller\SessionTestController::setNotStarted'
requirements:
_permission: 'access content'
session_test_is_logged_in:
pattern: '/session-test/is-logged-in'
defaults:
_title: 'Check if user is logged in'
_controller: '\Drupal\session_test\Controller\SessionTestController::isLoggedIn'
requirements:
_user_is_logged_in: 'TRUE'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment