|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Schematic Data Model. |
|
11
|
|
|
* |
|
12
|
|
|
* Encapsulates data that has been exported via schematic. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Nerds & Company |
|
15
|
|
|
* @copyright Copyright (c) 2015-2019, Nerds & Company |
|
16
|
|
|
* @license MIT |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://www.nerds.company |
|
19
|
|
|
*/ |
|
20
|
|
|
class Data extends Model |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Replace placeholders with enviroment variables. |
|
24
|
|
|
* |
|
25
|
|
|
* Placeholders start with % and end with %. This will be replaced by the |
|
26
|
|
|
* environment variable with the name SCHEMATIC_{PLACEHOLDER}. If the |
|
27
|
|
|
* environment variable is not set an exception will be thrown. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $yaml |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
* |
|
33
|
|
|
* @throws Exception |
|
34
|
|
|
*/ |
|
35
|
9 |
|
public static function replaceEnvVariables($yaml): string |
|
36
|
|
|
{ |
|
37
|
9 |
|
$matches = null; |
|
38
|
9 |
|
preg_match_all('/%\w+%/', $yaml, $matches); |
|
39
|
9 |
|
$originalValues = $matches[0]; |
|
40
|
9 |
|
$replaceValues = []; |
|
41
|
9 |
|
foreach ($originalValues as $match) { |
|
42
|
9 |
|
$envVariable = substr($match, 1, -1); |
|
43
|
9 |
|
$envValue = getenv($envVariable); |
|
44
|
9 |
|
if (!$envValue) { |
|
45
|
9 |
|
$envVariable = strtoupper($envVariable); |
|
46
|
9 |
|
$envVariable = 'SCHEMATIC_'.$envVariable; |
|
47
|
9 |
|
$envValue = getenv($envVariable); |
|
48
|
9 |
|
if (!$envValue) { |
|
49
|
1 |
|
throw new Exception("Schematic environment variable not set: {$envVariable}"); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
8 |
|
$replaceValues[] = $envValue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
return str_replace($originalValues, $replaceValues, $yaml); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Convert array to yaml. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $data |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public static function toYaml(array $data): string |
|
66
|
|
|
{ |
|
67
|
3 |
|
return Yaml::dump($data, 12, 2); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Read yaml file and parse content. |
|
72
|
|
|
* |
|
73
|
|
|
* @param $file |
|
74
|
|
|
* @return array |
|
75
|
|
|
* @throws Exception |
|
76
|
|
|
*/ |
|
77
|
9 |
|
public static function parseYamlFile($file): array |
|
78
|
|
|
{ |
|
79
|
9 |
|
if (file_exists($file)) { |
|
80
|
9 |
|
$data = file_get_contents($file); |
|
81
|
9 |
|
if (!empty($data)) { |
|
82
|
9 |
|
$data = static::replaceEnvVariables($data); |
|
83
|
8 |
|
return Yaml::parse($data); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return []; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|