forked from civicrm/cv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildkitReader.php
55 lines (50 loc) · 1.42 KB
/
BuildkitReader.php
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
<?php
namespace Civi\Cv;
use Civi\Cv\Util\Filesystem;
class BuildkitReader {
/**
* Given a civicrm.settings.php file, find the matching buildkit .sh config file.
*
* @param string $settingsFile
* @return null|string
*/
public static function findShFile($settingsFile) {
$fs = new Filesystem();
$settingsFile = $fs->toAbsolutePath($settingsFile);
$parts = explode('/', str_replace('\\', '/', $settingsFile));
while (!empty($parts)) {
$last = array_pop($parts);
$basePath = implode('/', $parts);
$shFile = "$basePath/$last.sh";
if (is_dir("$basePath/$last") && file_exists($shFile)) {
// Does it look vaguely like a buildkit config file?
if (preg_match('/ADMIN_USER=/', file_get_contents($shFile))) {
return $shFile;
}
}
}
return NULL;
}
/**
* Parse the key-value paris in buildkit .sh config file.
*
* @param string $shFile
* @return array
*/
public static function readShFile($shFile) {
$lines = explode("\n", file_get_contents($shFile));
$result = array();
foreach ($lines as $line) {
if (empty($line) || $line[0] == '#') {
continue;
}
if (preg_match('/^([A-Z0-9_]+)=\"(.*)\"$/', $line, $matches)) {
$result[$matches[1]] = stripcslashes($matches[2]);
}
else {
throw new \RuntimeException("Malformed line [$line]");
}
}
return $result;
}
}