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

Debug toolbar