db = $db; $this->request = $request; } /** * @return Session_Storage_Interface */ public function create() { if ( $this->can_use_db_session_storage() ) { return new DB_Session_Storage( $this->request->cookie(), $this->db ); } return new In_Memory_Session_Storage(); } /** * @return bool */ private function can_use_db_session_storage() { return $this->tables_exist(); } /** * @return bool */ private function tables_exist() { $table_sessions = $this->get_table_full_name( DB_Session_Storage::TABLE_SESSIONS ); $table_session_variables = $this->get_table_full_name( DB_Session_Storage::TABLE_SESSION_VARIABLES ); $result1 = $this->db->get_var( "SHOW TABLES LIKE '{$table_sessions}'" ); $result2 = $this->db->get_var( "SHOW TABLES LIKE '{$table_session_variables}'" ); return ! is_null( $result1 ) && ! is_null( $result2 ); } /** * @param string $table_name * * @return string */ private function get_table_full_name( $table_name ) { return $this->db->prefix . $table_name; } }