MockFileSessionStorage.php 3.9 KB

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