MockFileSessionStorage.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. */
  34. public function __construct($savePath = null, $name = 'MOCKSESSID')
  35. {
  36. if (null === $savePath) {
  37. $savePath = sys_get_temp_dir();
  38. }
  39. if (!is_dir($savePath)) {
  40. mkdir($savePath, 0777, true);
  41. }
  42. $this->savePath = $savePath;
  43. parent::__construct($name);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function start()
  49. {
  50. if ($this->started) {
  51. return true;
  52. }
  53. if (!$this->id) {
  54. $this->id = $this->generateId();
  55. }
  56. $this->read();
  57. $this->started = true;
  58. return true;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function regenerate($destroy = false)
  64. {
  65. if ($destroy) {
  66. $this->destroy();
  67. }
  68. $this->id = $this->generateId();
  69. return true;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function save()
  75. {
  76. file_put_contents($this->getFilePath(), serialize($this->data));
  77. }
  78. /**
  79. * Deletes a session from persistent storage.
  80. * Deliberately leaves session data in memory intact.
  81. */
  82. private function destroy()
  83. {
  84. if (is_file($this->getFilePath())) {
  85. unlink($this->getFilePath());
  86. }
  87. }
  88. /**
  89. * Calculate path to file.
  90. *
  91. * @return string File path
  92. */
  93. private function getFilePath()
  94. {
  95. return $this->savePath.'/'.$this->id.'.mocksess';
  96. }
  97. /**
  98. * Reads session from storage and loads session.
  99. */
  100. private function read()
  101. {
  102. $filePath = $this->getFilePath();
  103. $this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
  104. $this->loadSession();
  105. }
  106. }