StrictSessionHandler.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. protected function doRead(#[\SensitiveParameter] string $sessionId): string
  42. {
  43. return $this->handler->read($sessionId);
  44. }
  45. public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string $data): bool
  46. {
  47. return $this->write($sessionId, $data);
  48. }
  49. protected function doWrite(#[\SensitiveParameter] string $sessionId, string $data): bool
  50. {
  51. return $this->handler->write($sessionId, $data);
  52. }
  53. public function destroy(#[\SensitiveParameter] string $sessionId): bool
  54. {
  55. $this->doDestroy = true;
  56. $destroyed = parent::destroy($sessionId);
  57. return $this->doDestroy ? $this->doDestroy($sessionId) : $destroyed;
  58. }
  59. protected function doDestroy(#[\SensitiveParameter] string $sessionId): bool
  60. {
  61. $this->doDestroy = false;
  62. return $this->handler->destroy($sessionId);
  63. }
  64. public function close(): bool
  65. {
  66. return $this->handler->close();
  67. }
  68. public function gc(int $maxlifetime): int|false
  69. {
  70. return $this->handler->gc($maxlifetime);
  71. }
  72. }