MockFileSessionStorage.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 $savePath Path of directory to save session files
  27. * @param string $name Session name
  28. */
  29. public function __construct(string $savePath = null, string $name = 'MOCKSESSID', MetadataBag $metaBag = null)
  30. {
  31. if (null === $savePath) {
  32. $savePath = sys_get_temp_dir();
  33. }
  34. if (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) {
  35. throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s".', $savePath));
  36. }
  37. $this->savePath = $savePath;
  38. parent::__construct($name, $metaBag);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function start()
  44. {
  45. if ($this->started) {
  46. return true;
  47. }
  48. if (!$this->id) {
  49. $this->id = $this->generateId();
  50. }
  51. $this->read();
  52. $this->started = true;
  53. return true;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function regenerate($destroy = false, $lifetime = null)
  59. {
  60. if (!$this->started) {
  61. $this->start();
  62. }
  63. if ($destroy) {
  64. $this->destroy();
  65. }
  66. return parent::regenerate($destroy, $lifetime);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function save()
  72. {
  73. if (!$this->started) {
  74. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
  75. }
  76. $data = $this->data;
  77. foreach ($this->bags as $bag) {
  78. if (empty($data[$key = $bag->getStorageKey()])) {
  79. unset($data[$key]);
  80. }
  81. }
  82. if ([$key = $this->metadataBag->getStorageKey()] === array_keys($data)) {
  83. unset($data[$key]);
  84. }
  85. try {
  86. if ($data) {
  87. file_put_contents($this->getFilePath(), serialize($data));
  88. } else {
  89. $this->destroy();
  90. }
  91. } finally {
  92. $this->data = $data;
  93. }
  94. // this is needed for Silex, where the session object is re-used across requests
  95. // in functional tests. In Symfony, the container is rebooted, so we don't have
  96. // this issue
  97. $this->started = false;
  98. }
  99. /**
  100. * Deletes a session from persistent storage.
  101. * Deliberately leaves session data in memory intact.
  102. */
  103. private function destroy(): void
  104. {
  105. if (is_file($this->getFilePath())) {
  106. unlink($this->getFilePath());
  107. }
  108. }
  109. /**
  110. * Calculate path to file.
  111. */
  112. private function getFilePath(): string
  113. {
  114. return $this->savePath.'/'.$this->id.'.mocksess';
  115. }
  116. /**
  117. * Reads session from storage and loads session.
  118. */
  119. private function read(): void
  120. {
  121. $filePath = $this->getFilePath();
  122. $this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : [];
  123. $this->loadSession();
  124. }
  125. }