MigratingSessionHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. private \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface $currentHandler;
  23. private \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface $writeOnlyHandler;
  24. public function __construct(\SessionHandlerInterface $currentHandler, \SessionHandlerInterface $writeOnlyHandler)
  25. {
  26. if (!$currentHandler instanceof \SessionUpdateTimestampHandlerInterface) {
  27. $currentHandler = new StrictSessionHandler($currentHandler);
  28. }
  29. if (!$writeOnlyHandler instanceof \SessionUpdateTimestampHandlerInterface) {
  30. $writeOnlyHandler = new StrictSessionHandler($writeOnlyHandler);
  31. }
  32. $this->currentHandler = $currentHandler;
  33. $this->writeOnlyHandler = $writeOnlyHandler;
  34. }
  35. public function close(): bool
  36. {
  37. $result = $this->currentHandler->close();
  38. $this->writeOnlyHandler->close();
  39. return $result;
  40. }
  41. public function destroy(#[\SensitiveParameter] string $sessionId): bool
  42. {
  43. $result = $this->currentHandler->destroy($sessionId);
  44. $this->writeOnlyHandler->destroy($sessionId);
  45. return $result;
  46. }
  47. public function gc(int $maxlifetime): int|false
  48. {
  49. $result = $this->currentHandler->gc($maxlifetime);
  50. $this->writeOnlyHandler->gc($maxlifetime);
  51. return $result;
  52. }
  53. public function open(string $savePath, string $sessionName): bool
  54. {
  55. $result = $this->currentHandler->open($savePath, $sessionName);
  56. $this->writeOnlyHandler->open($savePath, $sessionName);
  57. return $result;
  58. }
  59. public function read(#[\SensitiveParameter] string $sessionId): string
  60. {
  61. // No reading from new handler until switch-over
  62. return $this->currentHandler->read($sessionId);
  63. }
  64. public function write(#[\SensitiveParameter] string $sessionId, string $sessionData): bool
  65. {
  66. $result = $this->currentHandler->write($sessionId, $sessionData);
  67. $this->writeOnlyHandler->write($sessionId, $sessionData);
  68. return $result;
  69. }
  70. public function validateId(#[\SensitiveParameter] string $sessionId): bool
  71. {
  72. // No reading from new handler until switch-over
  73. return $this->currentHandler->validateId($sessionId);
  74. }
  75. public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string $sessionData): bool
  76. {
  77. $result = $this->currentHandler->updateTimestamp($sessionId, $sessionData);
  78. $this->writeOnlyHandler->updateTimestamp($sessionId, $sessionData);
  79. return $result;
  80. }
  81. }