Repository URL to install this package:
|
Version:
2.1.0 ▾
|
<?php
namespace Drupal\dds\Service;
use Symfony\Component\HttpFoundation\Request;
class SessionService {
/**
* @var \Drupal\Core\TempStore\PrivateTempStore $privateTempStore
* The private temp store.
*/
protected $privateTempStore;
/**
* @var \Drupal\Core\TempStore\SharedTempStore $sharedTempStore
* The shared temp store.
*/
protected $sharedTempStore;
/**
* Get the temp store.
*
* This function allows you to get a tempstore, even for anonymous users if
* you make sure that autostart session is allowed.
*
* @param string $type
* The tempstore type, can be either 'private' or 'shared'.
* @param string $key
* The key for the tempstore to get.
* @param bool $autostart_session
* (Optional, Default: True) Whether to start a session if one doesn't already exist.
*
* @return \Drupal\Core\TempStore\PrivateTempStore|\Drupal\Core\TempStore\SharedTempStore|bool
* Returns the tempstore if successful, otherwise returns FALSE.
*/
public function getTempStore($type, $key, $autostart_session = TRUE) {
// If the type specified is not private or shared, we return FALSE
// to indicate we failed at getting the temp store.
if (!in_array($type, ['private', 'shared'])) {
return FALSE;
}
if ($autostart_session) {
$this->startSession();
}
// Generate the property key.
$tempstore_key = $type.'TempStore';
// Generate the service name.
$tempstore_service = 'user.' . $type . '_tempstore';
/** @var \Drupal\Core\TempStore\PrivateTempStoreFactory|\Drupal\Core\TempStore\SharedTempStoreFactory $tempStoreFactory */
$tempStoreFactory = \Drupal::service($tempstore_service);
// If the tempstore is null, attempt to load it.
if ($this->{$tempstore_key} === NULL) {
$this->{$tempstore_key} = $tempStoreFactory->get($key);
}
// Return the tempstore if it exists, otherwise return FALSE.
return !empty($this->{$tempstore_key})? $this->{$tempstore_key} : FALSE;
}
/**
* Starts a session for the current request.
*
* If this function is called from CLI, it does not start a session.
*/
public function startSession() {
// If this is reached via PHP CLI, just return as it has no session.
if (php_sapi_name() === 'cli') {
return;
}
// We only need to handle creating sessions for anonymous users.
if (\Drupal::currentUser()->isAnonymous()) {
/** @var \Drupal\Core\Session\SessionManagerInterface $sessionManager */
$sessionManager = \Drupal::service('session_manager');
// Add a value to the session to make sure browsers garbage collection
// doesn't remove it if it's empty.
$_SESSION['dds_keep_session'] = TRUE;
/** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
$requestStack = \Drupal::service('request_stack');
// Only create session if the current request is null, as a session should
// already exist if a current request exists.
if ($requestStack->getCurrentRequest() === NULL) {
/** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
$session = \Drupal::service('session');
// If session isn't started, migrate it to a new session so we keep all
// our session variables.
if (!$session->isStarted()) {
$session->migrate();
}
// Create a new request from the globals.
$request = Request::createFromGlobals();
// Set the request session to the one we are using.
$request->setSession($session);
}
// Start the session.
$sessionManager->start();
}
}
}