MigratingSessionHandler.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 $currentHandler;
  26. /**
  27. * @var \SessionHandlerInterface&\SessionUpdateTimestampHandlerInterface
  28. */
  29. private $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. /**
  42. * @return bool
  43. */
  44. #[\ReturnTypeWillChange]
  45. public function close()
  46. {
  47. $result = $this->currentHandler->close();
  48. $this->writeOnlyHandler->close();
  49. return $result;
  50. }
  51. /**
  52. * @return bool
  53. */
  54. #[\ReturnTypeWillChange]
  55. public function destroy($sessionId)
  56. {
  57. $result = $this->currentHandler->destroy($sessionId);
  58. $this->writeOnlyHandler->destroy($sessionId);
  59. return $result;
  60. }
  61. /**
  62. * @return int|false
  63. */
  64. #[\ReturnTypeWillChange]
  65. public function gc($maxlifetime)
  66. {
  67. $result = $this->currentHandler->gc($maxlifetime);
  68. $this->writeOnlyHandler->gc($maxlifetime);
  69. return $result;
  70. }
  71. /**
  72. * @return bool
  73. */
  74. #[\ReturnTypeWillChange]
  75. public function open($savePath, $sessionName)
  76. {
  77. $result = $this->currentHandler->open($savePath, $sessionName);
  78. $this->writeOnlyHandler->open($savePath, $sessionName);
  79. return $result;
  80. }
  81. /**
  82. * @return string
  83. */
  84. #[\ReturnTypeWillChange]
  85. public function read($sessionId)
  86. {
  87. // No reading from new handler until switch-over
  88. return $this->currentHandler->read($sessionId);
  89. }
  90. /**
  91. * @return bool
  92. */
  93. #[\ReturnTypeWillChange]
  94. public function write($sessionId, $sessionData)
  95. {
  96. $result = $this->currentHandler->write($sessionId, $sessionData);
  97. $this->writeOnlyHandler->write($sessionId, $sessionData);
  98. return $result;
  99. }
  100. /**
  101. * @return bool
  102. */
  103. #[\ReturnTypeWillChange]
  104. public function validateId($sessionId)
  105. {
  106. // No reading from new handler until switch-over
  107. return $this->currentHandler->validateId($sessionId);
  108. }
  109. /**
  110. * @return bool
  111. */
  112. #[\ReturnTypeWillChange]
  113. public function updateTimestamp($sessionId, $sessionData)
  114. {
  115. $result = $this->currentHandler->updateTimestamp($sessionId, $sessionData);
  116. $this->writeOnlyHandler->updateTimestamp($sessionId, $sessionData);
  117. return $result;
  118. }
  119. }