-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathFSGetter.php
More file actions
67 lines (55 loc) · 1.83 KB
/
FSGetter.php
File metadata and controls
67 lines (55 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php namespace DBDiff\Params;
use DBDiff\Exceptions\FSException;
use Symfony\Component\Yaml\Yaml;
class FSGetter implements ParamsGetter {
function __construct($params) {
$this->params = $params;
}
public function getParams() {
$params = new \StdClass;
$configFile = $this->getFile();
if ($configFile) {
try {
$config = Yaml::parse(file_get_contents($configFile));
foreach ($config as $key => $value) {
$this->setIn($params, $key, $value);
}
} catch (Exceptions $e) {
throw new FSException("Error parsing config file");
}
}
return $params;
}
protected function getFile() {
$configFile = false;
if (isset($this->params->config)) {
$configFile = $this->params->config;
if (!file_exists($configFile)) {
throw new FSException("Config file not found");
}
} else {
$candidates = [
getcwd() . '/.dbdiff', // legacy — checked first for backwards compatibility
getcwd() . '/dbdiff.yml', // recommended going forwards
getcwd() . '/.dbdiff.yml',
getcwd() . '/dbdiff.yaml',
];
foreach ($candidates as $candidate) {
if (file_exists($candidate)) {
$configFile = $candidate;
break;
}
}
}
return $configFile;
}
protected function setIn($obj, $key, $value) {
if (strpos($key, '-') !== false) {
$parts = explode('-', $key);
$array = &$obj->$parts[0];
$array[$parts[1]] = $value;
} else {
$obj->$key = $value;
}
}
}