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-2018, Nerds & Company |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* @see http://www.nerds.company |
19
|
|
|
*/ |
20
|
|
|
class Data extends Model |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Parse a yaml file. |
24
|
|
|
* |
25
|
|
|
* @param string $yaml |
26
|
|
|
* @param string $overrideYaml |
|
|
|
|
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
9 |
|
public static function fromYaml($yaml): array |
31
|
|
|
{ |
32
|
9 |
|
return Yaml::parse($yaml); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Replace placeholders with enviroment variables. |
37
|
|
|
* |
38
|
|
|
* Placeholders start with % and end with %. This will be replaced by the |
39
|
|
|
* environment variable with the name SCHEMATIC_{PLACEHOLDER}. If the |
40
|
|
|
* environment variable is not set an exception will be thrown. |
41
|
|
|
* |
42
|
|
|
* @param string $yaml |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
* |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
|
|
public static function replaceEnvVariables($yaml): string |
49
|
|
|
{ |
50
|
|
|
$matches = null; |
51
|
|
|
preg_match_all('/%\w+%/', $yaml, $matches); |
52
|
|
|
$originalValues = $matches[0]; |
53
|
|
|
$replaceValues = []; |
54
|
|
|
foreach ($originalValues as $match) { |
55
|
|
|
$envVariable = substr($match, 1, -1); |
56
|
|
|
$envValue = getenv($envVariable); |
57
|
|
|
if (!$envValue) { |
58
|
|
|
$envVariable = strtoupper($envVariable); |
59
|
|
|
$envVariable = 'SCHEMATIC_'.$envVariable; |
60
|
|
|
$envValue = getenv($envVariable); |
61
|
|
|
if (!$envValue) { |
62
|
|
|
throw new Exception("Schematic environment variable not set: {$envVariable}"); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$replaceValues[] = $envValue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return str_replace($originalValues, $replaceValues, $yaml); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Convert array to yaml. |
73
|
|
|
* |
74
|
|
|
* @param array $data |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
3 |
|
public static function toYaml(array $data): string |
79
|
|
|
{ |
80
|
3 |
|
return Yaml::dump($data, 12, 2); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Read yaml file and parse content. |
85
|
|
|
* |
86
|
|
|
* @param $file |
87
|
|
|
* @return array |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
|
|
public static function parseYamlFile($file): array |
91
|
|
|
{ |
92
|
|
|
if (file_exists($file)) { |
93
|
|
|
$data = file_get_contents($file); |
94
|
|
|
if (!empty($data)) { |
95
|
|
|
$data = static::replaceEnvVariables($data); |
96
|
|
|
return Yaml::parse($data); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return []; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.