StrictSessionHandler.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * Adds basic `SessionUpdateTimestampHandlerInterface` behaviors to another `SessionHandlerInterface`.
  13. *
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class StrictSessionHandler extends AbstractSessionHandler
  17. {
  18. private $handler;
  19. private $doDestroy;
  20. public function __construct(\SessionHandlerInterface $handler)
  21. {
  22. if ($handler instanceof \SessionUpdateTimestampHandlerInterface) {
  23. throw new \LogicException(sprintf('"%s" is already an instance of "SessionUpdateTimestampHandlerInterface", you cannot wrap it with "%s".', get_debug_type($handler), self::class));
  24. }
  25. $this->handler = $handler;
  26. }
  27. /**
  28. * @return bool
  29. */
  30. public function open($savePath, $sessionName)
  31. {
  32. parent::open($savePath, $sessionName);
  33. return $this->handler->open($savePath, $sessionName);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function doRead(string $sessionId)
  39. {
  40. return $this->handler->read($sessionId);
  41. }
  42. /**
  43. * @return bool
  44. */
  45. public function updateTimestamp($sessionId, $data)
  46. {
  47. return $this->write($sessionId, $data);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function doWrite(string $sessionId, string $data)
  53. {
  54. return $this->handler->write($sessionId, $data);
  55. }
  56. /**
  57. * @return bool
  58. */
  59. public function destroy($sessionId)
  60. {
  61. $this->doDestroy = true;
  62. $destroyed = parent::destroy($sessionId);
  63. return $this->doDestroy ? $this->doDestroy($sessionId) : $destroyed;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function doDestroy(string $sessionId)
  69. {
  70. $this->doDestroy = false;
  71. return $this->handler->destroy($sessionId);
  72. }
  73. /**
  74. * @return bool
  75. */
  76. public function close()
  77. {
  78. return $this->handler->close();
  79. }
  80. /**
  81. * @return bool
  82. */
  83. public function gc($maxlifetime)
  84. {
  85. return $this->handler->gc($maxlifetime);
  86. }
  87. }