|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Schematic. |
|
10
|
|
|
* |
|
11
|
|
|
* Sync Craft Setups. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Nerds & Company |
|
14
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
15
|
|
|
* @license MIT |
|
16
|
|
|
* |
|
17
|
|
|
* @see http://www.nerds.company |
|
18
|
|
|
*/ |
|
19
|
|
|
class Schematic |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The available datatypes. |
|
23
|
|
|
* |
|
24
|
|
|
* @TODO: Make data types and processor components configurable. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
const DATA_TYPES = [ |
|
29
|
|
|
'sites' => Services\ModelProcessor::class, |
|
30
|
|
|
'volumes' => Services\ModelProcessor::class, |
|
31
|
|
|
'assetTransforms' => Services\ModelProcessor::class, |
|
32
|
|
|
'fields' => Services\ModelProcessor::class, |
|
33
|
|
|
'plugins' => Services\Plugins::class, |
|
34
|
|
|
'sections' => Services\ModelProcessor::class, |
|
35
|
|
|
'globalSets' => Services\ModelProcessor::class, |
|
36
|
|
|
'userGroups' => Services\ModelProcessor::class, |
|
37
|
|
|
'users' => Services\Users::class, |
|
38
|
|
|
'categoryGroups' => Services\ModelProcessor::class, |
|
39
|
|
|
'tagGroups' => Services\ModelProcessor::class, |
|
40
|
|
|
'elementIndexSettings' => Services\ElementIndexSettings::class, |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get records for datatype. |
|
45
|
|
|
* |
|
46
|
|
|
* @TODO: Make this more dynamic |
|
47
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $datatype |
|
50
|
|
|
* |
|
51
|
|
|
* @return Model[] |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getRecords(string $datatype) |
|
54
|
|
|
{ |
|
55
|
|
|
$records = []; |
|
56
|
|
|
switch ($datatype) { |
|
57
|
|
|
case 'assetTransforms': |
|
58
|
|
|
$records = Craft::$app->assetTransforms->getAllTransforms(); |
|
59
|
|
|
break; |
|
60
|
|
|
case 'categoryGroups': |
|
61
|
|
|
$records = Craft::$app->categories->getAllGroups(); |
|
62
|
|
|
break; |
|
63
|
|
|
case 'fields': |
|
64
|
|
|
$records = Craft::$app->fields->getAllFields(); |
|
65
|
|
|
break; |
|
66
|
|
|
case 'globalSets': |
|
67
|
|
|
$records = Craft::$app->globals->getAllSets(); |
|
68
|
|
|
break; |
|
69
|
|
|
case 'plugins': |
|
70
|
|
|
$records = Craft::$app->plugins->getAllPluginInfo(); |
|
71
|
|
|
break; |
|
72
|
|
|
case 'sections': |
|
73
|
|
|
$records = Craft::$app->sections->getAllSections(); |
|
74
|
|
|
break; |
|
75
|
|
|
case 'sites': |
|
76
|
|
|
$records = Craft::$app->sites->getAllSites(); |
|
77
|
|
|
break; |
|
78
|
|
|
case 'userGroups': |
|
79
|
|
|
$records = Craft::$app->userGroups->getAllGroups(); |
|
80
|
|
|
break; |
|
81
|
|
|
case 'volumes': |
|
82
|
|
|
$records = Craft::$app->volumes->getAllVolumes(); |
|
83
|
|
|
break; |
|
84
|
|
|
case 'tagGroups': |
|
85
|
|
|
$records = Craft::$app->tags->getAllTagGroups(); |
|
86
|
|
|
break; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $records; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Is force enabled? |
|
94
|
|
|
* |
|
95
|
|
|
* @var bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public static $force = false; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Logs an error message. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
103
|
|
|
* complex data structure, such as array. |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function error($message) |
|
106
|
|
|
{ |
|
107
|
|
|
Craft::error($message, 'schematic'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Logs a warning message. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
114
|
|
|
* complex data structure, such as array. |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function warning($message) |
|
117
|
|
|
{ |
|
118
|
|
|
Craft::warning($message, 'schematic'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Logs an info message. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
125
|
|
|
* complex data structure, such as array. |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function info($message) |
|
128
|
|
|
{ |
|
129
|
|
|
Craft::info($message, 'schematic'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Log an import error. |
|
134
|
|
|
* |
|
135
|
|
|
* @param Model $record |
|
136
|
|
|
* @param string $handle |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function importError(Model $record, string $handle) |
|
139
|
|
|
{ |
|
140
|
|
|
static::warning('- Error importing '.get_class($record).' '.$handle); |
|
141
|
|
|
foreach ($record->getErrors() as $errors) { |
|
142
|
|
|
foreach ($errors as $error) { |
|
143
|
|
|
static::error(' - '.$error); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|