StrictSessionHandler.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 bool $doDestroy;
  19. public function __construct(
  20. private \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. }
  26. /**
  27. * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
  28. *
  29. * @internal
  30. */
  31. public function isWrapper(): bool
  32. {
  33. return $this->handler instanceof \SessionHandler;
  34. }
  35. public function open(string $savePath, string $sessionName): bool
  36. {
  37. parent::open($savePath, $sessionName);
  38. return $this->handler->open($savePath, $sessionName);
  39. }
  40. protected function doRead(#[\SensitiveParameter] string $sessionId): string
  41. {
  42. return $this->handler->read($sessionId);
  43. }
  44. public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string $data): bool
  45. {
  46. return $this->write($sessionId, $data);
  47. }
  48. protected function doWrite(#[\SensitiveParameter] string $sessionId, string $data): bool
  49. {
  50. return $this->handler->write($sessionId, $data);
  51. }
  52. public function destroy(#[\SensitiveParameter] string $sessionId): bool
  53. {
  54. $this->doDestroy = true;
  55. $destroyed = parent::destroy($sessionId);
  56. return $this->doDestroy ? $this->doDestroy($sessionId) : $destroyed;
  57. }
  58. protected function doDestroy(#[\SensitiveParameter] string $sessionId): bool
  59. {
  60. $this->doDestroy = false;
  61. return $this->handler->destroy($sessionId);
  62. }
  63. public function close(): bool
  64. {
  65. return $this->handler->close();
  66. }
  67. public function gc(int $maxlifetime): int|false
  68. {
  69. return $this->handler->gc($maxlifetime);
  70. }
  71. }