Changeset 4890
- Timestamp:
- 08/23/07 17:48:11 (2 years 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, $sql); 195 105 196 106 if ($result != false && @pg_num_rows($result) == 1) … … 204 114 { 205 115 // session does not exist, create it 206 $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', ' . 207 $db_data_col.', '.$db_time_col.') VALUES (' . 208 '\''.$id.'\', \'\', '.time().')'; 116 $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (\''.$id.'\', \'\', '.time().')'; 209 117 210 if (@pg_query($this-> resource, $sql))118 if (@pg_query($this->db, $sql)) 211 119 { 212 120 return ''; … … 214 122 215 123 // can't create record 216 throw new sfDatabaseException(sprintf(' PostgreSQLSessionStorage cannot create new record for id "%s".', $id));124 throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot create new record for id "%s".', $id)); 217 125 } 218 126 } … … 231 139 { 232 140 // get table/column 233 $db_table = $this->getParameter Holder()->get('db_table');234 $db_data_col = $this->getParameter Holder()->get('db_data_col', 'sess_data');235 $db_id_col = $this->getParameter Holder()->get('db_id_col', 'sess_id');236 $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'); 237 145 238 146 // cleanup the session id and data, just in case … … 241 149 242 150 // delete the record associated with this id 243 $sql = 'UPDATE '.$db_table.' '. 244 'SET '.$db_data_col.' = \''.$data.'\', '. 245 $db_time_col.' = '.time().' '. 246 'WHERE '.$db_id_col.' = \''.$id.'\''; 151 $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = \''.$data.'\', '.$db_time_col.' = '.time().' WHERE '.$db_id_col.' = \''.$id.'\''; 247 152 248 if (@pg_query($this-> resource, $sql))153 if (@pg_query($this->db, $sql)) 249 154 { 250 155 return true; … … 252 157 253 158 // failed to write session data 254 throw new sfDatabaseException(sprintf('PostgreSQLSessionStorage cannot write session data for id "%s".', $id)); 255 } 256 257 /** 258 * Executes the shutdown procedure. 259 * 260 */ 261 public function shutdown() 262 { 159 throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot write session data for id "%s".', $id)); 263 160 } 264 161 } trunk/lib/storage/sfSessionStorage.class.php
r4887 r4890 29 29 * Initializes this Storage instance. 30 30 * 31 * @param sfContext A sfContext instance32 31 * @param array An associative array of initialization parameters 33 32 * … … 36 35 * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage 37 36 */ 38 public function initialize($ context, $parameters = null)37 public function initialize($parameters = null) 39 38 { 40 39 // initialize parent 41 parent::initialize($ context, $parameters);40 parent::initialize($parameters); 42 41 43 42 // set session name 44 $sessionName = $this->getParameter Holder()->get('session_name', 'symfony');43 $sessionName = $this->getParameter('session_name', 'symfony'); 45 44 46 45 session_name($sessionName); … … 48 47 if (!(boolean) ini_get('session.use_cookies')) 49 48 { 50 $sessionId = $context->getRequest()->getParameter($sessionName, ''); 51 52 if ($sessionId != '') 49 if ($sessionId = $this->getParameter('session_id')) 53 50 { 54 51 session_id($sessionId); … … 87 84 * @return mixed Data associated with the key 88 85 */ 89 public function &read($key)86 public function read($key) 90 87 { 91 88 $retval = null; … … 93 90 if (isset($_SESSION[$key])) 94 91 { 95 $retval = &$_SESSION[$key];92 $retval = $_SESSION[$key]; 96 93 } 97 94 … … 108 105 * @return mixed Data associated with the key 109 106 */ 110 public function &remove($key)107 public function remove($key) 111 108 { 112 109 $retval = null; … … 114 111 if (isset($_SESSION[$key])) 115 112 { 116 $retval = &$_SESSION[$key];113 $retval = $_SESSION[$key]; 117 114 unset($_SESSION[$key]); 118 115 } … … 130 127 * 131 128 */ 132 public function write($key, &$data)129 public function write($key, $data) 133 130 { 134 $_SESSION[$key] = &$data;131 $_SESSION[$key] = $data; 135 132 } 136 133 trunk/lib/storage/sfSessionTestStorage.class.php
r3329 r4890 21 21 protected 22 22 $sessionId = null, 23 $sessionData = array(), 24 $sessionPath = null; 23 $sessionData = array(); 25 24 26 25 /** 27 26 * Initializes this Storage instance. 28 27 * 29 * @param sfContext A sfContext instance30 28 * @param array An associative array of initialization parameters 31 29 * … … 34 32 * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage 35 33 */ 36 public function initialize($ context, $parameters = null)34 public function initialize($parameters = null) 37 35 { 38 36 // initialize parent 39 parent::initialize($ context, $parameters);37 parent::initialize($parameters); 40 38 41 $this->sessionPath = sfConfig::get('sf_test_cache_dir').DIRECTORY_SEPARATOR.'sessions'; 39 if (!$this->getParameter('session_path')) 40 { 41 throw new sfInitializationException('Factory configuration file is missing required "session_path" parameter for the "storage" category.'); 42 } 42 43 43 44 if (array_key_exists('session_id', $_SERVER)) … … 46 47 47 48 // we read session data from temp file 48 $file = $this-> sessionPath.DIRECTORY_SEPARATOR.$this->sessionId.'.session';49 $file = $this->getParameter('session_path').DIRECTORY_SEPARATOR.$this->sessionId.'.session'; 49 50 $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array(); 50 51 } … … 75 76 * @return mixed Data associated with the key 76 77 */ 77 public function &read($key)78 public function read($key) 78 79 { 79 80 $retval = null; … … 81 82 if (isset($this->sessionData[$key])) 82 83 { 83 $retval = &$this->sessionData[$key];84 $retval = $this->sessionData[$key]; 84 85 } 85 86 … … 96 97 * @return mixed Data associated with the key 97 98 */ 98 public function &remove($key)99 public function remove($key) 99 100 { 100 101 $retval = null; … … 102 103 if (isset($this->sessionData[$key])) 103 104 { 104 $retval = &$this->sessionData[$key];105 $retval = $this->sessionData[$key]; 105 106 unset($this->sessionData[$key]); 106 107 } … … 118 119 * 119 120 */ 120 public function write($key, &$data)121 public function write($key, $data) 121 122 { 122 $this->sessionData[$key] = &$data;123 $this->sessionData[$key] = $data; 123 124 } 124 125 … … 128 129 public function clear() 129 130 { 130 sfToolkit::clearDirectory($this-> sessionPath);131 sfToolkit::clearDirectory($this->getParameter('session_path')); 131 132 } 132 133 … … 140 141 { 141 142 $current_umask = umask(0000); 142 if (!is_dir($this-> sessionPath))143 if (!is_dir($this->getParameter('session_path'))) 143 144 { 144 mkdir($this-> sessionPath, 0777, true);145 mkdir($this->getParameter('session_path'), 0777, true); 145 146 } 146 147 umask($current_umask); 147 file_put_contents($this-> sessionPath.DIRECTORY_SEPARATOR.$this->sessionId.'.session', serialize($this->sessionData));148 file_put_contents($this->getParameter('session_path').DIRECTORY_SEPARATOR.$this->sessionId.'.session', serialize($this->sessionData)); 148 149 $this->sessionId = ''; 149 150 $this->sessionData = array(); trunk/lib/storage/sfStorage.class.php
r4597 r4890 22 22 { 23 23 protected 24 $parameterHolder = null, 25 $context = null; 26 27 /** 28 * Retrieves the current application context. 29 * 30 * @return sfContext A sfContext instance 31 */ 32 public function getContext() 33 { 34 return $this->context; 35 } 24 $parameterHolder = null; 36 25 37 26 /** 38 27 * Initializes this Storage instance. 39 28 * 40 * @param sfContext A sfContext instance41 29 * @param array An associative array of initialization parameters 42 30 * … … 45 33 * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfStorage 46 34 */ 47 public function initialize($ context, $parameters = array())35 public function initialize($parameters = array()) 48 36 { 49 $this->context = $context;50 51 37 $this->parameterHolder = new sfParameterHolder(); 52 38 $this->getParameterHolder()->add($parameters); … … 85 71 * @throws <b>sfStorageException</b> If an error occurs while reading data from this storage 86 72 */ 87 abstract function &read($key);73 abstract public function read($key); 88 74 89 75 /** … … 98 84 * @throws <b>sfStorageException</b> If an error occurs while removing data from this storage 99 85 */ 100 abstract function &remove($key);86 abstract public function remove($key); 101 87 102 88 /** … … 105 91 * @throws <b>sfStorageException</b> If an error occurs while shutting down this storage 106 92 */ 107 abstract function shutdown();93 abstract public function shutdown(); 108 94 109 95 /** … … 117 103 * @throws <b>sfStorageException</b> If an error occurs while writing to this storage 118 104 */ 119 abstract function write($key, &$data);105 abstract public function write($key, $data); 120 106 121 107 /** trunk/test/functional/fixtures/project/apps/backend/config/factories.yml
r2217 r4890 8 8 storage: 9 9 class: sfSessionTestStorage 10 param: 11 session_path: %SF_TEST_CACHE_DIR%/sessions 10 12 11 13 all: trunk/test/functional/fixtures/project/apps/cache/config/factories.yml
r2169 r4890 8 8 storage: 9 9 class: sfSessionTestStorage 10 param: 11 session_path: %SF_TEST_CACHE_DIR%/sessions 10 12 11 13 all: trunk/test/functional/fixtures/project/apps/crud/config/factories.yml
r2126 r4890 8 8 storage: 9 9 class: sfSessionTestStorage 10 param: 11 session_path: %SF_TEST_CACHE_DIR%/sessions 10 12 11 13 all: trunk/test/functional/fixtures/project/apps/frontend/config/factories.yml
r2069 r4890 8 8 storage: 9 9 class: sfSessionTestStorage 10 param: 11 session_path: %SF_TEST_CACHE_DIR%/sessions 10 12 11 13 all: trunk/test/functional/fixtures/project/apps/i18n/config/factories.yml
r2740 r4890 8 8 storage: 9 9 class: sfSessionTestStorage 10 param: 11 session_path: %SF_TEST_CACHE_DIR%/sessions 10 12 11 13 all: trunk/test/unit/sfContextMock.class.php
r4440 r4890 29 29 30 30 self::$instance->storage = sfStorage::newInstance('sfSessionTestStorage'); 31 self::$instance->storage->initialize( self::$instance);31 self::$instance->storage->initialize(array('session_path' => sfConfig::get('sf_test_cache_dir').'/sessions')); 32 32 33 33 foreach ($factories as $type => $class) trunk/test/unit/storage/sfStorageTest.php
r4440 r4890 10 10 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 require_once($_test_dir.'/unit/sfContextMock.class.php');13 12 14 $t = new lime_test(1 9, new lime_output_color());13 $t = new lime_test(17, new lime_output_color()); 15 14 16 15 class myStorage extends sfStorage 17 16 { 18 function &read($key) {}19 function &remove($key) {}20 function shutdown() {}21 function write($key, &$data) {}17 public function read($key) {} 18 public function remove($key) {} 19 public function shutdown() {} 20 public function write($key, $data) {} 22 21 } 23 22 … … 25 24 { 26 25 } 27 28 $context = sfContext::getInstance(array('storage' => 'myStorage'));29 26 30 27 // ::newInstance() … … 46 43 $t->diag('->initialize()'); 47 44 $storage = sfStorage::newInstance('myStorage'); 48 $t->is($storage->getContext(), null, '->initialize() takes a sfContext object as its first argument'); 49 $storage->initialize($context, array('foo' => 'bar')); 45 $storage->initialize(array('foo' => 'bar')); 50 46 $t->is($storage->getParameter('foo'), 'bar', '->initialize() takes an array of parameters as its second argument'); 51 47 52 48 $storage = new myStorage(); 53 $storage->initialize($context); 54 55 // ->getContext() 56 $t->diag('->getContext()'); 57 $storage->initialize($context); 58 $t->is($storage->getContext(), $context, '->getContext() returns the current context'); 49 $storage->initialize(); 59 50 60 51 // parameter holder proxy trunk/test/unit/user/sfUserTest.php
r4440 r4890 69 69 $context->getUser()->shutdown(); 70 70 $context->getUser()->initialize($context); 71 $parameters = $context->getStorage()->getParameterHolder()->getAll(); 71 72 $context->getStorage()->shutdown(); 72 $context->getStorage()->initialize($ context);73 $context->getStorage()->initialize($parameters); 73 74 }

