MigratingSessionHandler.php 3.2 KB

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