StrictSessionHandler.php 2.4 KB

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