These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace NerdsAndCompany\Schematic\Models; |
||
4 | |||
5 | use Craft\Craft; |
||
6 | use Craft\BaseModel as Base; |
||
7 | use Craft\AttributeType; |
||
8 | use Craft\Exception; |
||
9 | use Symfony\Component\Yaml\Yaml; |
||
10 | |||
11 | /** |
||
12 | * Schematic Data Model. |
||
13 | * |
||
14 | * Encapsulates data that has been exported via schematic. |
||
15 | * |
||
16 | * @author Nerds & Company |
||
17 | * @copyright Copyright (c) 2015, Nerds & Company |
||
18 | * @license MIT |
||
19 | * |
||
20 | * @link http://www.nerds.company |
||
21 | * |
||
22 | * @property array $Locales |
||
23 | * @property array $assetSources |
||
24 | * @property array $fields |
||
25 | * @property array $globalSets |
||
26 | * @property array $plugins |
||
27 | * @property array $sections |
||
28 | * @property array $userGroups |
||
29 | * @property array $users |
||
30 | * @property array $pluginData |
||
31 | */ |
||
32 | class Data extends Base |
||
33 | { |
||
34 | /** |
||
35 | * Define attributes. |
||
36 | * |
||
37 | * @inheritdoc |
||
38 | */ |
||
39 | protected function defineAttributes() |
||
40 | { |
||
41 | return [ |
||
42 | 'locales' => [AttributeType::Mixed, 'default' => []], |
||
43 | 'assetSources' => [AttributeType::Mixed, 'default' => []], |
||
44 | 'fields' => [AttributeType::Mixed, 'default' => []], |
||
45 | 'globalSets' => [AttributeType::Mixed, 'default' => []], |
||
46 | 'plugins' => [AttributeType::Mixed, 'default' => []], |
||
47 | 'sections' => [AttributeType::Mixed, 'default' => []], |
||
48 | 'userGroups' => [AttributeType::Mixed, 'default' => []], |
||
49 | 'users' => [AttributeType::Mixed, 'default' => []], |
||
50 | 'pluginData' => [AttributeType::Mixed, 'default' => []], |
||
51 | ]; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Populate data model from yaml. |
||
56 | * |
||
57 | * @param string $yaml |
||
58 | * @param string $overrideYaml |
||
59 | * |
||
60 | * @return Data |
||
61 | */ |
||
62 | public static function fromYaml($yaml, $overrideYaml) |
||
63 | { |
||
64 | $data = Yaml::parse($yaml); |
||
65 | if (!empty($overrideYaml)) { |
||
66 | $overrideYaml = static::replaceEnvVariables($overrideYaml); |
||
67 | $overrideData = Yaml::parse($overrideYaml); |
||
68 | if ($overrideData != null) { |
||
69 | $data = array_replace_recursive($data, $overrideData); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | return $data === null ? null : new static($data); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Replace placeholders with enviroment variables. |
||
78 | * |
||
79 | * Placeholders start with % and end with %. This will be replaced by the |
||
80 | * environment variable with the name SCHEMATIC_{PLACEHOLDER}. If the |
||
81 | * environment variable is not set an exception will be thrown. |
||
82 | * |
||
83 | * @param string $yaml |
||
84 | * |
||
85 | * @return string |
||
86 | * |
||
87 | * @throws Exception |
||
88 | */ |
||
89 | public static function replaceEnvVariables($yaml) |
||
90 | { |
||
91 | $matches = null; |
||
92 | preg_match_all('/%\w+%/', $yaml, $matches); |
||
93 | $original_values = $matches[0]; |
||
94 | $replace_values = [); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
95 | foreach ($original_values as $match) { |
||
96 | $env_variable = strtoupper(substr($match, 1, -1)); |
||
97 | $env_variable = 'SCHEMATIC_'.$env_variable; |
||
98 | $env_value = getenv($env_variable); |
||
99 | if (!$env_value) { |
||
100 | throw new Exception(Craft::t("Schematic environment variable not set: {$env_variable}")); |
||
101 | } |
||
102 | $replace_values[] = $env_value; |
||
103 | } |
||
104 | |||
105 | return str_replace($original_values, $replace_values, $yaml); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Populate yaml from data model. |
||
110 | * |
||
111 | * @param array $data |
||
112 | * |
||
113 | * @return Data |
||
114 | */ |
||
115 | public static function toYaml(array $data) |
||
116 | { |
||
117 | $data = $data === null ? null : new static($data); |
||
118 | |||
119 | return Yaml::dump($data->attributes, 12, 2); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $attribute |
||
124 | * @param bool|false $flattenValue |
||
125 | * @param array $default |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getAttribute($attribute, $flattenValue = false, $default = [)) |
||
130 | { |
||
131 | $attribute = parent::getAttribute($attribute, $flattenValue); |
||
132 | |||
133 | return (!is_null($attribute) ? $attribute : $default); |
||
134 | } |
||
135 | } |
||
136 |