1 | <?php |
||
23 | class Schematic extends BaseApplication |
||
24 | { |
||
25 | const SCHEMATIC_METHOD_IMPORT = 'import'; |
||
26 | const SCHEMATIC_METHOD_EXPORT = 'export'; |
||
27 | |||
28 | protected static $exportableDataTypes = [ |
||
29 | 'locales', |
||
30 | 'assetSources', |
||
31 | 'assetTransforms', |
||
32 | 'fields', |
||
33 | 'plugins', |
||
34 | 'sections', |
||
35 | 'globalSets', |
||
36 | 'userGroups', |
||
37 | 'users', |
||
38 | 'categoryGroups', |
||
39 | 'tagGroups', |
||
40 | 'elementIndexSettings', |
||
41 | 'pluginData', |
||
42 | ]; |
||
43 | |||
44 | public static function getExportableDataTypes() |
||
45 | { |
||
46 | return self::$exportableDataTypes; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Returns data from import model or default. |
||
51 | * |
||
52 | * @param array $data |
||
53 | * @param string $handle |
||
54 | * @param array $default |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | private function getPluginData(array $data, $handle, array $default = []) |
||
59 | { |
||
60 | return (array_key_exists($handle, $data) && !is_null($data[$handle])) ? $data[$handle] : $default; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Import from Yaml file. |
||
65 | * |
||
66 | * @param string $file |
||
67 | * @param string $override |
||
68 | * @param bool $force if set to true items not included in import will be deleted |
||
69 | * |
||
70 | * @return Result |
||
71 | */ |
||
72 | 1 | public function importFromYaml($file, $override = null, $force = false) |
|
73 | { |
||
74 | 1 | Craft::app()->config->maxPowerCaptain(); |
|
75 | 1 | Craft::app()->setComponent('userSession', $this); |
|
76 | |||
77 | 1 | $yaml = IOHelper::getFileContents($file); |
|
78 | 1 | $yaml_override = IOHelper::getFileContents($override); |
|
79 | 1 | $dataModel = Data::fromYaml($yaml, $yaml_override); |
|
80 | |||
81 | 1 | return $this->importDataModel($dataModel, $force); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Export to Yaml file. |
||
86 | * |
||
87 | * @param string $file |
||
88 | * @param bool $autoCreate |
||
89 | * |
||
90 | * @return Result |
||
91 | */ |
||
92 | 3 | public function exportToYaml($file, $dataTypes = 'all', $autoCreate = true) |
|
93 | { |
||
94 | 3 | Craft::app()->config->maxPowerCaptain(); |
|
95 | 3 | Craft::app()->setComponent('userSession', $this); |
|
96 | |||
97 | 3 | $result = new Result(); |
|
98 | 3 | $dataModel = $this->exportDataModel($dataTypes); |
|
99 | $yaml = Data::toYaml($dataModel); |
||
100 | |||
101 | if (!IOHelper::writeToFile($file, $yaml, $autoCreate)) { // Do not auto create |
||
102 | $result->addError('errors', "Failed to write contents to \"$file\""); |
||
103 | } |
||
104 | |||
105 | return $result; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Import data model. |
||
110 | * |
||
111 | * @param Data $model |
||
112 | * @param bool $force if set to true items not in the import will be deleted |
||
113 | * |
||
114 | * @return Result |
||
115 | */ |
||
116 | 1 | private function importDataModel(Data $model, $force) |
|
117 | { |
||
118 | // Import schema |
||
119 | 1 | $locales = $model->getAttribute('locales', $force); |
|
120 | 1 | $localesImportResult = Craft::app()->schematic_locales->import($locales); |
|
121 | |||
122 | 1 | $plugins = $model->getAttribute('plugins', $force); |
|
123 | 1 | $pluginImportResult = Craft::app()->schematic_plugins->import($plugins); |
|
124 | |||
125 | 1 | $fields = $model->getAttribute('fields'); |
|
126 | 1 | $fieldImportResult = Craft::app()->schematic_fields->import($fields, $force); |
|
127 | |||
128 | 1 | $assetSources = $model->getAttribute('assetSources'); |
|
129 | 1 | $assetSourcesImportResult = Craft::app()->schematic_assetSources->import($assetSources, $force); |
|
130 | |||
131 | 1 | $assetTransforms = $model->getAttribute('assetTransforms'); |
|
132 | 1 | $assetTransformsImportResult = Craft::app()->schematic_assetTransforms->import($assetTransforms, $force); |
|
133 | |||
134 | $globalSets = $model->getAttribute('globalSets'); |
||
135 | $globalSetsImportResult = Craft::app()->schematic_globalSets->import($globalSets, $force); |
||
136 | |||
137 | $sections = $model->getAttribute('sections'); |
||
138 | $sectionImportResult = Craft::app()->schematic_sections->import($sections, $force); |
||
139 | |||
140 | $categoryGroups = $model->getAttribute('categoryGroups'); |
||
141 | $categoryGroupImportResult = Craft::app()->schematic_categoryGroups->import($categoryGroups, $force); |
||
142 | |||
143 | $tagGroups = $model->getAttribute('tagGroups'); |
||
144 | $tagGroupImportResult = Craft::app()->schematic_tagGroups->import($tagGroups, $force); |
||
145 | |||
146 | $userGroups = $model->getAttribute('userGroups'); |
||
147 | $userGroupImportResult = Craft::app()->schematic_userGroups->import($userGroups, $force); |
||
148 | |||
149 | $users = $model->getAttribute('users'); |
||
150 | $userImportResult = Craft::app()->schematic_users->import($users, true); |
||
151 | |||
152 | $fields = $model->getAttribute('fields'); |
||
153 | $fieldImportResultFinal = Craft::app()->schematic_fields->import($fields, $force); |
||
154 | |||
155 | // Element index settings are supported from Craft 2.5 |
||
156 | if (version_compare(CRAFT_VERSION, '2.5', '>=')) { |
||
157 | $elementIndexSettingsImportResult = Craft::app()->schematic_elementIndexSettings->import( |
||
158 | $model->getAttribute('elementIndexSettings'), |
||
159 | $force |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | // Verify results |
||
164 | $result = new Result(); |
||
165 | $result->consume($localesImportResult); |
||
166 | $result->consume($pluginImportResult); |
||
167 | $result->consume($fieldImportResult); |
||
168 | $result->consume($assetSourcesImportResult); |
||
169 | $result->consume($assetTransformsImportResult); |
||
170 | $result->consume($globalSetsImportResult); |
||
171 | $result->consume($sectionImportResult); |
||
172 | $result->consume($categoryGroupImportResult); |
||
173 | $result->consume($tagGroupImportResult); |
||
174 | $result->consume($userGroupImportResult); |
||
175 | $result->consume($userImportResult); |
||
176 | $result->consume($fieldImportResultFinal); |
||
177 | |||
178 | // Element index settings are supported from Craft 2.5 |
||
179 | if (version_compare(CRAFT_VERSION, '2.5', '>=')) { |
||
180 | $result->consume($elementIndexSettingsImportResult); |
||
|
|||
181 | } |
||
182 | |||
183 | $services = Craft::app()->plugins->call('registerMigrationService'); |
||
184 | $this->doImport($result, $model->pluginData, $services, $force); |
||
185 | |||
186 | return $result; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Handles importing. |
||
191 | * |
||
192 | * @param Result $result |
||
193 | * @param array $data |
||
194 | * @param array|Base[] $services |
||
195 | * @param bool $force |
||
196 | */ |
||
197 | private function doImport(Result $result, array $data, $services, $force) |
||
198 | { |
||
199 | foreach ($services as $handle => $service) { |
||
200 | if (is_array($service)) { |
||
201 | $this->doImport($result, $data, $service, $force); |
||
202 | } elseif ($service instanceof Base) { |
||
203 | $pluginData = $this->getPluginData($data, $handle); |
||
204 | $hookResult = $service->import($pluginData, $force); |
||
205 | $result->consume($hookResult); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Export data model. |
||
212 | * |
||
213 | * @param string|array $dataTypes The data types to export |
||
214 | * |
||
215 | * @return array |
||
216 | * |
||
217 | * @throws Exception |
||
218 | */ |
||
219 | 3 | private function exportDataModel($dataTypes = 'all') |
|
220 | { |
||
221 | // If all data types should be exported, get all the available data types that can be exported. |
||
222 | 3 | if (is_array($dataTypes)) { |
|
223 | // Validate that each data type specified to be exported is reconized. |
||
224 | 1 | foreach ($dataTypes as $dataType) { |
|
225 | 1 | if (!in_array($dataType, $this->exportableDataTypes)) { |
|
226 | $errorMessage = 'Invalid export type "'.$dataType.'". Accepted types are ' |
||
227 | .implode(', ', $this->exportableDataTypes); |
||
228 | throw new Exception($errorMessage); |
||
229 | } |
||
230 | 1 | } |
|
231 | 1 | } else { |
|
232 | 2 | $dataTypes = $this->exportableDataTypes; |
|
233 | } |
||
234 | |||
235 | 3 | $categoryGroups = Craft::app()->categories->getAllGroups(); |
|
236 | 3 | $tagGroups = Craft::app()->tags->getAllTagGroups(); |
|
237 | |||
238 | 3 | $export = []; |
|
239 | |||
240 | 3 | if (in_array('locales', $dataTypes)) { |
|
241 | 3 | $export['locales'] = Craft::app()->schematic_locales->export(); |
|
242 | 3 | } |
|
243 | |||
244 | 3 | if (in_array('assetSources', $dataTypes)) { |
|
245 | 3 | $export['assetSources'] = Craft::app()->schematic_assetSources->export(); |
|
246 | 3 | } |
|
247 | |||
248 | 3 | if (in_array('assetTransforms', $dataTypes)) { |
|
249 | 3 | $export['assetTransforms'] = Craft::app()->schematic_assetTransforms->export(); |
|
250 | } |
||
251 | |||
252 | if (in_array('fields', $dataTypes)) { |
||
253 | $fieldGroups = Craft::app()->fields->getAllGroups(); |
||
254 | $export['fields'] = Craft::app()->schematic_fields->export($fieldGroups); |
||
255 | } |
||
256 | |||
257 | if (in_array('plugins', $dataTypes)) { |
||
258 | $export['plugins'] = Craft::app()->schematic_plugins->export(); |
||
259 | } |
||
260 | |||
261 | if (in_array('sections', $dataTypes)) { |
||
262 | $sections = Craft::app()->sections->getAllSections(); |
||
263 | $export['sections'] = Craft::app()->schematic_sections->export($sections); |
||
264 | } |
||
265 | |||
266 | if (in_array('globalSets', $dataTypes)) { |
||
267 | $globals = Craft::app()->globals->getAllSets(); |
||
268 | $export['globalSets'] = Craft::app()->schematic_globalSets->export($globals); |
||
269 | } |
||
270 | |||
271 | if (in_array('userGroups', $dataTypes)) { |
||
272 | $userGroups = Craft::app()->userGroups->getAllGroups(); |
||
273 | $export['userGroups'] = Craft::app()->schematic_userGroups->export($userGroups); |
||
274 | } |
||
275 | |||
276 | if (in_array('users', $dataTypes)) { |
||
277 | $export['users'] = Craft::app()->schematic_users->export(); |
||
278 | } |
||
279 | |||
280 | if (in_array('categoryGroups', $dataTypes)) { |
||
281 | $export['categoryGroups'] = Craft::app()->schematic_categoryGroups->export($categoryGroups); |
||
282 | } |
||
283 | |||
284 | if (in_array('tagGroups', $dataTypes)) { |
||
285 | $export['tagGroups'] = Craft::app()->schematic_tagGroups->export($tagGroups); |
||
286 | } |
||
287 | |||
288 | // Element index settings are supported from Craft 2.5 |
||
289 | if (in_array('elementIndexSettings', $dataTypes) && version_compare(CRAFT_VERSION, '2.5', '>=')) { |
||
290 | $export['elementIndexSettings'] = Craft::app()->schematic_elementIndexSettings->export(); |
||
291 | } |
||
292 | |||
293 | if (in_array('pluginData', $dataTypes)) { |
||
294 | $export['pluginData'] = []; |
||
295 | $services = Craft::app()->plugins->call('registerMigrationService'); |
||
296 | $this->doExport($services, $export['pluginData']); |
||
297 | } |
||
298 | |||
299 | return $export; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Handles exporting. |
||
304 | * |
||
305 | * @param array $services |
||
306 | * @param array $data |
||
307 | */ |
||
308 | private function doExport(array $services, array &$data) |
||
309 | { |
||
310 | foreach ($services as $handle => $service) { |
||
311 | if (is_array($service)) { |
||
312 | $this->doExport($service, $data); |
||
313 | } elseif ($service instanceof Base) { |
||
314 | if ($service instanceof Base) { |
||
315 | $data[$handle] = $service->export(); |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Always return the super user. |
||
323 | * |
||
324 | * @return Craft\UserModel |
||
325 | */ |
||
326 | public function getUser() |
||
330 | |||
331 | /** |
||
332 | * Assume schematic can do anything. |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | public function checkPermission() |
||
337 | { |
||
338 | return true; |
||
340 | } |
||
341 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: