sfSessionTestStorage.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfSessionTestStorage is a fake sfSessionStorage implementation to allow easy testing.
  11. *
  12. * @package symfony
  13. * @subpackage storage
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfSessionTestStorage.class.php 9942 2008-06-27 18:00:49Z fabien $
  16. */
  17. class sfSessionTestStorage extends sfStorage
  18. {
  19. protected
  20. $sessionId = null,
  21. $sessionData = array();
  22. /**
  23. * Available options:
  24. *
  25. * * session_path: The path to store the session files (%SF_TEST_CACHE_DIR%/sessions by default)
  26. * * session_id: The session identifier
  27. *
  28. * @param array $options An associative array of options
  29. *
  30. * @see sfStorage
  31. */
  32. public function initialize($options = null)
  33. {
  34. $options = array_merge(array(
  35. 'session_path' => sfConfig::get('sf_test_cache_dir').'/sessions',
  36. 'session_id' => null,
  37. ), $options);
  38. // initialize parent
  39. parent::initialize($options);
  40. $this->sessionId = !is_null($this->options['session_id']) ? $this->options['session_id'] : (array_key_exists('session_id', $_SERVER) ? $_SERVER['session_id'] : null);
  41. if ($this->sessionId)
  42. {
  43. // we read session data from temp file
  44. $file = $this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session';
  45. $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array();
  46. }
  47. else
  48. {
  49. $this->sessionId = md5(uniqid(rand(), true));
  50. $this->sessionData = array();
  51. }
  52. }
  53. /**
  54. * Gets session id for the current session storage instance.
  55. *
  56. * @return string Session id
  57. */
  58. public function getSessionId()
  59. {
  60. return $this->sessionId;
  61. }
  62. /**
  63. * Reads data from this storage.
  64. *
  65. * The preferred format for a key is directory style so naming conflicts can be avoided.
  66. *
  67. * @param string $key A unique key identifying your data
  68. *
  69. * @return mixed Data associated with the key
  70. */
  71. public function read($key)
  72. {
  73. $retval = null;
  74. if (isset($this->sessionData[$key]))
  75. {
  76. $retval = $this->sessionData[$key];
  77. }
  78. return $retval;
  79. }
  80. /**
  81. * Removes data from this storage.
  82. *
  83. * The preferred format for a key is directory style so naming conflicts can be avoided.
  84. *
  85. * @param string $key A unique key identifying your data
  86. *
  87. * @return mixed Data associated with the key
  88. */
  89. public function remove($key)
  90. {
  91. $retval = null;
  92. if (isset($this->sessionData[$key]))
  93. {
  94. $retval = $this->sessionData[$key];
  95. unset($this->sessionData[$key]);
  96. }
  97. return $retval;
  98. }
  99. /**
  100. * Writes data to this storage.
  101. *
  102. * The preferred format for a key is directory style so naming conflicts can be avoided
  103. *
  104. * @param string $key A unique key identifying your data
  105. * @param mixed $data Data associated with your key
  106. *
  107. */
  108. public function write($key, $data)
  109. {
  110. $this->sessionData[$key] = $data;
  111. }
  112. /**
  113. * Clears all test sessions.
  114. */
  115. public function clear()
  116. {
  117. sfToolkit::clearDirectory($this->options['session_path']);
  118. }
  119. /**
  120. * Regenerates id that represents this storage.
  121. *
  122. * @param boolean $destroy Destroy session when regenerating?
  123. *
  124. * @return boolean True if session regenerated, false if error
  125. *
  126. */
  127. public function regenerate($destroy = false)
  128. {
  129. return true;
  130. }
  131. /**
  132. * Executes the shutdown procedure.
  133. *
  134. */
  135. public function shutdown()
  136. {
  137. if ($this->sessionId)
  138. {
  139. $current_umask = umask(0000);
  140. if (!is_dir($this->options['session_path']))
  141. {
  142. mkdir($this->options['session_path'], 0777, true);
  143. }
  144. umask($current_umask);
  145. file_put_contents($this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session', serialize($this->sessionData));
  146. $this->sessionId = '';
  147. $this->sessionData = array();
  148. }
  149. }
  150. }