Development

/branches/1.3/lib/storage/sfSessionTestStorage.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/storage/sfSessionTestStorage.class.php

Revision 23810, 4.1 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.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
11 /**
12  * sfSessionTestStorage is a fake sfSessionStorage implementation to allow easy testing.
13  *
14  * @package    symfony
15  * @subpackage storage
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfSessionTestStorage extends sfStorage
20 {
21   protected
22     $sessionId   = null,
23     $sessionData = array();
24
25   /**
26    * Available options:
27    *
28    *  * session_path: The path to store the session files
29    *  * session_id:   The session identifier
30    *
31    * @param array $options  An associative array of options
32    *
33    * @see sfStorage
34    */
35   public function initialize($options = null)
36   {
37     if (!isset($options['session_path']))
38     {
39       throw new InvalidArgumentException('The "session_path" option is mandatory for the sfSessionTestStorage class.');
40     }
41
42     $options = array_merge(array(
43       'session_id'   => null,
44     ), $options);
45
46     // initialize parent
47     parent::initialize($options);
48
49     $this->sessionId = null !== $this->options['session_id'] ? $this->options['session_id'] : (array_key_exists('session_id', $_SERVER) ? $_SERVER['session_id'] : null);
50
51     if ($this->sessionId)
52     {
53       // we read session data from temp file
54       $file = $this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session';
55       $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array();
56     }
57     else
58     {
59       $this->sessionId   = md5(uniqid(rand(), true));
60       $this->sessionData = array();
61     }
62   }
63
64   /**
65    * Gets session id for the current session storage instance.
66    *
67    * @return string Session id
68    */
69   public function getSessionId()
70   {
71     return $this->sessionId;
72   }
73
74   /**
75    * Reads data from this storage.
76    *
77    * The preferred format for a key is directory style so naming conflicts can be avoided.
78    *
79    * @param  string $key  A unique key identifying your data
80    *
81    * @return mixed Data associated with the key
82    */
83   public function read($key)
84   {
85     $retval = null;
86
87     if (isset($this->sessionData[$key]))
88     {
89       $retval = $this->sessionData[$key];
90     }
91
92     return $retval;
93   }
94
95   /**
96    * Removes data from this storage.
97    *
98    * The preferred format for a key is directory style so naming conflicts can be avoided.
99    *
100    * @param  string $key  A unique key identifying your data
101    *
102    * @return mixed Data associated with the key
103    */
104   public function remove($key)
105   {
106     $retval = null;
107
108     if (isset($this->sessionData[$key]))
109     {
110       $retval = $this->sessionData[$key];
111       unset($this->sessionData[$key]);
112     }
113
114     return $retval;
115   }
116
117   /**
118    * Writes data to this storage.
119    *
120    * The preferred format for a key is directory style so naming conflicts can be avoided
121    *
122    * @param string $key   A unique key identifying your data
123    * @param mixed  $data  Data associated with your key
124    *
125    */
126   public function write($key, $data)
127   {
128     $this->sessionData[$key] = $data;
129   }
130
131   /**
132    * Clears all test sessions.
133    */
134   public function clear()
135   {
136     sfToolkit::clearDirectory($this->options['session_path']);
137   }
138
139   /**
140    * Regenerates id that represents this storage.
141    *
142    * @param  boolean $destroy Destroy session when regenerating?
143    *
144    * @return boolean True if session regenerated, false if error
145    *
146    */
147   public function regenerate($destroy = false)
148   {
149     return true;
150   }
151
152   /**
153    * Executes the shutdown procedure.
154    *
155    */
156   public function shutdown()
157   {
158     if ($this->sessionId)
159     {
160       $current_umask = umask(0000);
161       if (!is_dir($this->options['session_path']))
162       {
163         mkdir($this->options['session_path'], 0777, true);
164       }
165       umask($current_umask);
166       file_put_contents($this->options['session_path'].DIRECTORY_SEPARATOR.$this->sessionId.'.session', serialize($this->sessionData));
167       $this->sessionId   = '';
168       $this->sessionData = array();
169     }
170   }
171 }
172
Note: See TracBrowser for help on using the browser.