1 | <?php |
||
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 | 27 | public function __construct($driverName, $configuration = []) |
|
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 | 27 | protected function getDriver($driverName, $configuration = []) |
|
45 | { |
||
46 | 27 | $mysql = [ 'mysql', 'mysqli' ]; |
|
47 | 27 | $sqlite = [ 'pdo', 'sqlite', 'sqlite3' ]; |
|
48 | |||
49 | 27 | list($database, $hostname, $username, $password) = $this->parseConfiguration($configuration); |
|
50 | |||
51 | 27 | if (in_array($driverName, $mysql)) { |
|
52 | 12 | $dsn = 'mysql:host=' . $hostname . ';dbname=' . $database; |
|
53 | 12 | $pdo = new \PDO($dsn, $username, $password); |
|
54 | |||
55 | 12 | 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 | } |
||
63 | |||
64 | 3 | $message = 'Specified database driver not found!'; |
|
65 | |||
66 | 3 | throw new \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException($message); |
|
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 | 6 | public function showTables() |
|
85 | { |
||
86 | 6 | return $this->driver->showTables(); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Parses the configuration into separate variables. |
||
91 | * |
||
92 | * @param array $configuration |
||
93 | * @return array |
||
94 | */ |
||
95 | 27 | protected function parseConfiguration(array $configuration) |
|
110 | } |
||
111 |