Changeset 4890
- Timestamp:
- 08/23/07 17:48:11 (1 year ago)
- Files:
-
- trunk/data/skeleton/app/app/config/factories.yml (modified) (1 diff)
- trunk/lib/addon/creole/storage/sfCreoleSessionStorage.class.php (modified) (9 diffs)
- trunk/lib/config/sfFactoryConfigHandler.class.php (modified) (1 diff)
- trunk/lib/database/sfDatabase.class.php (modified) (3 diffs)
- trunk/lib/storage/sfDatabaseSessionStorage.class.php (added)
- trunk/lib/storage/sfMySQLSessionStorage.class.php (modified) (10 diffs)
- trunk/lib/storage/sfNoStorage.class.php (added)
- trunk/lib/storage/sfPDOSessionStorage.class.php (modified) (10 diffs)
- trunk/lib/storage/sfPostgreSQLSessionStorage.class.php (modified) (13 diffs)
- trunk/lib/storage/sfSessionStorage.class.php (modified) (8 diffs)
- trunk/lib/storage/sfSessionTestStorage.class.php (modified) (10 diffs)
- trunk/lib/storage/sfStorage.class.php (modified) (6 diffs)
- trunk/test/functional/fixtures/project/apps/backend/config/factories.yml (modified) (1 diff)
- trunk/test/functional/fixtures/project/apps/cache/config/factories.yml (modified) (1 diff)
- trunk/test/functional/fixtures/project/apps/crud/config/factories.yml (modified) (1 diff)
- trunk/test/functional/fixtures/project/apps/frontend/config/factories.yml (modified) (1 diff)
- trunk/test/functional/fixtures/project/apps/i18n/config/factories.yml (modified) (1 diff)
- trunk/test/unit/sfContextMock.class.php (modified) (1 diff)
- trunk/test/unit/storage/sfNoStorageTest.php (added)
- trunk/test/unit/storage/sfPDOSessionStorageTest.php (added)
- trunk/test/unit/storage/sfStorageTest.php (modified) (3 diffs)
- trunk/test/unit/user/sfUserTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/data/skeleton/app/app/config/factories.yml
r4852 r4890 17 17 storage: 18 18 class: sfSessionTestStorage 19 param: 20 session_path: %SF_TEST_CACHE_DIR%/sessions 19 21 20 22 #all: trunk/lib/addon/creole/storage/sfCreoleSessionStorage.class.php
r4887 r4890 16 16 * Provides support for session storage using a CreoleDb database abstraction layer. 17 17 * 18 * <b>Required parameters:</b> 19 * 20 * # <b>db_table</b> - [none] - The database table in which session data will be 21 * stored. 22 * 23 * <b>Optional parameters:</b> 24 * 25 * # <b>database</b> - [default] - The database connection to use 26 * (see databases.ini). 27 * # <b>db_id_col</b> - [sess_id] - The database column in which the 28 * session id will be stored. 29 * # <b>db_data_col</b> - [sess_data] - The database column in which the 30 * session data will be stored. 31 * # <b>db_time_col</b> - [sess_time] - The database column in which the 32 * session timestamp will be stored. 33 * # <b>session_name</b> - [Agavi] - The name of the session. 18 * <b>parameters:</b> see sfDatabaseSessionStorage 34 19 * 35 20 * @package symfony … … 40 25 * @version SVN: $Id$ 41 26 */ 42 class sfCreoleSessionStorage extends sf SessionStorage27 class sfCreoleSessionStorage extends sfDatabaseSessionStorage 43 28 { 44 /**45 * Creole Database Connection46 * @var Connection47 */48 protected $db;49 50 /**51 * Initialize this Storage.52 *53 * @param Context A Context instance.54 * @param array An associative array of initialization parameters.55 *56 * @return bool true, if initialization completes successfully, otherwise57 * false.58 *59 * @throws <b>InitializationException</b> If an error occurs while60 * initializing this Storage.61 */62 public function initialize($context, $parameters = null)63 {64 // disable auto_start65 $parameters['auto_start'] = false;66 67 // initialize the parent68 parent::initialize($context, $parameters);69 70 if (!$this->getParameterHolder()->has('db_table'))71 {72 // missing required 'db_table' parameter73 throw new sfInitializationException('Factory configuration file is missing required "db_table" parameter for the Storage category.');74 }75 76 // use this object as the session handler77 session_set_save_handler(array($this, 'sessionOpen'),78 array($this, 'sessionClose'),79 array($this, 'sessionRead'),80 array($this, 'sessionWrite'),81 array($this, 'sessionDestroy'),82 array($this, 'sessionGC'));83 84 // start our session85 session_start();86 }87 88 /**89 * Close a session.90 *91 * @return bool true, if the session was closed, otherwise false.92 */93 public function sessionClose()94 {95 // do nothing96 return true;97 }98 99 29 /** 100 30 * Destroy a session. … … 109 39 { 110 40 // get table/column 111 $db_table = $this->getParameter Holder()->get('db_table');112 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');41 $db_table = $this->getParameter('db_table'); 42 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 113 43 114 44 // delete the record associated with this id 115 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';45 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?'; 116 46 117 47 try … … 125 55 throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage())); 126 56 } 57 58 return true; 127 59 } 128 60 … … 138 70 public function sessionGC($lifetime) 139 71 { 140 // determine deletable session time141 $time = time() - $lifetime;142 143 72 // get table/column 144 $db_table = $this->getParameter Holder()->get('db_table');145 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');73 $db_table = $this->getParameter('db_table'); 74 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 146 75 147 76 // delete the record associated with this id 148 $sql = 'DELETE FROM '.$db_table.' '.'WHERE '.$db_time_col.' < '.$time;77 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime); 149 78 150 79 try 151 80 { 152 81 $this->db->executeQuery($sql); 153 return true;154 82 } 155 83 catch (SQLException $e) 156 84 { 157 85 throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage())); 158 }159 }160 161 /**162 * Open a session.163 *164 * @param string165 * @param string166 *167 * @return bool true, if the session was opened, otherwise an exception is thrown.168 *169 * @throws <b>DatabaseException</b> If a connection with the database does170 * not exist or cannot be created.171 */172 public function sessionOpen($path, $name)173 {174 // what database are we using?175 $database = $this->getParameterHolder()->get('database', 'default');176 177 // autoload propel propely if we're reusing the propel connection for session storage178 if ($this->getContext()->getDatabaseManager()->getDatabase($database) instanceof sfPropelDatabase && !Propel::isInit())179 {180 throw new sfDatabaseException('Creole dabatase connection is the same as the propel database connection, but could not be initialized.');181 }182 183 $this->db = $this->getContext()->getDatabaseConnection($database);184 if ($this->db == null || !$this->db instanceof Connection)185 {186 throw new sfDatabaseException('Creole dabatase connection doesn\'t exist. Unable to open session.');187 86 } 188 87 … … 202 101 { 203 102 // get table/columns 204 $db_table = $this->getParameter Holder()->get('db_table');205 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');206 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');207 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');103 $db_table = $this->getParameter('db_table'); 104 $db_data_col = $this->getParameter('db_data_col', 'sess_data'); 105 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 106 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 208 107 209 108 try … … 256 155 { 257 156 // get table/column 258 $db_table = $this->getParameter Holder()->get('db_table');259 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');260 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');261 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');157 $db_table = $this->getParameter('db_table'); 158 $db_data_col = $this->getParameter('db_data_col', 'sess_data'); 159 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 160 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 262 161 263 162 $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.'=?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'=?'; … … 269 168 $stmt->setString(2, $id); 270 169 $stmt->executeUpdate(); 271 272 return true;273 170 } 274 275 171 catch (SQLException $e) 276 172 { … … 278 174 } 279 175 280 return false; 281 } 282 283 /** 284 * Execute the shutdown procedure. 285 * 286 * @return void 287 */ 288 public function shutdown() 289 { 176 return true; 290 177 } 291 178 } trunk/lib/config/sfFactoryConfigHandler.class.php
r4845 r4890 127 127 128 128 // append instance initialization 129 $inits[] = sprintf(" \$this->factories['storage']->initialize(\$this, sfConfig::get('sf_factory_storage_parameters', %s));", var_export($parameters, true)); 129 $defaultParameters = array(); 130 $defaultParameters[] = sprintf("'session_id' => \$this->getRequest()->getParameter('%s'),", $parameters['session_name']); 131 if (is_subclass_of($class, 'sfDatabaseSessionStorage')) 132 { 133 $defaultParameters[] = sprintf("'database' => \$this->getDatabaseManager()->getDatabase('%s'),", isset($parameters['database']) ? $parameters['database'] : 'default'); 134 } 135 $inits[] = sprintf(" \$this->factories['storage']->initialize(array_merge(array(\n%s\n), sfConfig::get('sf_factory_storage_parameters', %s)));", implode("\n", $defaultParameters), var_export($parameters, true)); 130 136 break; 131 137 trunk/lib/database/sfDatabase.class.php
r3210 r4890 23 23 { 24 24 protected 25 $parameterHolder = null, 25 26 $connection = null, 26 $parameterHolder = null,27 27 $resource = null; 28 28 … … 46 46 public function getConnection() 47 47 { 48 if ( $this->connection == null)48 if (is_null($this->connection)) 49 49 { 50 50 $this->connect(); … … 63 63 public function getResource() 64 64 { 65 if ( $this->resource == null)65 if (is_null($this->resource)) 66 66 { 67 67 $this->connect(); trunk/lib/storage/sfMySQLSessionStorage.class.php
r4887 r4890 13 13 * Provides support for session storage using a MySQL brand database. 14 14 * 15 * <b>Required parameters:</b> 16 * 17 * # <b>db_table</b> - [none] - The database table in which session data will be 18 * stored. 19 * 20 * <b>Optional parameters:</b> 21 * 22 * # <b>db_id_col</b> - [sess_id] - The database column in which the 23 * session id will be stored. 24 * # <b>db_data_col</b> - [sess_data] - The database column in which the 25 * session data will be stored. 26 * # <b>db_time_col</b> - [sess_time] - The database column in which the 27 * session timestamp will be stored. 28 * # <b>session_name</b> - [symfony] - The name of the session. 15 * <b>parameters:</b> see sfDatabaseSessionStorage 29 16 * 30 17 * @package symfony … … 34 21 * @version SVN: $Id$ 35 22 */ 36 class sfMySQLSessionStorage extends sf SessionStorage23 class sfMySQLSessionStorage extends sfDatabaseSessionStorage 37 24 { 38 protected39 $resource = null;40 41 /**42 * Initializes this Storage instance.43 *44 * @param sfContext A sfContext instance45 * @param array An associative array of initialization parameters46 *47 * @return boolean true, if initialization completes successfully, otherwise false48 *49 * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage50 */51 public function initialize($context, $parameters = null)52 {53 // disable auto_start54 $parameters['auto_start'] = false;55 56 // initialize the parent57 parent::initialize($context, $parameters);58 59 if (!$this->getParameterHolder()->has('db_table'))60 {61 // missing required 'db_table' parameter62 throw new sfInitializationException('Factory configuration file is missing required "db_table" parameter for the Storage category.');63 }64 65 // use this object as the session handler66 session_set_save_handler(array($this, 'sessionOpen'),67 array($this, 'sessionClose'),68 array($this, 'sessionRead'),69 array($this, 'sessionWrite'),70 array($this, 'sessionDestroy'),71 array($this, 'sessionGC'));72 73 // start our session74 session_start();75 }76 77 /**78 * Closes a session.79 *80 * @return boolean true, if the session was closed, otherwise false81 */82 public function sessionClose()83 {84 // do nothing85 return true;86 }87 88 25 /** 89 26 * Destroys a session. … … 98 35 { 99 36 // get table/column 100 $db_table = $this->getParameter Holder()->get('db_table');101 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');37 $db_table = $this->getParameter('db_table'); 38 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 102 39 103 40 // cleanup the session id, just in case 104 $id = mysql_real_escape_string($id, $this-> resource);41 $id = mysql_real_escape_string($id, $this->db); 105 42 106 43 // delete the record associated with this id 107 44 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\''; 108 45 109 if (@mysql_query($sql, $this-> resource))46 if (@mysql_query($sql, $this->db)) 110 47 { 111 48 return true; … … 113 50 114 51 // failed to destroy session 115 throw new sfDatabaseException(sprintf(' MySQLSessionStorage cannot destroy session id "%s".', $id));52 throw new sfDatabaseException(sprintf('sfMySQLSessionStorage cannot destroy session id "%s".', $id)); 116 53 } 117 54 … … 127 64 public function sessionGC($lifetime) 128 65 { 129 // determine deletable session time130 $time = time() - $lifetime;131 132 66 // get table/column 133 $db_table = $this->getParameter Holder()->get('db_table');134 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');67 $db_table = $this->getParameter('db_table'); 68 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 135 69 136 70 // delete the record associated with this id 137 $sql = 'DELETE FROM '.$db_table.' '. 138 'WHERE '.$db_time_col.' < '.$time; 71 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime); 139 72 140 if ( @mysql_query($sql, $this->resource))73 if (!@mysql_query($sql, $this->db)) 141 74 { 142 return true;75 throw new sfDatabaseException('sfMySQLSessionStorage cannot delete old sessions.'); 143 76 } 144 145 // failed to cleanup old sessions146 throw new sfDatabaseException('MySQLSessionStorage cannot delete old sessions.');147 }148 149 /**150 * Opens a session.151 *152 * @param string153 * @param string154 *155 * @return boolean true, if the session was opened, otherwise an exception is thrown156 *157 * @throws <b>sfDatabaseException</b> If a connection with the database does not exist or cannot be created158 */159 public function sessionOpen($path, $name)160 {161 // what database are we using?162 $database = $this->getParameterHolder()->get('database', 'default');163 164 // get the database resource165 $this->resource = $this->context->getDatabaseManager()->getDatabase($database)->getResource();166 77 167 78 return true; … … 180 91 { 181 92 // get table/column 182 $db_table = $this->getParameter Holder()->get('db_table');183 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');184 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');185 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');93 $db_table = $this->getParameter('db_table'); 94 $db_data_col = $this->getParameter('db_data_col', 'sess_data'); 95 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 96 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 186 97 187 98 // cleanup the session id, just in case 188 $id = mysql_real_escape_string($id, $this-> resource);99 $id = mysql_real_escape_string($id, $this->db); 189 100 190 101 // delete the record associated with this id 191 $sql = 'SELECT '.$db_data_col.' ' . 192 'FROM '.$db_table.' ' . 193 'WHERE '.$db_id_col.' = \''.$id.'\''; 102 $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\''; 194 103 195 $result = @mysql_query($sql, $this-> resource);104 $result = @mysql_query($sql, $this->db); 196 105 197 106 if ($result != false && @mysql_num_rows($result) == 1) … … 205 114 { 206 115 // session does not exist, create it 207 $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', ' . 208 $db_data_col.', '.$db_time_col.') VALUES (' . 209 '\''.$id.'\', \'\', '.time().')'; 116 $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (\''.$id.'\', \'\', '.time().')'; 210 117 211 if (@mysql_query($sql, $this-> resource))118 if (@mysql_query($sql, $this->db)) 212 119 { 213 120 return ''; … … 215 122 216 123 // can't create record 217 throw new sfDatabaseException(sprintf(' MySQLSessionStorage cannot create new record for id "%s".', $id));124 throw new sfDatabaseException(sprintf('sfMySQLSessionStorage cannot create new record for id "%s".', $id)); 218 125 } 219 126 } … … 232 139 { 233 140 // get table/column 234 $db_table = $this->getParameter Holder()->get('db_table');235 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');236 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');237 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');141 $db_table = $this->getParameter('db_table'); 142 $db_data_col = $this->getParameter('db_data_col', 'sess_data'); 143 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 144 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 238 145 239 146 // cleanup the session id and data, just in case 240 $id = mysql_real_escape_string($id, $this-> resource);241 $data = mysql_real_escape_string($data, $this-> resource);147 $id = mysql_real_escape_string($id, $this->db); 148 $data = mysql_real_escape_string($data, $this->db); 242 149 243 150 // delete the record associated with this id 244 $sql = 'UPDATE '.$db_table.' ' . 245 'SET '.$db_data_col.' = \''.$data.'\', ' . 246 $db_time_col.' = '.time().' ' . 247 'WHERE '.$db_id_col.' = \''.$id.'\''; 151 $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = \''.$data.'\', '.$db_time_col.' = '.time().' WHERE '.$db_id_col.' = \''.$id.'\''; 248 152 249 if (@mysql_query($sql, $this-> resource))153 if (@mysql_query($sql, $this->db)) 250 154 { 251 155 return true; … … 253 157 254 158 // failed to write session data 255 throw new sfDatabaseException(sprintf('MySQLSessionStorage cannot write session data for id "%s".', $id)); 256 } 257 258 /** 259 * Executes the shutdown procedure. 260 * 261 */ 262 public function shutdown() 263 { 159 throw new sfDatabaseException(sprintf('sfMySQLSessionStorage cannot write session data for id "%s".', $id)); 264 160 } 265 161 } trunk/lib/storage/sfPDOSessionStorage.class.php
r4887 r4890 13 13 * Provides support for session storage using a PDO database abstraction layer. 14 14 * 15 * <b>Required parameters:</b> 16 * 17 * # <b>db_table</b> - [none] - The database table in which session data will be stored. 18 * 19 * <b>Optional parameters:</b> 20 * 21 * # <b>database</b> - [default] - The database connection to use (see databases.yml). 22 * # <b>db_id_col</b> - [sess_id] - The database column in which the session id will be stored. 23 * # <b>db_data_col</b> - [sess_data] - The database column in which the session data will be stored. 24 * # <b>db_time_col</b> - [sess_time] - The database column in which the session timestamp will be stored. 25 * # <b>session_name</b> - [symfony] - The name of the session. 15 * <b>parameters:</b> see sfDatabaseSessionStorage 26 16 * 27 17 * @package symfony … … 32 22 * @version SVN: $Id$ 33 23 */ 34 class sfPDOSessionStorage extends sf SessionStorage24 class sfPDOSessionStorage extends sfDatabaseSessionStorage 35 25 { 36 /**37 * PDO connection38 * @var Connection39 */40 protected $db;41 42 /**43 * Initializes this Storage instance.44 *45 * @param sfContext A sfContext instance46 * @param array An associative array of initialization parameters47 *48 * @return boolean true, if initialization completes successfully, otherwise false49 *50 * @throws <b>InitializationException</b> If an error occurs while initializing this Storage51 */52 public function initialize($context, $parameters = null)53 {54 // disable auto_start55 $parameters['auto_start'] = false;56 57 // initialize the parent58 parent::initialize($context, $parameters);59 60 if (!$this->getParameterHolder()->has('db_table'))61 {62 // missing required 'db_table' parameter63 $error = 'Factory configuration file is missing required "db_table" parameter for the Storage category';64 65 throw new sfInitializationException($error);66 }67 68 // use this object as the session handler69 session_set_save_handler(array($this, 'sessionOpen'),70 array($this, 'sessionClose'),71 array($this, 'sessionRead'),72 array($this, 'sessionWrite'),73 array($this, 'sessionDestroy'),74 array($this, 'sessionGC'));75 76 // start our session77 session_start();78 }79 80 /**81 * Closes a session.82 *83 * @return boolean true, if the session was closed, otherwise false84 */85 public function sessionClose()86 {87 // do nothing88 return true;89 }90 91 26 /** 92 27 * Destroys a session. … … 101 36 { 102 37 // get table/column 103 $db_table = $this->getParameter Holder()->get('db_table');104 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');38 $db_table = $this->getParameter('db_table'); 39 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 105 40 106 41 // delete the record associated with this id … … 110 45 { 111 46 $stmt = $this->db->prepare($sql); 112 $stmt->bindParam(1, $id, PDO::PARAM_STR); // setString(1, $id);47 $stmt->bindParam(1, $id, PDO::PARAM_STR); 113 48 $stmt->execute(); 114 49 } 115 50 catch (PDOException $e) 116 51 { 117 $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); 118 119 throw new sfDatabaseException($error); 52 throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); 120 53 } 121 54 } … … 132 65 public function sessionGC($lifetime) 133 66 { 134 // determine deletable session time135 $time = time() - $lifetime;136 137 67 // get table/column 138 $db_table = $this->getParameter Holder()->get('db_table');139 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');68 $db_table = $this->getParameter('db_table'); 69 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 140 70 141 71 // delete the record associated with this id 142 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '. $time;72 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime); 143 73 144 74 try 145 75 { 146 76 $this->db->query($sql); 147 return true;148 77 } 149 78 catch (PDOException $e) 150 79 { 151 $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); 152 153 throw new sfDatabaseException($error); 154 } 155 } 156 157 /** 158 * Opens a session. 159 * 160 * @param string 161 * @param string 162 * 163 * @return boolean true, if the session was opened, otherwise an exception is thrown 164 * 165 * @throws <b>DatabaseException</b> If a connection with the database does not exist or cannot be created 166 */ 167 public function sessionOpen($path, $name) 168 { 169 // what database are we using? 170 $database = $this->getParameterHolder()->get('database', 'default'); 171 172 $this->db = $this->context->getDatabaseManager()->getDatabase($database)->getResource(); 173 if ($this->db == null || !$this->db instanceof PDO) 174 { 175 $error = 'PDO dabatase connection doesn\'t exist. Unable to open session.'; 176 177 throw new sfDatabaseException($error); 80 throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); 178 81 } 179 82 … … 193 96 { 194 97 // get table/columns 195 $db_table = $this->getParameter Holder()->get('db_table');196 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');197 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');198 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');98 $db_table = $this->getParameter('db_table'); 99 $db_data_col = $this->getParameter('db_data_col', 'sess_data'); 100 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 101 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 199 102 200 103 try … … 216 119 217 120 $stmt = $this->db->prepare($sql); 218 $stmt->bindParam(1, $id, PDO::PARAM_STR); // setString(1, $id);219 $stmt->bindValue(2, '', PDO::PARAM_STR); // setString(2, '');220 $stmt->bindValue(3, time(), PDO::PARAM_INT); // setInt(3, time());121 $stmt->bindParam(1, $id, PDO::PARAM_STR); 122 $stmt->bindValue(2, '', PDO::PARAM_STR); 123 $stmt->bindValue(3, time(), PDO::PARAM_INT); 221 124 $stmt->execute(); 222 125 … … 226 129 catch (PDOException $e) 227 130 { 228 $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); 229 230 throw new sfDatabaseException($error); 131 throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); 231 132 } 232 133 } … … 245 146 { 246 147 // get table/column 247 $db_table = $this->getParameter Holder()->get('db_table');248 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');249 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');250 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');148 $db_table = $this->getParameter('db_table'); 149 $db_data_col = $this->getParameter('db_data_col', 'sess_data'); 150 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 151 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 251 152 252 153 $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?'; … … 255 156 { 256 157 $stmt = $this->db->prepare($sql); 257 $stmt->bindParam(1, $data, PDO::PARAM_STR); // setString(1, $data);258 $stmt->bindParam(2, $id, PDO::PARAM_STR); // setString(2, $id);158 $stmt->bindParam(1, $data, PDO::PARAM_STR); 159 $stmt->bindParam(2, $id, PDO::PARAM_STR); 259 160 $stmt->execute(); 260 return true;261 161 } 262 263 162 catch (PDOException $e) 264 163 { 265 $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); 266 267 throw new sfDatabaseException($error); 164 throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); 268 165 } 269 166 270 167 return false; 271 168 } 272 273 /**274 * Executes the shutdown procedure.275 *276 */277 public function shutdown()278 {279 }280 169 } trunk/lib/storage/sfPostgreSQLSessionStorage.class.php
r4887 r4890 13 13 * Provides support for session storage using a PostgreSQL brand database. 14 14 * 15 * <b>Required parameters:</b> 16 * 17 * # <b>db_table</b> - [none] - The database table in which session data will be stored. 18 * 19 * <b>Optional parameters:</b> 20 * 21 * # <b>db_id_col</b> - [sess_id] - The database column in which the 22 * session id will be stored. 23 * # <b>db_data_col</b> - [sess_data] - The database column in which the 24 * session data will be stored. 25 * # <b>db_time_col</b> - [sess_time] - The database column in which the 26 * session timestamp will be stored. 27 * # <b>session_name</b> - [symfony] - The name of the session. 15 * <b>parameters:</b> see sfDatabaseSessionStorage 28 16 * 29 17 * @package symfony … … 33 21 * @version SVN: $Id$ 34 22 */ 35 class sfPostgreSQLSessionStorage extends sf SessionStorage23 class sfPostgreSQLSessionStorage extends sfDatabaseSessionStorage 36 24 { 37 protected38 $resource = null;39 40 /**41 * Initializes this Storage instance.42 *43 * @param sfContext A sfContext instance44 * @param array An associative array of initialization parameters45 *46 * @return boolean true, if initialization completes successfully, otherwise false47 *48 * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage49 */50 public function initialize($context, $parameters = null)51 {52 // disable auto_start53 $parameters['auto_start'] = false;54 55 // initialize the parent56 parent::initialize($context, $parameters);57 58 if (!$this->getParameterHolder()->has('db_table'))59 {60 // missing required 'db_table' parameter61 throw new sfInitializationException('Factory configuration file is missing required "db_table" parameter for the Storage category.');62 }63 64 // use this object as the session handler65 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 session73 session_start();74 }75 76 /**77 * Closes a session.78 *79 * @return boolean true, if the session was closed, otherwise false80 */81 public function sessionClose()82 {83 // do nothing84 return true;85 }86 87 25 /** 88 26 * Destroys a session. … … 97 35 { 98 36 // get table/column 99 $db_table = $this->getParameter Holder()->get('db_table');100 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');37 $db_table = $this->getParameter('db_table'); 38 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 101 39 102 40 // cleanup the session id, just in case … … 106 44 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\''; 107 45 108 if (@pg_query($this-> resource, $sql))46 if (@pg_query($this->db, $sql)) 109 47 { 110 48 return true; … … 112 50 113 51 // failed to destroy session 114 throw new sfDatabaseException(sprintf(' PostgreSQLSessionStorage cannot destroy session id "%s".', $id));52 throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot destroy session id "%s".', $id)); 115 53 } 116 54 … … 126 64 public function sessionGC($lifetime) 127 65 { 128 // determine deletable session time129 $time = time() - $lifetime;130 131 66 // get table/column 132 $db_table = $this->getParameter Holder()->get('db_table');133 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');67 $db_table = $this->getParameter('db_table'); 68 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 134 69 135 70 // delete the record associated with this id 136 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '. $lifetime;71 $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime); 137 72 138 if ( @pg_query($this->resource, $sql))73 if (!@pg_query($this->db, $sql)) 139 74 { 140 return true;75 throw new sfDatabaseException('sfPostgreSQLSessionStorage cannot delete old sessions.'); 141 76 } 142 143 // failed to cleanup old sessions144 throw new sfDatabaseException('PostgreSQLSessionStorage cannot delete old sessions.');145 }146 147 /**148 * Opens a session.149 *150 * @param string151 * @param string152 *153 * @return boolean true, if the session was opened, otherwise an exception is thrown154 *155 * @throws <b>sfDatabaseException</b> If a connection with the database does156 * not exist or cannot be created157 */158 public function sessionOpen($path, $name)159 {160 // what database are we using?161 $database = $this->getParameterHolder()->get('database', 'default');162 163 // get the database resource164 $this->resource = $this->context->getDatabaseManager()->getDatabase($database)->getResource();165 77 166 78 return true; … … 179 91 { 180 92 // get table/column 181 $db_table = $this->getParameter Holder()->get('db_table');182 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');183 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');184 $db_time_col = $this->getParameter Holder()->get('db_time_col', 'sess_time');93 $db_table = $this->getParameter('db_table'); 94 $db_data_col = $this->getParameter('db_data_col', 'sess_data'); 95 $db_id_col = $this->getParameter('db_id_col', 'sess_id'); 96 $db_time_col = $this->getParameter('db_time_col', 'sess_time'); 185 97 186 98 // cleanup the session id, just in case … … 188 100 189 101 // delete the record associated with this id 190 $sql = 'SELECT '.$db_data_col.' ' . 191 'FROM '.$db_table.' ' . 192 'WHERE '.$db_id_col.' = \''.$id.'\''; 102 $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\''; 193 103 194 $result = @pg_query($this-> resource, $sql);104 $result = @pg_query($this->db, $