|
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 string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $driver = ''; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $driver |
|
28
|
|
|
* @param array $configuration |
|
29
|
|
|
*/ |
|
30
|
18 |
|
public function __construct($driver, $configuration = []) |
|
31
|
|
|
{ |
|
32
|
18 |
|
$this->driver = $driver; |
|
33
|
18 |
|
$this->configuration = $configuration; |
|
34
|
18 |
|
} |
|
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|null |
|
42
|
|
|
*/ |
|
43
|
18 |
|
public function getDriver($driverName, $configuration = []) |
|
44
|
|
|
{ |
|
45
|
18 |
|
$driver = null; |
|
46
|
18 |
|
$mysql = [ 'mysql', 'mysqli' ]; |
|
47
|
18 |
|
$sqlite = [ 'pdo', 'sqlite', 'sqlite3' ]; |
|
48
|
|
|
|
|
49
|
18 |
|
list($database, $hostname, $username, $password) = $this->parseConfiguration($configuration); |
|
50
|
|
|
|
|
51
|
18 |
|
if (in_array($driverName, $mysql)) { |
|
52
|
9 |
|
$dsn = 'mysql:host=' . $hostname . ';dbname=' . $database; |
|
53
|
9 |
|
$pdo = new \PDO($dsn, $username, $password); |
|
54
|
9 |
|
$driver = new MySQLDriver($pdo, $database); |
|
55
|
9 |
|
} |
|
56
|
|
|
|
|
57
|
18 |
|
if (in_array($driverName, $sqlite)) { |
|
58
|
9 |
|
$pdo = new \PDO($hostname); |
|
59
|
9 |
|
$driver = new SQLiteDriver($pdo); |
|
60
|
9 |
|
} |
|
61
|
|
|
|
|
62
|
18 |
|
return $driver; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns the result. |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
12 |
|
public function getTable($table) |
|
71
|
|
|
{ |
|
72
|
12 |
|
$driver = $this->getDriver($this->driver, $this->configuration); |
|
73
|
|
|
|
|
74
|
12 |
|
return $driver->getTable($table); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Shows the list of tables. |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
6 |
|
public function showTables() |
|
83
|
|
|
{ |
|
84
|
6 |
|
$driver = $this->getDriver($this->driver, $this->configuration); |
|
85
|
|
|
|
|
86
|
6 |
|
return $driver->showTables(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parses the configuration into separate variables. |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $configuration |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
18 |
|
protected function parseConfiguration(array $configuration) |
|
96
|
|
|
{ |
|
97
|
18 |
|
$database = null; |
|
98
|
18 |
|
$hostname = null; |
|
99
|
18 |
|
$password = null; |
|
100
|
18 |
|
$username = null; |
|
101
|
|
|
|
|
102
|
18 |
|
if (isset($configuration['database'])) { |
|
103
|
9 |
|
$database = $configuration['database']; |
|
104
|
9 |
|
} |
|
105
|
|
|
|
|
106
|
18 |
|
if (isset($configuration['hostname'])) { |
|
107
|
18 |
|
$hostname = $configuration['hostname']; |
|
108
|
18 |
|
} |
|
109
|
|
|
|
|
110
|
18 |
|
if (isset($configuration['password'])) { |
|
111
|
9 |
|
$password = $configuration['password']; |
|
112
|
9 |
|
} |
|
113
|
|
|
|
|
114
|
18 |
|
if (isset($configuration['username'])) { |
|
115
|
9 |
|
$username = $configuration['username']; |
|
116
|
9 |
|
} |
|
117
|
|
|
|
|
118
|
18 |
|
return [ $database, $hostname, $username, $password ]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|