|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use craft\base\Plugin; |
|
8
|
|
|
use yii\helpers\Console; |
|
9
|
|
|
use NerdsAndCompany\Schematic\DataTypes\AssetTransformDataType; |
|
10
|
|
|
use NerdsAndCompany\Schematic\DataTypes\CategoryGroupDataType; |
|
11
|
|
|
use NerdsAndCompany\Schematic\DataTypes\ElementIndexDataType; |
|
12
|
|
|
use NerdsAndCompany\Schematic\DataTypes\FieldDataType; |
|
13
|
|
|
use NerdsAndCompany\Schematic\DataTypes\GlobalSetDataType; |
|
14
|
|
|
use NerdsAndCompany\Schematic\DataTypes\PluginDataType; |
|
15
|
|
|
use NerdsAndCompany\Schematic\DataTypes\SectionDataType; |
|
16
|
|
|
use NerdsAndCompany\Schematic\DataTypes\SiteDataType; |
|
17
|
|
|
use NerdsAndCompany\Schematic\DataTypes\TagGroupDataType; |
|
18
|
|
|
use NerdsAndCompany\Schematic\DataTypes\UserGroupDataType; |
|
19
|
|
|
use NerdsAndCompany\Schematic\DataTypes\UserSettingsDataType; |
|
20
|
|
|
use NerdsAndCompany\Schematic\DataTypes\VolumeDataType; |
|
21
|
|
|
use NerdsAndCompany\Schematic\Mappers\ElementIndexMapper; |
|
22
|
|
|
use NerdsAndCompany\Schematic\Mappers\ModelMapper; |
|
23
|
|
|
use NerdsAndCompany\Schematic\Mappers\PluginMapper; |
|
24
|
|
|
use NerdsAndCompany\Schematic\Mappers\UserSettingsMapper; |
|
25
|
|
|
use NerdsAndCompany\Schematic\Interfaces\ConverterInterface; |
|
26
|
|
|
use NerdsAndCompany\Schematic\Interfaces\DataTypeInterface; |
|
27
|
|
|
use NerdsAndCompany\Schematic\Interfaces\MapperInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Schematic. |
|
31
|
|
|
* |
|
32
|
|
|
* Sync Craft Setups. |
|
33
|
|
|
* |
|
34
|
|
|
* @author Nerds & Company |
|
35
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
36
|
|
|
* @license MIT |
|
37
|
|
|
* |
|
38
|
|
|
* @see http://www.nerds.company |
|
39
|
|
|
* |
|
40
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
41
|
|
|
*/ |
|
42
|
|
|
class Schematic extends Plugin |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
public $controllerNamespace = 'NerdsAndCompany\Schematic\Controllers'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
public $dataTypes = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initialize the module. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function init(): void |
|
58
|
|
|
{ |
|
59
|
|
|
Craft::setAlias('@NerdsAndCompany/Schematic', __DIR__); |
|
60
|
|
|
|
|
61
|
|
|
$config = [ |
|
62
|
|
|
'components' => [ |
|
63
|
|
|
'elementIndexMapper' => [ |
|
64
|
|
|
'class' => ElementIndexMapper::class, |
|
65
|
|
|
], |
|
66
|
|
|
'modelMapper' => [ |
|
67
|
|
|
'class' => ModelMapper::class, |
|
68
|
|
|
], |
|
69
|
|
|
'pluginMapper' => [ |
|
70
|
|
|
'class' => PluginMapper::class, |
|
71
|
|
|
], |
|
72
|
|
|
'userSettingsMapper' => [ |
|
73
|
|
|
'class' => UserSettingsMapper::class, |
|
74
|
|
|
], |
|
75
|
|
|
], |
|
76
|
|
|
'dataTypes' => [ |
|
77
|
|
|
'plugins' => PluginDataType::class, |
|
78
|
|
|
'sites' => SiteDataType::class, |
|
79
|
|
|
'volumes' => VolumeDataType::class, |
|
80
|
|
|
'assetTransforms' => AssetTransformDataType::class, |
|
81
|
|
|
'fields' => FieldDataType::class, |
|
82
|
|
|
'sections' => SectionDataType::class, |
|
83
|
|
|
'globalSets' => GlobalSetDataType::class, |
|
84
|
|
|
'categoryGroups' => CategoryGroupDataType::class, |
|
85
|
|
|
'tagGroups' => TagGroupDataType::class, |
|
86
|
|
|
'userGroups' => UserGroupDataType::class, |
|
87
|
|
|
'userSettings' => UserSettingsDataType::class, |
|
88
|
|
|
'elementIndexSettings' => ElementIndexDataType::class, |
|
89
|
|
|
], |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
Craft::configure($this, $config); |
|
93
|
|
|
|
|
94
|
|
|
parent::init(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get datatype by handle. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $dataTypeHandle |
|
101
|
|
|
* |
|
102
|
|
|
* @return DateTypeInterface|null |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getDataType(string $dataTypeHandle) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!isset($this->dataTypes[$dataTypeHandle])) { |
|
107
|
|
|
Schematic::error('DataType '.$dataTypeHandle.' is not registered'); |
|
108
|
|
|
|
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$dataTypeClass = $this->dataTypes[$dataTypeHandle]; |
|
113
|
|
|
if (!class_exists($dataTypeClass)) { |
|
114
|
|
|
Schematic::error('Class '.$dataTypeClass.' does not exist'); |
|
115
|
|
|
|
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$dataType = new $dataTypeClass(); |
|
120
|
|
|
if (!$dataType instanceof DataTypeInterface) { |
|
121
|
|
|
Schematic::error($dataTypeClass.' does not implement DataTypeInterface'); |
|
122
|
|
|
|
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $dataType; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Check mapper handle is valid. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $mapper |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function checkMapper(string $mapper): bool |
|
137
|
|
|
{ |
|
138
|
|
|
if (!isset($this->$mapper)) { |
|
139
|
|
|
Schematic::error('Mapper '.$mapper.' not found'); |
|
140
|
|
|
|
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
if (!$this->$mapper instanceof MapperInterface) { |
|
144
|
|
|
Schematic::error(get_class($this->$mapper).' does not implement MapperInterface'); |
|
145
|
|
|
|
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Find converter for model class. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $modelClass |
|
156
|
|
|
* |
|
157
|
|
|
* @return ConverterInterface|null |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getConverter(string $modelClass, string $originalClass = '') |
|
160
|
|
|
{ |
|
161
|
|
|
if ('' === $originalClass) { |
|
162
|
|
|
$originalClass = $modelClass; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$converterClass = 'NerdsAndCompany\\Schematic\\Converters\\'.ucfirst(str_replace('craft\\', '', $modelClass)); |
|
166
|
|
|
if (class_exists($converterClass)) { |
|
167
|
|
|
$converter = new $converterClass(); |
|
168
|
|
|
if ($converter instanceof ConverterInterface) { |
|
169
|
|
|
return $converter; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$parentClass = get_parent_class($modelClass); |
|
174
|
|
|
if (!$parentClass) { |
|
175
|
|
|
Schematic::error('No converter found for '.$originalClass); |
|
176
|
|
|
|
|
177
|
|
|
return null; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $this->getConverter($parentClass, $originalClass); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Is force enabled? |
|
185
|
|
|
* |
|
186
|
|
|
* @var bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public static $force = false; |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Logs an error message. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
194
|
|
|
* complex data structure, such as array. |
|
195
|
|
|
*/ |
|
196
|
|
|
public static function error($message): void |
|
197
|
|
|
{ |
|
198
|
|
|
Craft::$app->controller->stdout($message.PHP_EOL, Console::FG_RED); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Logs a warning message. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
205
|
|
|
* complex data structure, such as array. |
|
206
|
|
|
*/ |
|
207
|
|
|
public static function warning($message): void |
|
208
|
|
|
{ |
|
209
|
|
|
Craft::$app->controller->stdout($message.PHP_EOL, Console::FG_YELLOW); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Logs an info message. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
216
|
|
|
* complex data structure, such as array. |
|
217
|
|
|
*/ |
|
218
|
|
|
public static function info($message): void |
|
219
|
|
|
{ |
|
220
|
|
|
Craft::$app->controller->stdout($message.PHP_EOL); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Log an import error. |
|
225
|
|
|
* |
|
226
|
|
|
* @param Model $record |
|
227
|
|
|
* @param string $handle |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function importError(Model $record, string $handle): void |
|
230
|
|
|
{ |
|
231
|
|
|
static::warning('- Error importing '.get_class($record).' '.$handle); |
|
232
|
|
|
$errors = $record->getErrors(); |
|
233
|
|
|
if (!is_array($errors)) { |
|
234
|
|
|
static::error(' - An unknown error has occurred'); |
|
235
|
|
|
|
|
236
|
|
|
return; |
|
237
|
|
|
} |
|
238
|
|
|
foreach ($errors as $subErrors) { |
|
239
|
|
|
foreach ($subErrors as $error) { |
|
240
|
|
|
static::error(' - '.$error); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|