MockFileSessionStorage.php 3.4 KB

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