Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DbTable.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class DbTable extends \SessionHandler
16 {
22  protected $_sessionTable;
23 
29  protected $connection;
30 
36  public function __construct(\Magento\Framework\App\ResourceConnection $resource)
37  {
38  $this->_sessionTable = $resource->getTableName('session');
39  $this->connection = $resource->getConnection();
40  $this->checkConnection();
41  }
42 
49  protected function checkConnection()
50  {
51  if (!$this->connection) {
52  throw new SessionException(
53  new Phrase("The write connection to the database isn't available. Please try again later.")
54  );
55  }
56  if (!$this->connection->isTableExists($this->_sessionTable)) {
57  throw new SessionException(
58  new Phrase("The database storage table doesn't exist. Verify the table and try again.")
59  );
60  }
61  }
62 
71  public function open($savePath, $sessionName)
72  {
73  return true;
74  }
75 
81  public function close()
82  {
83  return true;
84  }
85 
92  public function read($sessionId)
93  {
94  // need to use write connection to get the most fresh DB sessions
95  $select = $this->connection->select()->from(
96  $this->_sessionTable,
97  ['session_data']
98  )->where(
99  'session_id = :session_id'
100  );
101  $bind = ['session_id' => $sessionId];
102  $data = $this->connection->fetchOne($select, $bind);
103 
104  // check if session data is a base64 encoded string
105  $decodedData = base64_decode($data, true);
106  if ($decodedData !== false) {
107  $data = $decodedData;
108  }
109  return $data;
110  }
111 
119  public function write($sessionId, $sessionData)
120  {
121  // need to use write connection to get the most fresh DB sessions
122  $bindValues = ['session_id' => $sessionId];
123  $select = $this->connection->select()->from($this->_sessionTable)->where('session_id = :session_id');
124  $exists = $this->connection->fetchOne($select, $bindValues);
125 
126  // encode session serialized data to prevent insertion of incorrect symbols
127  $sessionData = base64_encode($sessionData);
128  $bind = ['session_expires' => time(), 'session_data' => $sessionData];
129 
130  if ($exists) {
131  $this->connection->update($this->_sessionTable, $bind, ['session_id=?' => $sessionId]);
132  } else {
133  $bind['session_id'] = $sessionId;
134  $this->connection->insert($this->_sessionTable, $bind);
135  }
136  return true;
137  }
138 
145  public function destroy($sessionId)
146  {
147  $where = ['session_id = ?' => $sessionId];
148  $this->connection->delete($this->_sessionTable, $where);
149  return true;
150  }
151 
159  public function gc($maxLifeTime)
160  {
161  $where = ['session_expires < ?' => time() - $maxLifeTime];
162  $this->connection->delete($this->_sessionTable, $where);
163  return true;
164  }
165 }
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\App\ResourceConnection $resource)
Definition: DbTable.php:36