MockFileSessionStorage.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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;
  11. /**
  12. * MockFileSessionStorage is used to mock sessions for
  13. * functional testing where you may need to persist session data
  14. * across separate PHP processes.
  15. *
  16. * No PHP session is actually started since a session can be initialized
  17. * and shutdown only once per PHP execution cycle and this class does
  18. * not pollute any session related globals, including session_*() functions
  19. * or session.* PHP ini directives.
  20. *
  21. * @author Drak <drak@zikula.org>
  22. */
  23. class MockFileSessionStorage extends MockArraySessionStorage
  24. {
  25. private string $savePath;
  26. /**
  27. * @param string|null $savePath Path of directory to save session files
  28. */
  29. public function __construct(?string $savePath = null, string $name = 'MOCKSESSID', ?MetadataBag $metaBag = null)
  30. {
  31. $savePath ??= sys_get_temp_dir();
  32. if (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) {
  33. throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s".', $savePath));
  34. }
  35. $this->savePath = $savePath;
  36. parent::__construct($name, $metaBag);
  37. }
  38. public function start(): bool
  39. {
  40. if ($this->started) {
  41. return true;
  42. }
  43. if (!$this->id) {
  44. $this->id = $this->generateId();
  45. }
  46. $this->read();
  47. $this->started = true;
  48. return true;
  49. }
  50. public function regenerate(bool $destroy = false, ?int $lifetime = null): bool
  51. {
  52. if (!$this->started) {
  53. $this->start();
  54. }
  55. if ($destroy) {
  56. $this->destroy();
  57. }
  58. return parent::regenerate($destroy, $lifetime);
  59. }
  60. /**
  61. * @return void
  62. */
  63. public function save()
  64. {
  65. if (!$this->started) {
  66. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
  67. }
  68. $data = $this->data;
  69. foreach ($this->bags as $bag) {
  70. if (empty($data[$key = $bag->getStorageKey()])) {
  71. unset($data[$key]);
  72. }
  73. }
  74. if ([$key = $this->metadataBag->getStorageKey()] === array_keys($data)) {
  75. unset($data[$key]);
  76. }
  77. try {
  78. if ($data) {
  79. $path = $this->getFilePath();
  80. $tmp = $path.bin2hex(random_bytes(6));
  81. file_put_contents($tmp, serialize($data));
  82. rename($tmp, $path);
  83. } else {
  84. $this->destroy();
  85. }
  86. } finally {
  87. $this->data = $data;
  88. }
  89. // this is needed when the session object is re-used across multiple requests
  90. // in functional tests.
  91. $this->started = false;
  92. }
  93. /**
  94. * Deletes a session from persistent storage.
  95. * Deliberately leaves session data in memory intact.
  96. */
  97. private function destroy(): void
  98. {
  99. set_error_handler(static function () {});
  100. try {
  101. unlink($this->getFilePath());
  102. } finally {
  103. restore_error_handler();
  104. }
  105. }
  106. /**
  107. * Calculate path to file.
  108. */
  109. private function getFilePath(): string
  110. {
  111. return $this->savePath.'/'.$this->id.'.mocksess';
  112. }
  113. /**
  114. * Reads session from storage and loads session.
  115. */
  116. private function read(): void
  117. {
  118. set_error_handler(static function () {});
  119. try {
  120. $data = file_get_contents($this->getFilePath());
  121. } finally {
  122. restore_error_handler();
  123. }
  124. $this->data = $data ? unserialize($data) : [];
  125. $this->loadSession();
  126. }
  127. }