123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- abstract class sfDatabaseSessionStorage extends sfSessionStorage
- {
- protected
- $db = null,
- $con = null;
-
- public function initialize($options = array())
- {
- $options = array_merge(array(
- 'db_id_col' => 'sess_id',
- 'db_data_col' => 'sess_data',
- 'db_time_col' => 'sess_time',
- ), $options);
-
- $options['auto_start'] = false;
-
- parent::initialize($options);
- if (!isset($this->options['db_table']))
- {
- throw new sfInitializationException('You must provide a "db_table" option to sfDatabaseSessionStorage.');
- }
- if (!isset($this->options['database']))
- {
- throw new sfInitializationException('You must provide a "database" option to sfDatabaseSessionStorage.');
- }
-
- session_set_save_handler(array($this, 'sessionOpen'),
- array($this, 'sessionClose'),
- array($this, 'sessionRead'),
- array($this, 'sessionWrite'),
- array($this, 'sessionDestroy'),
- array($this, 'sessionGC'));
-
- session_start();
- }
-
- public function sessionClose()
- {
-
- return true;
- }
-
- public function sessionOpen($path = null, $name = null)
- {
-
- $database = $this->options['database'];
-
- $databaseClass = get_class($database);
- if($databaseClass == 'sfPropelDatabase')
- {
- $this->db = Propel::getConnection();
- }
- elseif($databaseClass == 'sfDoctrineDatabase')
- {
- $this->db = $database->getConnection();
- }
- else
- {
- $this->db = $database->getResource();
- }
- $this->con = $database->getConnection();
- if (is_null($this->db) && is_null($this->con))
- {
- throw new sfDatabaseException('Database connection does not exist. Unable to open session.');
- }
- return true;
- }
-
- abstract public function sessionDestroy($id);
-
- abstract public function sessionGC($lifetime);
-
- abstract public function sessionRead($id);
-
- abstract public function sessionWrite($id, $data);
-
- public function regenerate($destroy = false)
- {
- if (self::$sessionIdRegenerated)
- {
- return;
- }
- $currentId = session_id();
- parent::regenerate($destroy);
- $newId = session_id();
- $this->sessionRead($newId);
- return $this->sessionWrite($newId, $this->sessionRead($currentId));
- }
-
- public function shutdown()
- {
- }
- }
|