Development

/branches/1.1/lib/storage/sfDatabaseSessionStorage.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/storage/sfDatabaseSessionStorage.class.php

Revision 17521, 5.2 kB (checked in by dwhittle, 5 years ago)

[1.2]: fixed session_write_close is not called in sfDatabaseSessionStorage::shutdown (#6216)

  • 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  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * Base class for all sfStorage that uses a sfDatabase object as a storage.
14  *
15  * @package    symfony
16  * @subpackage storage
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 abstract class sfDatabaseSessionStorage extends sfSessionStorage
22 {
23   protected
24     $db = null,
25     $con = null;
26
27   /**
28    * Available options:
29    *
30    *   * db_table:    The database table in which session data will be stored
31    *   * database:    The sfDatabase object to use
32    *   * db_id_col:   The database column in which the session id will be stored (sess_id by default)
33    *   * db_data_col: The database column in which the session data will be stored (sess_data by default)
34    *   * db_time_col: The database column in which the session timestamp will be stored (sess_time by default)
35    *
36    * @param  array $options  An associative array of options
37    *
38    * @see sfSessionStorage
39    */
40   public function initialize($options = array())
41   {
42     $options = array_merge(array(
43       'db_id_col'   => 'sess_id',
44       'db_data_col' => 'sess_data',
45       'db_time_col' => 'sess_time',
46     ), $options);
47
48     // disable auto_start
49     $options['auto_start'] = false;
50
51     // initialize the parent
52     parent::initialize($options);
53
54     if (!isset($this->options['db_table']))
55     {
56       throw new sfInitializationException('You must provide a "db_table" option to sfDatabaseSessionStorage.');
57     }
58
59     if (!isset($this->options['database']))
60     {
61       throw new sfInitializationException('You must provide a "database" option to sfDatabaseSessionStorage.');
62     }
63
64     // use this object as the session handler
65     session_set_save_handler(array($this, 'sessionOpen'),
66                              array($this, 'sessionClose'),
67                              array($this, 'sessionRead'),
68                              array($this, 'sessionWrite'),
69                              array($this, 'sessionDestroy'),
70                              array($this, 'sessionGC'));
71
72     // start our session
73     session_start();
74   }
75
76   /**
77    * Closes a session.
78    *
79    * @return boolean true, if the session was closed, otherwise false
80    */
81   public function sessionClose()
82   {
83     // do nothing
84     return true;
85   }
86
87   /**
88    * Opens a session.
89    *
90    * @param  string $path  (ignored)
91    * @param  string $name  (ignored)
92    *
93    * @return boolean true, if the session was opened, otherwise an exception is thrown
94    *
95    * @throws <b>DatabaseException</b> If a connection with the database does not exist or cannot be created
96    */
97   public function sessionOpen($path = null, $name = null)
98   {
99     // what database are we using?
100     $database = $this->options['database'];
101
102     // get the database resource
103     $this->db = $database->getResource();
104     $this->con = $database->getConnection();
105     
106     if (is_null($this->db) && is_null($this->con))
107     {
108       throw new sfDatabaseException('Database connection doesn\'t exist. Unable to open session.');
109     }
110
111     return true;
112   }
113
114   /**
115    * Destroys a session.
116    *
117    * @param  string $id  A session ID
118    *
119    * @return bool true, if the session was destroyed, otherwise an exception is thrown
120    *
121    * @throws <b>DatabaseException</b> If the session cannot be destroyed
122    */
123   abstract public function sessionDestroy($id);
124
125   /**
126    * Cleans up old sessions.
127    *
128    * @param  int $lifetime  The lifetime of a session
129    *
130    * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
131    *
132    * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
133    */
134   abstract public function sessionGC($lifetime);
135
136   /**
137    * Reads a session.
138    *
139    * @param  string $id  A session ID
140    *
141    * @return bool true, if the session was read, otherwise an exception is thrown
142    *
143    * @throws <b>DatabaseException</b> If the session cannot be read
144    */
145   abstract public function sessionRead($id);
146
147   /**
148    * Writes session data.
149    *
150    * @param  string $id    A session ID
151    * @param  string $data  A serialized chunk of session data
152    *
153    * @return bool true, if the session was written, otherwise an exception is thrown
154    *
155    * @throws <b>DatabaseException</b> If the session data cannot be written
156    */
157   abstract public function sessionWrite($id, $data);
158
159   /**
160    * Regenerates id that represents this storage.
161    *
162    * @param  boolean $destroy Destroy session when regenerating?
163    *
164    * @return boolean True if session regenerated, false if error
165    *
166    */
167   public function regenerate($destroy = false)
168   {
169     if (self::$sessionIdRegenerated)
170     {
171       return;
172     }
173
174     $currentId = session_id();
175
176     parent::regenerate($destroy);
177
178     $newId = session_id();
179     $this->sessionRead($newId);
180
181     return $this->sessionWrite($newId, $this->sessionRead($currentId));
182   }
183
184   /**
185    * Executes the shutdown procedure.
186    *
187    */
188   public function shutdown()
189   {
190     parent::shutdown();
191   }
192 }
193
Note: See TracBrowser for help on using the browser.