Mercurial > hg > cmmr2012-drupal-site
comparison core/lib/Drupal/Core/Session/WriteSafeSessionHandler.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\Core\Session; | 3 namespace Drupal\Core\Session; |
4 | 4 |
5 /** | 5 /** |
6 * Wraps another SessionHandlerInterface to prevent writes when not allowed. | 6 * Wraps the session handler to prevent writes when not necessary or allowed. |
7 */ | 7 */ |
8 class WriteSafeSessionHandler implements \SessionHandlerInterface, WriteSafeSessionHandlerInterface { | 8 class WriteSafeSessionHandler implements \SessionHandlerInterface, WriteSafeSessionHandlerInterface { |
9 | 9 |
10 /** | 10 /** |
11 * @var \SessionHandlerInterface | 11 * @var \SessionHandlerInterface |
16 * Whether or not the session is enabled for writing. | 16 * Whether or not the session is enabled for writing. |
17 * | 17 * |
18 * @var bool | 18 * @var bool |
19 */ | 19 */ |
20 protected $sessionWritable; | 20 protected $sessionWritable; |
21 | |
22 /** | |
23 * The read sessions. | |
24 * | |
25 * @var array | |
26 * Session data keyed by the session ID. | |
27 */ | |
28 private $readSessions; | |
21 | 29 |
22 /** | 30 /** |
23 * Constructs a new write safe session handler. | 31 * Constructs a new write safe session handler. |
24 * | 32 * |
25 * @param \SessionHandlerInterface $wrapped_session_handler | 33 * @param \SessionHandlerInterface $wrapped_session_handler |
62 | 70 |
63 /** | 71 /** |
64 * {@inheritdoc} | 72 * {@inheritdoc} |
65 */ | 73 */ |
66 public function read($session_id) { | 74 public function read($session_id) { |
67 return $this->wrappedSessionHandler->read($session_id); | 75 $value = $this->wrappedSessionHandler->read($session_id); |
76 $this->readSessions[$session_id] = $value; | |
77 return $value; | |
68 } | 78 } |
69 | 79 |
70 /** | 80 /** |
71 * {@inheritdoc} | 81 * {@inheritdoc} |
72 */ | 82 */ |
73 public function write($session_id, $session_data) { | 83 public function write($session_id, $session_data) { |
84 // Only write the session when it has been modified. | |
85 if (isset($this->readSessions[$session_id]) && $this->readSessions[$session_id] === $session_data) { | |
86 return TRUE; | |
87 } | |
74 if ($this->isSessionWritable()) { | 88 if ($this->isSessionWritable()) { |
75 return $this->wrappedSessionHandler->write($session_id, $session_data); | 89 return $this->wrappedSessionHandler->write($session_id, $session_data); |
76 } | 90 } |
77 else { | 91 return TRUE; |
78 return TRUE; | |
79 } | |
80 } | 92 } |
81 | 93 |
82 /** | 94 /** |
83 * {@inheritdoc} | 95 * {@inheritdoc} |
84 */ | 96 */ |