1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Morphism; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Configuration; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\DBALException; |
8
|
|
|
use Doctrine\DBAL\DriverManager; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Symfony\Component\Yaml\Parser; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A parser for config files |
14
|
|
|
*/ |
15
|
|
|
class Config |
16
|
|
|
{ |
17
|
|
|
/** @var array */ |
18
|
|
|
private $entries = []; |
19
|
|
|
/** @var string */ |
20
|
|
|
private $path; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor |
24
|
|
|
* |
25
|
|
|
* @param string $path Path to config file. |
26
|
|
|
*/ |
27
|
|
|
public function __construct($path) |
28
|
|
|
{ |
29
|
|
|
$this->path = $path; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Parse the config file provided in the constructor. |
34
|
|
|
*/ |
35
|
|
|
public function parse() |
36
|
|
|
{ |
37
|
|
|
if (!file_exists($this->path)) { |
38
|
|
|
throw new RuntimeException("Specified config file does not exist: {$this->path}"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$parser = new Parser(); |
42
|
|
|
$config = $parser->parse(file_get_contents($this->path)); |
43
|
|
|
|
44
|
|
|
if (!isset($config['databases'])) { |
45
|
|
|
throw new RuntimeException("Missing databases section in '{$this->path}'"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$entries = []; |
49
|
|
|
foreach ($config['databases'] as $connectionName => $entry) { |
50
|
|
|
if (empty($entry['morphism']['enable'])) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
$morphism = $entry['morphism']; |
54
|
|
|
unset($entry['morphism']); |
55
|
|
|
|
56
|
|
|
if (!isset($entry['dbname'])) { |
57
|
|
|
$entry['dbname'] = $connectionName; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$matchTables = []; |
61
|
|
|
foreach (['include', 'exclude'] as $key) { |
62
|
|
|
$regex = ''; |
63
|
|
|
if (!empty($morphism["{$key}"])) { |
64
|
|
|
$regex = '/^(' . implode('|', $morphism["{$key}"]) . ')$/'; |
65
|
|
|
} |
66
|
|
|
$matchTables[$key] = $regex; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$schemaDefinitionPaths = $morphism['schemaDefinitionPath']; |
70
|
|
|
if (!$schemaDefinitionPaths) { |
71
|
|
|
$schemaDefinitionPaths = 'schema/'.$connectionName; |
72
|
|
|
} |
73
|
|
|
if (!is_array($schemaDefinitionPaths)) { |
74
|
|
|
$schemaDefinitionPaths = [ $schemaDefinitionPaths ]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$entries[$connectionName] = [ |
78
|
|
|
'connection' => $entry, |
79
|
|
|
'morphism' => [ |
80
|
|
|
'matchTables' => $matchTables, |
81
|
|
|
'schemaDefinitionPath' => $schemaDefinitionPaths, |
82
|
|
|
], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
$this->entries = $entries; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the parameters to open the named database connection, suitable |
90
|
|
|
* for passing to \Doctrine\DBAL\DriverManager::getConnection(). |
91
|
|
|
* |
92
|
|
|
* Some subset of these parameters will be returned: |
93
|
|
|
* |
94
|
|
|
* 'driver' => $pdo_driver |
95
|
|
|
* 'dbname' => $dbname |
96
|
|
|
* 'user' => $user |
97
|
|
|
* 'password' => $password |
98
|
|
|
* 'host' => $host |
99
|
|
|
* 'port' => $port |
100
|
|
|
* 'unix_socket' => $socket |
101
|
|
|
* |
102
|
|
|
* @param string $connectionName Name of the connection to look up |
103
|
|
|
* @return array ['connection' => [$param => $value, ...], 'morphism' => [ ... ] ] |
104
|
|
|
*/ |
105
|
|
|
public function getEntry($connectionName) |
106
|
|
|
{ |
107
|
|
|
if (!isset($this->entries[$connectionName])) { |
108
|
|
|
throw new RuntimeException("Unknown connection '$connectionName'"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->entries[$connectionName]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns a new database connection using the parameters named by |
116
|
|
|
* connectionName. |
117
|
|
|
* |
118
|
|
|
* @param string $connectionName |
119
|
|
|
* @return Connection |
120
|
|
|
* @throws DBALException |
121
|
|
|
*/ |
122
|
|
|
public function getConnection($connectionName) |
123
|
|
|
{ |
124
|
|
|
$entry = $this->getEntry($connectionName); |
125
|
|
|
$dbalConfig = new Configuration(); |
126
|
|
|
return DriverManager::getConnection($entry['connection'], $dbalConfig); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the names of all the connections specified in the config file. |
131
|
|
|
* |
132
|
|
|
* @return string[] |
133
|
|
|
*/ |
134
|
|
|
public function getConnectionNames() |
135
|
|
|
{ |
136
|
|
|
return array_keys($this->entries); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|