sfSessionTestStorage.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 11002 2008-08-20 16:34:30Z 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
  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. if (!isset($options['session_path']))
  35. {
  36. throw new InvalidArgumentException('The "session_path" option is mandatory for the sfSessionTestStorage class.');
  37. }
  38. $options = array_merge(array(
  39. 'session_id' => null,
  40. ), $options);
  41. // initialize parent
  42. parent::initialize($options);
  43. $this->sessionId = !is_null($this->options['session_id']) ? $this->options['session_id'] : (array_key_exists('session_id', $_SERVER) ? $_SERVER['session_id'] : null);
  44. if ($this->sessionId)
  45. {
  46. // we read session data from temp file
  47. $file = $this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session';
  48. $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array();
  49. }
  50. else
  51. {
  52. $this->sessionId = md5(uniqid(rand(), true));
  53. $this->sessionData = array();
  54. }
  55. }
  56. /**
  57. * Gets session id for the current session storage instance.
  58. *
  59. * @return string Session id
  60. */
  61. public function getSessionId()
  62. {
  63. return $this->sessionId;
  64. }
  65. /**
  66. * Reads data from this storage.
  67. *
  68. * The preferred format for a key is directory style so naming conflicts can be avoided.
  69. *
  70. * @param string $key A unique key identifying your data
  71. *
  72. * @return mixed Data associated with the key
  73. */
  74. public function read($key)
  75. {
  76. $retval = null;
  77. if (isset($this->sessionData[$key]))
  78. {
  79. $retval = $this->sessionData[$key];
  80. }
  81. return $retval;
  82. }
  83. /**
  84. * Removes data from this storage.
  85. *
  86. * The preferred format for a key is directory style so naming conflicts can be avoided.
  87. *
  88. * @param string $key A unique key identifying your data
  89. *
  90. * @return mixed Data associated with the key
  91. */
  92. public function remove($key)
  93. {
  94. $retval = null;
  95. if (isset($this->sessionData[$key]))
  96. {
  97. $retval = $this->sessionData[$key];
  98. unset($this->sessionData[$key]);
  99. }
  100. return $retval;
  101. }
  102. /**
  103. * Writes data to this storage.
  104. *
  105. * The preferred format for a key is directory style so naming conflicts can be avoided
  106. *
  107. * @param string $key A unique key identifying your data
  108. * @param mixed $data Data associated with your key
  109. *
  110. */
  111. public function write($key, $data)
  112. {
  113. $this->sessionData[$key] = $data;
  114. }
  115. /**
  116. * Clears all test sessions.
  117. */
  118. public function clear()
  119. {
  120. sfToolkit::clearDirectory($this->options['session_path']);
  121. }
  122. /**
  123. * Regenerates id that represents this storage.
  124. *
  125. * @param boolean $destroy Destroy session when regenerating?
  126. *
  127. * @return boolean True if session regenerated, false if error
  128. *
  129. */
  130. public function regenerate($destroy = false)
  131. {
  132. return true;
  133. }
  134. /**
  135. * Executes the shutdown procedure.
  136. *
  137. */
  138. public function shutdown()
  139. {
  140. if ($this->sessionId)
  141. {
  142. $current_umask = umask(0000);
  143. if (!is_dir($this->options['session_path']))
  144. {
  145. mkdir($this->options['session_path'], 0777, true);
  146. }
  147. umask($current_umask);
  148. file_put_contents($this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session', serialize($this->sessionData));
  149. $this->sessionId = '';
  150. $this->sessionData = array();
  151. }
  152. }
  153. }