StrictSessionHandler.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 \SessionHandlerInterface $handler;
  19. private bool $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. public function open(string $savePath, string $sessionName): bool
  37. {
  38. parent::open($savePath, $sessionName);
  39. return $this->handler->open($savePath, $sessionName);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function doRead(string $sessionId): string
  45. {
  46. return $this->handler->read($sessionId);
  47. }
  48. public function updateTimestamp(string $sessionId, string $data): bool
  49. {
  50. return $this->write($sessionId, $data);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function doWrite(string $sessionId, string $data): bool
  56. {
  57. return $this->handler->write($sessionId, $data);
  58. }
  59. public function destroy(string $sessionId): bool
  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): bool
  69. {
  70. $this->doDestroy = false;
  71. return $this->handler->destroy($sessionId);
  72. }
  73. public function close(): bool
  74. {
  75. return $this->handler->close();
  76. }
  77. public function gc(int $maxlifetime): int|false
  78. {
  79. return $this->handler->gc($maxlifetime);
  80. }
  81. }