菜谱项目

WriteCheckSessionHandler.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. }