1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Controllers; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\helpers\FileHelper; |
7
|
|
|
use NerdsAndCompany\Schematic\Models\Data; |
8
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Schematic Export 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 ExportController extends Base |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Exports the Craft datamodel. |
25
|
|
|
* |
26
|
|
|
* @return int |
27
|
|
|
* @throws \yii\base\ErrorException |
28
|
|
|
*/ |
29
|
|
|
public function actionIndex(): int |
30
|
|
|
{ |
31
|
|
|
$this->disableLogging(); |
32
|
|
|
|
33
|
|
|
$configurations = []; |
34
|
|
|
foreach ($this->getDataTypes() as $dataTypeHandle) { |
35
|
|
|
$dataType = $this->module->getDataType($dataTypeHandle); |
36
|
|
|
if (null == $dataType) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$mapper = $dataType->getMapperHandle(); |
41
|
|
|
if (!$this->module->checkMapper($mapper)) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$records = $dataType->getRecords(); |
46
|
|
|
$configurations[$dataTypeHandle] = $this->module->$mapper->export($records); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Load override file. |
50
|
|
|
$overrideData = []; |
51
|
|
|
if (file_exists($this->overrideFile)) { |
52
|
|
|
// Parse data in the overrideFile if available. |
53
|
|
|
$overrideData = Data::parseYamlFile($this->overrideFile); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Export the configuration to a single file. |
57
|
|
|
if ($this->getStorageType() === self::SINGLE_FILE) { |
58
|
|
|
$configurations = array_replace_recursive($configurations, $overrideData); |
59
|
|
|
FileHelper::writeToFile($this->file, Data::toYaml($configurations)); |
60
|
|
|
Schematic::info('Exported schema to '.$this->file); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Export the configuration to multiple yaml files. |
64
|
|
|
if ($this->getStorageType() === self::MULTIPLE_FILES) { |
65
|
|
|
// Create export directory if it doesn't exist. |
66
|
|
|
if (!file_exists($this->path)) { |
67
|
|
|
mkdir($this->path, 2775, true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
foreach ($configurations as $dataTypeHandle => $configuration) { |
71
|
|
|
Schematic::info('Exporting '.$dataTypeHandle); |
72
|
|
|
foreach ($configuration as $recordName => $records) { |
73
|
|
|
// Check if there is data in the override file for the current record. |
74
|
|
|
if (isset($overrideData[$dataTypeHandle][$recordName])) { |
75
|
|
|
$records = array_replace_recursive($records, $overrideData[$dataTypeHandle][$recordName]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Export records to file. |
79
|
|
|
$fileName = $this->toSafeFileName($dataTypeHandle.'.'.$recordName.'.yml'); |
80
|
|
|
FileHelper::writeToFile($this->path.$fileName, Data::toYaml($records)); |
81
|
|
|
Schematic::info('Exported '.$recordName.' to '.$fileName); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return 0; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|