3 namespace Drupal\Core\Session;
6 * Wraps another SessionHandlerInterface to prevent writes when not allowed.
8 class WriteSafeSessionHandler implements \SessionHandlerInterface, WriteSafeSessionHandlerInterface {
11 * @var \SessionHandlerInterface
13 protected $wrappedSessionHandler;
16 * Whether or not the session is enabled for writing.
20 protected $sessionWritable;
23 * Constructs a new write safe session handler.
25 * @param \SessionHandlerInterface $wrapped_session_handler
26 * The underlying session handler.
27 * @param bool $session_writable
28 * Whether or not the session should be initially writable.
30 public function __construct(\SessionHandlerInterface $wrapped_session_handler, $session_writable = TRUE) {
31 $this->wrappedSessionHandler = $wrapped_session_handler;
32 $this->sessionWritable = $session_writable;
38 public function close() {
39 return $this->wrappedSessionHandler->close();
45 public function destroy($session_id) {
46 return $this->wrappedSessionHandler->destroy($session_id);
52 public function gc($max_lifetime) {
53 return $this->wrappedSessionHandler->gc($max_lifetime);
59 public function open($save_path, $session_id) {
60 return $this->wrappedSessionHandler->open($save_path, $session_id);
66 public function read($session_id) {
67 return $this->wrappedSessionHandler->read($session_id);
73 public function write($session_id, $session_data) {
74 if ($this->isSessionWritable()) {
75 return $this->wrappedSessionHandler->write($session_id, $session_data);
85 public function setSessionWritable($flag) {
86 $this->sessionWritable = (bool) $flag;
92 public function isSessionWritable() {
93 return $this->sessionWritable;