WriteCheckSessionHandler.php 2.0 KB

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