class WriteSafeSessionHandler

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Session/WriteSafeSessionHandler.php \Drupal\Core\Session\WriteSafeSessionHandler
  2. 8.9.x core/lib/Drupal/Core/Session/WriteSafeSessionHandler.php \Drupal\Core\Session\WriteSafeSessionHandler
  3. 10 core/lib/Drupal/Core/Session/WriteSafeSessionHandler.php \Drupal\Core\Session\WriteSafeSessionHandler

Wraps the session handler to prevent writes when not necessary or allowed.

Hierarchy

Expanded class hierarchy of WriteSafeSessionHandler

1 file declares its use of WriteSafeSessionHandler
WriteSafeSessionHandlerTest.php in core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php
1 string reference to 'WriteSafeSessionHandler'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses WriteSafeSessionHandler
session_handler.write_safe in core/core.services.yml
Drupal\Core\Session\WriteSafeSessionHandler

File

core/lib/Drupal/Core/Session/WriteSafeSessionHandler.php, line 10

Namespace

Drupal\Core\Session
View source
class WriteSafeSessionHandler extends SessionHandlerProxy implements \SessionHandlerInterface, WriteSafeSessionHandlerInterface, \SessionUpdateTimestampHandlerInterface {
  
  /**
   * Whether or not the session is enabled for writing.
   *
   * @var bool
   */
  protected $sessionWritable;
  
  /**
   * The read sessions.
   *
   * @var array
   *   Session data keyed by the session ID.
   */
  private $readSessions;
  
  /**
   * Constructs a new write safe session handler.
   *
   * @param \SessionHandlerInterface $handler
   *   The underlying session handler.
   * @param bool $session_writable
   *   Whether or not the session should be initially writable.
   */
  public function __construct(\SessionHandlerInterface $handler, $session_writable = TRUE) {
    parent::__construct($handler);
    $this->sessionWritable = $session_writable;
  }
  
  /**
   * {@inheritdoc}
   */
  public function read(#[\SensitiveParameter] string $session_id) : string {
    $value = $this->handler
      ->read($session_id);
    $this->readSessions[$session_id] = $value;
    return $value;
  }
  
  /**
   * {@inheritdoc}
   */
  public function write(#[\SensitiveParameter] string $session_id, string $session_data) : bool {
    // Only write the session when it has been modified.
    if (isset($this->readSessions[$session_id]) && $this->readSessions[$session_id] === $session_data) {
      return TRUE;
    }
    if ($this->isSessionWritable()) {
      return $this->handler
        ->write($session_id, $session_data);
    }
    return TRUE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function setSessionWritable($flag) {
    $this->sessionWritable = (bool) $flag;
  }
  
  /**
   * {@inheritdoc}
   */
  public function isSessionWritable() {
    return $this->sessionWritable;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
WriteSafeSessionHandler::$readSessions private property The read sessions.
WriteSafeSessionHandler::$sessionWritable protected property Whether or not the session is enabled for writing.
WriteSafeSessionHandler::isSessionWritable public function Returns whether or not a session may be written to storage. Overrides WriteSafeSessionHandlerInterface::isSessionWritable
WriteSafeSessionHandler::read public function
WriteSafeSessionHandler::setSessionWritable public function Sets whether or not a session may be written to storage. Overrides WriteSafeSessionHandlerInterface::setSessionWritable
WriteSafeSessionHandler::write public function
WriteSafeSessionHandler::__construct public function Constructs a new write safe session handler.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.