MockFileSessionStorage.php 3.8KB

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