MigratingSessionHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * Migrating session handler for migrating from one handler to another. It reads
  13. * from the current handler and writes both the current and new ones.
  14. *
  15. * It ignores errors from the new handler.
  16. *
  17. * @author Ross Motley <ross.motley@amara.com>
  18. * @author Oliver Radwell <oliver.radwell@amara.com>
  19. */
  20. class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
  21. {
  22. /**
  23. * @var \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface
  24. */
  25. private \SessionHandlerInterface $currentHandler;
  26. /**
  27. * @var \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface
  28. */
  29. private \SessionHandlerInterface $writeOnlyHandler;
  30. public function __construct(\SessionHandlerInterface $currentHandler, \SessionHandlerInterface $writeOnlyHandler)
  31. {
  32. if (!$currentHandler instanceof \SessionUpdateTimestampHandlerInterface) {
  33. $currentHandler = new StrictSessionHandler($currentHandler);
  34. }
  35. if (!$writeOnlyHandler instanceof \SessionUpdateTimestampHandlerInterface) {
  36. $writeOnlyHandler = new StrictSessionHandler($writeOnlyHandler);
  37. }
  38. $this->currentHandler = $currentHandler;
  39. $this->writeOnlyHandler = $writeOnlyHandler;
  40. }
  41. public function close(): bool
  42. {
  43. $result = $this->currentHandler->close();
  44. $this->writeOnlyHandler->close();
  45. return $result;
  46. }
  47. public function destroy(string $sessionId): bool
  48. {
  49. $result = $this->currentHandler->destroy($sessionId);
  50. $this->writeOnlyHandler->destroy($sessionId);
  51. return $result;
  52. }
  53. public function gc(int $maxlifetime): int|false
  54. {
  55. $result = $this->currentHandler->gc($maxlifetime);
  56. $this->writeOnlyHandler->gc($maxlifetime);
  57. return $result;
  58. }
  59. public function open(string $savePath, string $sessionName): bool
  60. {
  61. $result = $this->currentHandler->open($savePath, $sessionName);
  62. $this->writeOnlyHandler->open($savePath, $sessionName);
  63. return $result;
  64. }
  65. public function read(string $sessionId): string
  66. {
  67. // No reading from new handler until switch-over
  68. return $this->currentHandler->read($sessionId);
  69. }
  70. public function write(string $sessionId, string $sessionData): bool
  71. {
  72. $result = $this->currentHandler->write($sessionId, $sessionData);
  73. $this->writeOnlyHandler->write($sessionId, $sessionData);
  74. return $result;
  75. }
  76. public function validateId(string $sessionId): bool
  77. {
  78. // No reading from new handler until switch-over
  79. return $this->currentHandler->validateId($sessionId);
  80. }
  81. public function updateTimestamp(string $sessionId, string $sessionData): bool
  82. {
  83. $result = $this->currentHandler->updateTimestamp($sessionId, $sessionData);
  84. $this->writeOnlyHandler->updateTimestamp($sessionId, $sessionData);
  85. return $result;
  86. }
  87. }