| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Rougin\Describe\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Database Driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * A database driver for using available database drivers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package  Describe | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @category Driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author   Rougin Royce Gutib <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class DatabaseDriver implements DriverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $configuration = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var \Rougin\Describe\Driver\DriverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string $driverName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param array  $configuration | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 30 |  |     public function __construct($driverName, $configuration = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 30 |  |         $this->configuration = $configuration; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 30 |  |         $this->driver        = $this->getDriver($driverName, $configuration); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 27 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Gets the specified driver from the specified database connection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param  string $driverName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param  array  $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return \Rougin\Describe\Driver\DriverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 30 |  |     protected function getDriver($driverName, $configuration = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 30 |  |         $mysql  = [ 'mysql', 'mysqli' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 30 |  |         $sqlite = [ 'pdo', 'sqlite', 'sqlite3' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 30 |  |         list($database, $hostname, $username, $password) = $this->parseConfiguration($configuration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 30 |  |         if (in_array($driverName, $mysql)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 15 |  |             $dsn = 'mysql:host=' . $hostname . ';dbname=' . $database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 15 |  |             $pdo = new \PDO($dsn, $username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 15 |  |             return new MySQLDriver($pdo, $database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 15 |  |         if (in_array($driverName, $sqlite)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 12 |  |             $pdo = new \PDO($hostname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 12 |  |             return new SQLiteDriver($pdo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |             $message = 'Specified database driver not found!'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |             throw new \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Returns the result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 18 |  |     public function getTable($table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 18 |  |         return $this->driver->getTable($table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Shows the list of tables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 9 |  |     public function showTables() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |         return $this->driver->showTables(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Parses the configuration into separate variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param  array  $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 30 |  |     protected function parseConfiguration(array $configuration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 30 |  |         $database = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 30 |  |         $hostname = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 30 |  |         $password = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 30 |  |         $username = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 30 |  |         foreach ($configuration as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 27 |  |             if (isset($configuration[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 27 |  |                 $$key = $configuration[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 27 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 30 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 30 |  |         return [ $database, $hostname, $username, $password ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |