StrictSessionHandler.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
  29. *
  30. * @internal
  31. */
  32. public function isWrapper(): bool
  33. {
  34. return $this->handler instanceof \SessionHandler;
  35. }
  36. /**
  37. * @return bool
  38. */
  39. #[\ReturnTypeWillChange]
  40. public function open($savePath, $sessionName)
  41. {
  42. parent::open($savePath, $sessionName);
  43. return $this->handler->open($savePath, $sessionName);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function doRead(string $sessionId)
  49. {
  50. return $this->handler->read($sessionId);
  51. }
  52. /**
  53. * @return bool
  54. */
  55. #[\ReturnTypeWillChange]
  56. public function updateTimestamp($sessionId, $data)
  57. {
  58. return $this->write($sessionId, $data);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function doWrite(string $sessionId, string $data)
  64. {
  65. return $this->handler->write($sessionId, $data);
  66. }
  67. /**
  68. * @return bool
  69. */
  70. #[\ReturnTypeWillChange]
  71. public function destroy($sessionId)
  72. {
  73. $this->doDestroy = true;
  74. $destroyed = parent::destroy($sessionId);
  75. return $this->doDestroy ? $this->doDestroy($sessionId) : $destroyed;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function doDestroy(string $sessionId)
  81. {
  82. $this->doDestroy = false;
  83. return $this->handler->destroy($sessionId);
  84. }
  85. /**
  86. * @return bool
  87. */
  88. #[\ReturnTypeWillChange]
  89. public function close()
  90. {
  91. return $this->handler->close();
  92. }
  93. /**
  94. * @return int|false
  95. */
  96. #[\ReturnTypeWillChange]
  97. public function gc($maxlifetime)
  98. {
  99. return $this->handler->gc($maxlifetime);
  100. }
  101. }