1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <[email protected]>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
13:
14: /**
15: * Wraps another SessionHandlerInterface to only write the session when it has been modified.
16: *
17: * @author Adrien Brault <[email protected]>
18: */
19: class WriteCheckSessionHandler implements \SessionHandlerInterface
20: {
21: /**
22: * @var \SessionHandlerInterface
23: */
24: private $wrappedSessionHandler;
25:
26: /**
27: * @var array sessionId => session
28: */
29: private $readSessions;
30:
31: public function __construct(\SessionHandlerInterface $wrappedSessionHandler)
32: {
33: $this->wrappedSessionHandler = $wrappedSessionHandler;
34: }
35:
36: /**
37: * {@inheritdoc}
38: */
39: public function close()
40: {
41: return $this->wrappedSessionHandler->close();
42: }
43:
44: /**
45: * {@inheritdoc}
46: */
47: public function destroy($sessionId)
48: {
49: return $this->wrappedSessionHandler->destroy($sessionId);
50: }
51:
52: /**
53: * {@inheritdoc}
54: */
55: public function gc($maxlifetime)
56: {
57: return $this->wrappedSessionHandler->gc($maxlifetime);
58: }
59:
60: /**
61: * {@inheritdoc}
62: */
63: public function open($savePath, $sessionName)
64: {
65: return $this->wrappedSessionHandler->open($savePath, $sessionName);
66: }
67:
68: /**
69: * {@inheritdoc}
70: */
71: public function read($sessionId)
72: {
73: $session = $this->wrappedSessionHandler->read($sessionId);
74:
75: $this->readSessions[$sessionId] = $session;
76:
77: return $session;
78: }
79:
80: /**
81: * {@inheritdoc}
82: */
83: public function write($sessionId, $data)
84: {
85: if (isset($this->readSessions[$sessionId]) && $data === $this->readSessions[$sessionId]) {
86: return true;
87: }
88:
89: return $this->wrappedSessionHandler->write($sessionId, $data);
90: }
91: }
92: