gggeek /
db-3v4l
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | namespace Db3v4l\Service; |
||
| 4 | |||
| 5 | class DatabaseConfigurationManager |
||
|
0 ignored issues
–
show
|
|||
| 6 | { |
||
| 7 | protected $instanceList = []; |
||
| 8 | |||
| 9 | /** |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 18 | * @param string|string[] $includeFilter accepts 'glob' wildcards. Empty = include all |
||
|
0 ignored issues
–
show
|
|||
| 19 | * @param string|string[] $excludeFilter accepts 'glob' wildcards. Empty = exclude none. NB: exclude match wins over include match |
||
|
0 ignored issues
–
show
|
|||
| 20 | * @return string[] |
||
|
0 ignored issues
–
show
|
|||
| 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) { |
||
|
0 ignored issues
–
show
|
|||
| 33 | |||
| 34 | if (empty($includeFilter)) { |
||
| 35 | $include = true; |
||
| 36 | } else { |
||
| 37 | $include = false; |
||
| 38 | foreach($includeFilter as $filter) { |
||
|
0 ignored issues
–
show
|
|||
| 39 | if (fnmatch($filter, $name)) { |
||
| 40 | $include = true; |
||
| 41 | break; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | if ($include && !empty($excludeFilter)) { |
||
| 47 | foreach($excludeFilter as $filter) { |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 63 | * @param string $instanceName |
||
|
0 ignored issues
–
show
|
|||
| 64 | * @return string[] |
||
|
0 ignored issues
–
show
|
|||
| 65 | * @throws \OutOfBoundsException |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 77 | * @param string[] $instanceNames |
||
|
0 ignored issues
–
show
|
|||
| 78 | * @return string[][] |
||
|
0 ignored issues
–
show
|
|||
| 79 | * @throws \OutOfBoundsException |
||
|
0 ignored issues
–
show
|
|||
| 80 | */ |
||
| 81 | public function getInstancesConfiguration(array $instanceNames) |
||
| 82 | { |
||
| 83 | $instancesDefinitions = []; |
||
| 84 | foreach($instanceNames as $instanceName) |
||
|
0 ignored issues
–
show
|
|||
| 85 | { |
||
| 86 | $instancesDefinitions[$instanceName] = $this->getInstanceConfiguration($instanceName); |
||
| 87 | } |
||
| 88 | return $instancesDefinitions; |
||
| 89 | } |
||
| 90 | } |
||
| 91 |