1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Service; |
4
|
|
|
|
5
|
|
|
class DatabaseConfigurationManager |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
protected $instanceList = []; |
8
|
|
|
|
9
|
|
|
/** |
|
|
|
|
10
|
|
|
* @param string[][] $instanceList key: instance name, values: connection specification |
11
|
|
|
*/ |
12
|
|
|
public function __construct(array $instanceList) |
13
|
|
|
{ |
14
|
|
|
$this->instanceList = $instanceList; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @param string|string[] $includeFilter accepts 'glob' wildcards. Empty = include all |
|
|
|
|
19
|
|
|
* @param string|string[] $excludeFilter accepts 'glob' wildcards. Empty = exclude none. NB: exclude match wins over include match |
|
|
|
|
20
|
|
|
* @return string[] |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
public function listInstances($includeFilter = [], $excludeFilter = []) |
23
|
|
|
{ |
24
|
|
|
if (!is_array($includeFilter)) { |
25
|
|
|
$includeFilter = [$includeFilter]; |
26
|
|
|
} |
27
|
|
|
if (!is_array($excludeFilter)) { |
28
|
|
|
$excludeFilter = [$excludeFilter]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$names = []; |
32
|
|
|
foreach(array_keys($this->instanceList) as $name) { |
|
|
|
|
33
|
|
|
|
34
|
|
|
if (empty($includeFilter)) { |
35
|
|
|
$include = true; |
36
|
|
|
} else { |
37
|
|
|
$include = false; |
38
|
|
|
foreach($includeFilter as $filter) { |
|
|
|
|
39
|
|
|
if (fnmatch($filter, $name)) { |
40
|
|
|
$include = true; |
41
|
|
|
break; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($include && !empty($excludeFilter)) { |
47
|
|
|
foreach($excludeFilter as $filter) { |
|
|
|
|
48
|
|
|
if (fnmatch($filter, $name)) { |
49
|
|
|
$include = false; |
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($include) { |
56
|
|
|
$names[] = $name; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return $names; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
|
|
|
|
63
|
|
|
* @param string $instanceName |
|
|
|
|
64
|
|
|
* @return string[] |
|
|
|
|
65
|
|
|
* @throws \OutOfBoundsException |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
public function getInstanceConfiguration($instanceName) |
68
|
|
|
{ |
69
|
|
|
if (isset($this->instanceList[$instanceName])) { |
70
|
|
|
return $this->instanceList[$instanceName]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
throw new \OutOfBoundsException("Unknown database instance '$instanceName'"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* @param string[] $instanceNames |
|
|
|
|
78
|
|
|
* @return string[][] |
|
|
|
|
79
|
|
|
* @throws \OutOfBoundsException |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function getInstancesConfiguration(array $instanceNames) |
82
|
|
|
{ |
83
|
|
|
$instancesDefinitions = []; |
84
|
|
|
foreach($instanceNames as $instanceName) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$instancesDefinitions[$instanceName] = $this->getInstanceConfiguration($instanceName); |
87
|
|
|
} |
88
|
|
|
return $instancesDefinitions; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|