123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- class sfSessionTestStorage extends sfStorage
- {
- protected
- $sessionId = null,
- $sessionData = array();
-
- public function initialize($options = null)
- {
- if (!isset($options['session_path']))
- {
- throw new InvalidArgumentException('The "session_path" option is mandatory for the sfSessionTestStorage class.');
- }
- $options = array_merge(array(
- 'session_id' => null,
- ), $options);
-
- parent::initialize($options);
- $this->sessionId = !is_null($this->options['session_id']) ? $this->options['session_id'] : (array_key_exists('session_id', $_SERVER) ? $_SERVER['session_id'] : null);
- if ($this->sessionId)
- {
-
- $file = $this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session';
- $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array();
- }
- else
- {
- $this->sessionId = md5(uniqid(rand(), true));
- $this->sessionData = array();
- }
- }
-
- public function getSessionId()
- {
- return $this->sessionId;
- }
-
- public function read($key)
- {
- $retval = null;
- if (isset($this->sessionData[$key]))
- {
- $retval = $this->sessionData[$key];
- }
- return $retval;
- }
-
- public function remove($key)
- {
- $retval = null;
- if (isset($this->sessionData[$key]))
- {
- $retval = $this->sessionData[$key];
- unset($this->sessionData[$key]);
- }
- return $retval;
- }
-
- public function write($key, $data)
- {
- $this->sessionData[$key] = $data;
- }
-
- public function clear()
- {
- sfToolkit::clearDirectory($this->options['session_path']);
- }
-
- public function regenerate($destroy = false)
- {
- return true;
- }
-
- public function shutdown()
- {
- if ($this->sessionId)
- {
- $current_umask = umask(0000);
- if (!is_dir($this->options['session_path']))
- {
- mkdir($this->options['session_path'], 0777, true);
- }
- umask($current_umask);
- file_put_contents($this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session', serialize($this->sessionData));
- $this->sessionId = '';
- $this->sessionData = array();
- }
- }
- }
|