WriteCheckSessionHandler.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. private $wrappedSessionHandler;
  19. /**
  20. * @var array sessionId => session
  21. */
  22. private $readSessions;
  23. public function __construct(\SessionHandlerInterface $wrappedSessionHandler)
  24. {
  25. $this->wrappedSessionHandler = $wrappedSessionHandler;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function close()
  31. {
  32. return $this->wrappedSessionHandler->close();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function destroy($sessionId)
  38. {
  39. return $this->wrappedSessionHandler->destroy($sessionId);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function gc($maxlifetime)
  45. {
  46. return $this->wrappedSessionHandler->gc($maxlifetime);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function open($savePath, $sessionName)
  52. {
  53. return $this->wrappedSessionHandler->open($savePath, $sessionName);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function read($sessionId)
  59. {
  60. $session = $this->wrappedSessionHandler->read($sessionId);
  61. $this->readSessions[$sessionId] = $session;
  62. return $session;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function write($sessionId, $data)
  68. {
  69. if (isset($this->readSessions[$sessionId]) && $data === $this->readSessions[$sessionId]) {
  70. return true;
  71. }
  72. return $this->wrappedSessionHandler->write($sessionId, $data);
  73. }
  74. }