1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Controllers; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use NerdsAndCompany\Schematic\Models\Data; |
7
|
|
|
use yii\console\Controller; |
8
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Schematic Base Controller. |
12
|
|
|
* |
13
|
|
|
* Sync Craft Setups. |
14
|
|
|
* |
15
|
|
|
* @author Nerds & Company |
16
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* @see http://www.nerds.company |
20
|
|
|
*/ |
21
|
|
|
class Base extends Controller |
22
|
|
|
{ |
23
|
|
|
const SINGLE_FILE = 'single'; |
24
|
|
|
const MULTIPLE_FILES = 'multiple'; |
25
|
|
|
|
26
|
|
|
public $file = 'config/schema.yml'; |
27
|
|
|
public $path = 'config/schema/'; |
28
|
|
|
public $overrideFile = 'config/override.yml'; |
29
|
|
|
public $configFile = 'config/schematic.yml'; |
30
|
|
|
public $exclude; |
31
|
|
|
public $include; |
32
|
|
|
/** @var string */ |
33
|
|
|
private $storageType; |
34
|
|
|
/** @var array */ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function options($actionID): array |
43
|
|
|
{ |
44
|
|
|
return ['file', 'overrideFile', 'include', 'exclude']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the datatypes to import and/or export. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
protected function getDataTypes(): array |
53
|
|
|
{ |
54
|
|
|
$dataTypes = array_keys($this->module->dataTypes); |
55
|
|
|
|
56
|
|
|
// If include is specified. |
57
|
|
|
if (null !== $this->include) { |
58
|
|
|
$dataTypes = $this->applyIncludes($dataTypes); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// If there are exclusions. |
62
|
|
|
if (null !== $this->exclude) { |
63
|
|
|
$dataTypes = $this->applyExcludes($dataTypes); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
//Import fields and usergroups again after all sources have been imported |
67
|
|
|
if (array_search('fields', $dataTypes) && count($dataTypes) > 1) { |
68
|
|
|
$dataTypes[] = 'fields'; |
69
|
|
|
$dataTypes[] = 'userGroups'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $dataTypes; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Apply given includes. |
77
|
|
|
* |
78
|
|
|
* @param array $dataTypes |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function applyIncludes($dataTypes): array |
83
|
|
|
{ |
84
|
|
|
$inclusions = explode(',', $this->include); |
85
|
|
|
// Find any invalid data to include. |
86
|
|
|
$invalidIncludes = array_diff($inclusions, $dataTypes); |
87
|
|
|
if (count($invalidIncludes) > 0) { |
88
|
|
|
$errorMessage = 'WARNING: Invalid include(s)'; |
89
|
|
|
$errorMessage .= ': '.implode(', ', $invalidIncludes).'.'.PHP_EOL; |
90
|
|
|
$errorMessage .= ' Valid inclusions are '.implode(', ', $dataTypes); |
91
|
|
|
|
92
|
|
|
// Output an error message outlining what invalid exclusions were specified. |
93
|
|
|
Schematic::warning($errorMessage); |
94
|
|
|
} |
95
|
|
|
// Remove any explicitly included data types from the list of data types to export. |
96
|
|
|
return array_intersect($dataTypes, $inclusions); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Apply given excludes. |
101
|
|
|
* |
102
|
|
|
* @param array $dataTypes |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function applyExcludes(array $dataTypes): array |
107
|
|
|
{ |
108
|
|
|
$exclusions = explode(',', $this->exclude); |
109
|
|
|
// Find any invalid data to exclude. |
110
|
|
|
$invalidExcludes = array_diff($exclusions, $dataTypes); |
111
|
|
|
if (count($invalidExcludes) > 0) { |
112
|
|
|
$errorMessage = 'WARNING: Invalid exlude(s)'; |
113
|
|
|
$errorMessage .= ': '.implode(', ', $invalidExcludes).'.'.PHP_EOL; |
114
|
|
|
$errorMessage .= ' Valid exclusions are '.implode(', ', $dataTypes); |
115
|
|
|
|
116
|
|
|
// Output an error message outlining what invalid exclusions were specified. |
117
|
|
|
Schematic::warning($errorMessage); |
118
|
|
|
} |
119
|
|
|
// Remove any explicitly excluded data types from the list of data types to export. |
120
|
|
|
return array_diff($dataTypes, $exclusions); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Disable normal logging (to stdout) while running console commands. |
125
|
|
|
* |
126
|
|
|
* @TODO: Find a less hacky way to solve this |
127
|
|
|
*/ |
128
|
|
|
protected function disableLogging() |
129
|
|
|
{ |
130
|
|
|
if (Craft::$app->log) { |
131
|
|
|
Craft::$app->log->targets = []; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Convert a filename to one safe to use. |
137
|
|
|
* |
138
|
|
|
* @param $fileName |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
protected function toSafeFileName($fileName) { |
142
|
|
|
// Remove all slashes and backslashes in the recordName to avoid file sturcture problems. |
143
|
|
|
$fileName = str_replace('\\', ':', $fileName); |
144
|
|
|
$fileName = str_replace('/', '::', $fileName); |
145
|
|
|
|
146
|
|
|
return $fileName; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Convert a safe filename back to it's original form. |
151
|
|
|
* |
152
|
|
|
* @param $fileName |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
protected function fromSafeFileName($fileName) { |
156
|
|
|
// Replace the placeholders back to slashes and backslashes. |
157
|
|
|
$fileName = str_replace(':', '\\', $fileName); |
158
|
|
|
$fileName = str_replace('::', '/', $fileName); |
159
|
|
|
|
160
|
|
|
return $fileName; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the storage type. |
165
|
|
|
* |
166
|
|
|
* @throws \Exception |
167
|
|
|
*/ |
168
|
|
|
protected function getStorageType() : string |
169
|
|
|
{ |
170
|
|
|
return $this->getConfigSetting('storageType') ?? self::SINGLE_FILE; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get a setting from the config. |
175
|
|
|
* |
176
|
|
|
* @param $name |
177
|
|
|
* |
178
|
|
|
* @return mixed|null |
179
|
|
|
* @throws \Exception |
180
|
|
|
*/ |
181
|
|
|
public function getConfigSetting($name) |
182
|
|
|
{ |
183
|
|
|
$config = $this->getConfig(); |
184
|
|
|
return $config[$name] ?? null; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return array |
189
|
|
|
* @throws \Exception |
190
|
|
|
*/ |
191
|
|
|
public function getConfig(): array |
192
|
|
|
{ |
193
|
|
|
if (empty($this->config)) { |
194
|
|
|
$this->readConfigFile(); |
195
|
|
|
} |
196
|
|
|
return $this->config ?? []; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $config |
201
|
|
|
*/ |
202
|
|
|
public function setConfig(array $config): void |
203
|
|
|
{ |
204
|
|
|
$this->config = $config; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @throws \Exception |
209
|
|
|
*/ |
210
|
|
|
protected function readConfigFile() |
211
|
|
|
{ |
212
|
|
|
// Load config file. |
213
|
|
|
if (file_exists($this->configFile)) { |
214
|
|
|
// Parse data in the overrideFile if available. |
215
|
|
|
$this->config = Data::parseYamlFile($this->configFile); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|