WriteCheckSessionHandler.php 2.3 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. * @deprecated since version 3.4, to be removed in 4.0. Implement `SessionUpdateTimestampHandlerInterface` or extend `AbstractSessionHandler` instead.
  17. */
  18. class WriteCheckSessionHandler implements \SessionHandlerInterface
  19. {
  20. private $wrappedSessionHandler;
  21. /**
  22. * @var array sessionId => session
  23. */
  24. private $readSessions;
  25. public function __construct(\SessionHandlerInterface $wrappedSessionHandler)
  26. {
  27. @trigger_error(sprintf('The %s class is deprecated since Symfony 3.4 and will be removed in 4.0. Implement `SessionUpdateTimestampHandlerInterface` or extend `AbstractSessionHandler` instead.', self::class), E_USER_DEPRECATED);
  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. }