| 
									
										
										
										
											2025-04-24 10:57:08 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace App\Core; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Exception; | 
					
						
							|  |  |  | use Feedback; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DatabaseConnector | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Connect to the database using given configuration and handle errors. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $config | 
					
						
							|  |  |  |      * @return mixed Database connection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function connect(array $config) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Load DB classes
 | 
					
						
							|  |  |  |         require_once __DIR__ . '/../classes/database.php'; | 
					
						
							|  |  |  |         require_once __DIR__ . '/../includes/database.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $db = connectDB($config); | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |             if (!$db) { | 
					
						
							| 
									
										
										
										
											2025-04-24 10:57:08 +00:00
										 |  |  |                 throw new Exception('Could not connect to database'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |             return $db; | 
					
						
							| 
									
										
										
										
											2025-04-24 10:57:08 +00:00
										 |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             // Show error and exit
 | 
					
						
							|  |  |  |             Feedback::flash('ERROR', 'DEFAULT', getError('Error connecting to the database.', $e->getMessage())); | 
					
						
							|  |  |  |             include __DIR__ . '/../templates/page-header.php'; | 
					
						
							|  |  |  |             include __DIR__ . '/../helpers/feedback.php'; | 
					
						
							|  |  |  |             include __DIR__ . '/../templates/page-footer.php'; | 
					
						
							|  |  |  |             exit(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |