MigratingSessionHandler.php 3.4 KB

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