1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\BaseApplicationComponent as BaseApplication; |
7
|
|
|
use Craft\IOHelper; |
8
|
|
|
use NerdsAndCompany\Schematic\Models\Data; |
9
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Schematic Service. |
13
|
|
|
* |
14
|
|
|
* Sync Craft Setups. |
15
|
|
|
* |
16
|
|
|
* @author Nerds & Company |
17
|
|
|
* @copyright Copyright (c) 2015-2016, Nerds & Company |
18
|
|
|
* @license MIT |
19
|
|
|
* |
20
|
|
|
* @link http://www.nerds.company |
21
|
|
|
*/ |
22
|
|
|
class Schematic extends BaseApplication |
23
|
|
|
{ |
24
|
|
|
const SCHEMATIC_METHOD_IMPORT = 'import'; |
25
|
|
|
const SCHEMATIC_METHOD_EXPORT = 'export'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns data from import model or default. |
29
|
|
|
* |
30
|
|
|
* @param array $data |
31
|
|
|
* @param string $handle |
32
|
|
|
* @param array $default |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
1 |
|
private function getPluginData(array $data, $handle, array $default = []) |
37
|
|
|
{ |
38
|
1 |
|
return (array_key_exists($handle, $data) && !is_null($data[$handle])) ? $data[$handle] : $default; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Import from Yaml file. |
43
|
|
|
* |
44
|
|
|
* @param string $file |
45
|
|
|
* @param string $override |
46
|
|
|
* @param bool $force if set to true items not included in import will be deleted |
47
|
|
|
* |
48
|
|
|
* @return Result |
49
|
|
|
*/ |
50
|
1 |
|
public function importFromYaml($file, $override = null, $force = false) |
51
|
|
|
{ |
52
|
1 |
|
Craft::app()->config->maxPowerCaptain(); |
53
|
1 |
|
Craft::app()->setComponent('userSession', $this); |
54
|
|
|
|
55
|
1 |
|
$yaml = IOHelper::getFileContents($file); |
56
|
1 |
|
$yaml_override = IOHelper::getFileContents($override); |
57
|
1 |
|
$dataModel = Data::fromYaml($yaml, $yaml_override); |
58
|
|
|
|
59
|
1 |
|
return $this->importDataModel($dataModel, $force); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Export to Yaml file. |
64
|
|
|
* |
65
|
|
|
* @param string $file |
66
|
|
|
* @param bool $autoCreate |
67
|
|
|
* |
68
|
|
|
* @return Result |
69
|
|
|
*/ |
70
|
2 |
|
public function exportToYaml($file, $autoCreate = true) |
71
|
|
|
{ |
72
|
2 |
|
Craft::app()->config->maxPowerCaptain(); |
73
|
2 |
|
Craft::app()->setComponent('userSession', $this); |
74
|
|
|
|
75
|
2 |
|
$result = new Result(); |
76
|
2 |
|
$dataModel = $this->exportDataModel(); |
77
|
2 |
|
$yaml = Data::toYaml($dataModel); |
78
|
|
|
|
79
|
2 |
|
if (!IOHelper::writeToFile($file, $yaml, $autoCreate)) { // Do not auto create |
80
|
1 |
|
$result->addError('errors', "Failed to write contents to \"$file\""); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
2 |
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Import data model. |
88
|
|
|
* |
89
|
|
|
* @param Data $model |
90
|
|
|
* @param bool $force if set to true items not in the import will be deleted |
91
|
|
|
* |
92
|
|
|
* @return Result |
93
|
|
|
*/ |
94
|
3 |
|
private function importDataModel(Data $model, $force) |
95
|
|
|
{ |
96
|
|
|
// Import schema |
97
|
1 |
|
$localesImportResult = Craft::app()->schematic_locales->import($model->getAttribute('locales', $force)); |
98
|
1 |
|
$pluginImportResult = Craft::app()->schematic_plugins->import($model->getAttribute('plugins', $force)); |
99
|
1 |
|
$assetSourcesImportResult = Craft::app()->schematic_assetSources->import($model->getAttribute('assetSources'), $force); |
100
|
1 |
|
$fieldImportResult = Craft::app()->schematic_fields->import($model->getAttribute('fields'), $force); |
101
|
1 |
|
$globalSetsImportResult = Craft::app()->schematic_globalSets->import($model->getAttribute('globalSets'), $force); |
102
|
1 |
|
$sectionImportResult = Craft::app()->schematic_sections->import($model->getAttribute('sections'), $force); |
103
|
1 |
|
$userGroupImportResult = Craft::app()->schematic_userGroups->import($model->getAttribute('userGroups'), $force); |
104
|
1 |
|
$userImportResult = Craft::app()->schematic_users->import($model->getAttribute('users'), true); |
105
|
1 |
|
$fieldImportResultFinal = Craft::app()->schematic_fields->import($model->getAttribute('fields'), $force); |
106
|
1 |
|
$categoryGroupImportResult = Craft::app()->schematic_categoryGroups->import($model->getAttribute('categoryGroups'), $force); |
107
|
|
|
|
108
|
|
|
// Element index settings are supported from Craft 2.5 |
109
|
1 |
|
if (version_compare(CRAFT_VERSION, '2.5', '>=')) { |
110
|
1 |
|
$elementIndexSettingsImportResult = Craft::app()->schematic_elementIndexSettings->import($model->getAttribute('elementIndexSettings'), $force); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
// Verify results |
114
|
1 |
|
$result = new Result(); |
115
|
1 |
|
$result->consume($localesImportResult); |
116
|
1 |
|
$result->consume($pluginImportResult); |
117
|
1 |
|
$result->consume($assetSourcesImportResult); |
118
|
1 |
|
$result->consume($fieldImportResult); |
119
|
1 |
|
$result->consume($globalSetsImportResult); |
120
|
1 |
|
$result->consume($sectionImportResult); |
121
|
1 |
|
$result->consume($userGroupImportResult); |
122
|
1 |
|
$result->consume($userImportResult); |
123
|
3 |
|
$result->consume($fieldImportResultFinal); |
124
|
1 |
|
$result->consume($categoryGroupImportResult); |
125
|
|
|
|
126
|
|
|
// Element index settings are supported from Craft 2.5 |
127
|
1 |
|
if (version_compare(CRAFT_VERSION, '2.5', '>=')) { |
128
|
1 |
|
$result->consume($elementIndexSettingsImportResult); |
|
|
|
|
129
|
1 |
|
} |
130
|
|
|
|
131
|
1 |
|
$services = Craft::app()->plugins->call('registerMigrationService'); |
132
|
1 |
|
$this->doImport($result, $model->pluginData, $services, $force); |
133
|
|
|
|
134
|
1 |
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Handles importing. |
139
|
|
|
* |
140
|
|
|
* @param Result $result |
141
|
|
|
* @param array $data |
142
|
|
|
* @param array|Base[] $services |
143
|
|
|
* @param bool $force |
144
|
|
|
*/ |
145
|
1 |
|
private function doImport(Result $result, array $data, $services, $force) |
146
|
|
|
{ |
147
|
1 |
|
foreach ($services as $handle => $service) { |
148
|
1 |
|
if (is_array($service)) { |
149
|
1 |
|
$this->doImport($result, $data, $service, $force); |
150
|
1 |
|
} elseif ($service instanceof Base) { |
151
|
1 |
|
$pluginData = $this->getPluginData($data, $handle); |
152
|
1 |
|
$hookResult = $service->import($pluginData, $force); |
153
|
1 |
|
$result->consume($hookResult); |
154
|
1 |
|
} |
155
|
1 |
|
} |
156
|
1 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Export data model. |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
2 |
|
private function exportDataModel() |
164
|
|
|
{ |
165
|
2 |
|
$fieldGroups = Craft::app()->fields->getAllGroups(); |
166
|
2 |
|
$sections = Craft::app()->sections->getAllSections(); |
167
|
2 |
|
$globals = Craft::app()->globals->getAllSets(); |
168
|
2 |
|
$userGroups = Craft::app()->userGroups->getAllGroups(); |
169
|
2 |
|
$categoryGroups = Craft::app()->categories->getAllGroups(); |
170
|
|
|
|
171
|
|
|
$export = [ |
172
|
2 |
|
'locales' => Craft::app()->schematic_locales->export(), |
173
|
2 |
|
'assetSources' => Craft::app()->schematic_assetSources->export(), |
174
|
2 |
|
'fields' => Craft::app()->schematic_fields->export($fieldGroups), |
175
|
2 |
|
'plugins' => Craft::app()->schematic_plugins->export(), |
176
|
2 |
|
'sections' => Craft::app()->schematic_sections->export($sections), |
177
|
2 |
|
'globalSets' => Craft::app()->schematic_globalSets->export($globals), |
178
|
2 |
|
'userGroups' => Craft::app()->schematic_userGroups->export($userGroups), |
179
|
2 |
|
'users' => Craft::app()->schematic_users->export(), |
180
|
2 |
|
'categoryGroups' => Craft::app()->schematic_categoryGroups->export($categoryGroups), |
181
|
2 |
|
]; |
182
|
|
|
|
183
|
|
|
// Element index settings are supported from Craft 2.5 |
184
|
2 |
|
if (version_compare(CRAFT_VERSION, '2.5', '>=')) { |
185
|
2 |
|
$export['elementIndexSettings'] = Craft::app()->schematic_elementIndexSettings->export(); |
186
|
2 |
|
} |
187
|
|
|
|
188
|
2 |
|
$export['pluginData'] = []; |
189
|
2 |
|
$services = Craft::app()->plugins->call('registerMigrationService'); |
190
|
2 |
|
$this->doExport($services, $export['pluginData']); |
191
|
|
|
|
192
|
2 |
|
return $export; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Handles exporting. |
197
|
|
|
* |
198
|
|
|
* @param array $services |
199
|
|
|
* @param array $data |
200
|
|
|
*/ |
201
|
2 |
|
private function doExport(array $services, array &$data) |
202
|
|
|
{ |
203
|
2 |
|
foreach ($services as $handle => $service) { |
204
|
2 |
|
if (is_array($service)) { |
205
|
2 |
|
$this->doExport($service, $data); |
206
|
2 |
|
} elseif ($service instanceof Base) { |
207
|
2 |
|
if ($service instanceof Base) { |
208
|
2 |
|
$data[$handle] = $service->export(); |
209
|
2 |
|
} |
210
|
2 |
|
} |
211
|
2 |
|
} |
212
|
2 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Assume schematic can do anything. |
216
|
|
|
* |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public function checkPermission() |
220
|
|
|
{ |
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
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: