1 | <?php |
||
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 array */ |
||
33 | private $config; |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function options($actionID): array |
||
41 | { |
||
42 | return ['file', 'overrideFile', 'include', 'exclude']; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Get the datatypes to import and/or export. |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | protected function getDataTypes(): array |
||
51 | { |
||
52 | $dataTypes = array_keys($this->module->dataTypes); |
||
53 | |||
54 | // If include is specified. |
||
55 | if (null !== $this->include) { |
||
56 | $dataTypes = $this->applyIncludes($dataTypes); |
||
57 | } |
||
58 | |||
59 | // If there are exclusions. |
||
60 | if (null !== $this->exclude) { |
||
61 | $dataTypes = $this->applyExcludes($dataTypes); |
||
62 | } |
||
63 | |||
64 | //Import fields and usergroups again after all sources have been imported |
||
65 | if (array_search('fields', $dataTypes) && count($dataTypes) > 1) { |
||
66 | $dataTypes[] = 'fields'; |
||
67 | $dataTypes[] = 'userGroups'; |
||
68 | } |
||
69 | |||
70 | return $dataTypes; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Apply given includes. |
||
75 | * |
||
76 | * @param array $dataTypes |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | protected function applyIncludes($dataTypes): array |
||
81 | { |
||
82 | $inclusions = explode(',', $this->include); |
||
83 | // Find any invalid data to include. |
||
84 | $invalidIncludes = array_diff($inclusions, $dataTypes); |
||
85 | if (count($invalidIncludes) > 0) { |
||
86 | $errorMessage = 'WARNING: Invalid include(s)'; |
||
87 | $errorMessage .= ': '.implode(', ', $invalidIncludes).'.'.PHP_EOL; |
||
88 | $errorMessage .= ' Valid inclusions are '.implode(', ', $dataTypes); |
||
89 | |||
90 | // Output an error message outlining what invalid exclusions were specified. |
||
91 | Schematic::warning($errorMessage); |
||
92 | } |
||
93 | // Remove any explicitly included data types from the list of data types to export. |
||
94 | return array_intersect($dataTypes, $inclusions); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Apply given excludes. |
||
99 | * |
||
100 | * @param array $dataTypes |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | protected function applyExcludes(array $dataTypes): array |
||
105 | { |
||
106 | $exclusions = explode(',', $this->exclude); |
||
107 | // Find any invalid data to exclude. |
||
108 | $invalidExcludes = array_diff($exclusions, $dataTypes); |
||
109 | if (count($invalidExcludes) > 0) { |
||
110 | $errorMessage = 'WARNING: Invalid exlude(s)'; |
||
111 | $errorMessage .= ': '.implode(', ', $invalidExcludes).'.'.PHP_EOL; |
||
112 | $errorMessage .= ' Valid exclusions are '.implode(', ', $dataTypes); |
||
113 | |||
114 | // Output an error message outlining what invalid exclusions were specified. |
||
115 | Schematic::warning($errorMessage); |
||
116 | } |
||
117 | // Remove any explicitly excluded data types from the list of data types to export. |
||
118 | return array_diff($dataTypes, $exclusions); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Disable normal logging (to stdout) while running console commands. |
||
123 | * |
||
124 | * @TODO: Find a less hacky way to solve this |
||
125 | */ |
||
126 | protected function disableLogging() |
||
127 | { |
||
128 | if (Craft::$app->log) { |
||
129 | Craft::$app->log->targets = []; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Convert a filename to one safe to use. |
||
135 | * |
||
136 | * @param $fileName |
||
137 | * @return mixed |
||
138 | */ |
||
139 | protected function toSafeFileName($fileName) |
||
140 | { |
||
141 | // Remove all slashes and backslashes in the recordName to avoid file sturcture problems. |
||
142 | $fileName = str_replace('\\', ':', $fileName); |
||
143 | $fileName = str_replace('/', '::', $fileName); |
||
144 | |||
145 | return $fileName; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Convert a safe filename back to it's original form. |
||
150 | * |
||
151 | * @param $fileName |
||
152 | * @return mixed |
||
153 | */ |
||
154 | protected function fromSafeFileName($fileName) |
||
155 | { |
||
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 |
||
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) |
||
186 | |||
187 | /** |
||
188 | * @return array |
||
189 | * @throws \Exception |
||
190 | */ |
||
191 | public function getConfig(): array |
||
198 | |||
199 | /** |
||
200 | * @param array $config |
||
201 | */ |
||
202 | public function setConfig(array $config): void |
||
206 | |||
207 | /** |
||
208 | * @throws \Exception |
||
209 | */ |
||
210 | protected function readConfigFile() |
||
218 | } |
||
219 |