|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use NerdsAndCompany\Schematic\Models\Data; |
|
7
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
8
|
|
|
use craft\errors\WrongEditionException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Schematic Import Command. |
|
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 ImportController extends Base |
|
22
|
|
|
{ |
|
23
|
|
|
public $force = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function options($actionID): array |
|
31
|
|
|
{ |
|
32
|
|
|
return array_merge(parent::options($actionID), ['force']); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Imports the Craft datamodel. |
|
37
|
|
|
* |
|
38
|
|
|
* @return int |
|
39
|
|
|
*/ |
|
40
|
|
|
public function actionIndex(): int |
|
41
|
|
|
{ |
|
42
|
|
|
if (!file_exists($this->path)) { |
|
43
|
|
|
Schematic::error('Directory not found: ' . $this->path); |
|
44
|
|
|
|
|
45
|
|
|
return 1; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$dataTypes = $this->getDataTypes(); |
|
49
|
|
|
$this->importFromYaml($dataTypes); |
|
50
|
|
|
Schematic::info('Loaded schema from '.$this->path); |
|
51
|
|
|
|
|
52
|
|
|
return 0; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Import from Yaml file. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $dataTypes The data types to import |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
private function importFromYaml(array $dataTypes) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->disableLogging(); |
|
64
|
|
|
|
|
65
|
|
|
<<<<<<< Updated upstream |
|
|
|
|
|
|
66
|
|
|
$yamlOverride = null; |
|
67
|
|
|
if (file_exists($this->overrideFile)) { |
|
68
|
|
|
$yamlOverride = file_get_contents($this->overrideFile); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Grab all yaml files in the schema directory. |
|
72
|
|
|
$schemaFiles = preg_grep('~\.(yml)$~', scandir($this->path)); |
|
73
|
|
|
|
|
74
|
|
|
// Read contents of each file and add it to the definitions. |
|
75
|
|
|
foreach ($schemaFiles as $fileName) { |
|
76
|
|
|
$schemaStructure = explode('.', $this->fromSafeFileName($fileName)); |
|
77
|
|
|
$dataTypeHandle = $schemaStructure[0]; |
|
78
|
|
|
$recordName = $schemaStructure[1]; |
|
79
|
|
|
|
|
80
|
|
|
$contents = file_get_contents($this->path . $fileName); |
|
81
|
|
|
|
|
82
|
|
|
$definitions[$dataTypeHandle][$recordName] = Data::fromYaml($contents, $yamlOverride); |
|
83
|
|
|
} |
|
84
|
|
|
======= |
|
85
|
|
|
// Parse data in the overrideFile if available. |
|
86
|
|
|
$overrideData = Data::parseYamlFile($this->overrideFile); |
|
87
|
|
|
|
|
88
|
|
|
// Grab all yaml files in the schema directory. |
|
89
|
|
|
$schemaFiles = preg_grep('~\.(yml)$~', scandir($this->path)); |
|
90
|
|
|
|
|
91
|
|
|
// Read contents of each file and add it to the definitions. |
|
92
|
|
|
foreach ($schemaFiles as $fileName) { |
|
93
|
|
|
$schemaStructure = explode('.', $this->fromSafeFileName($fileName)); |
|
94
|
|
|
$dataTypeHandle = $schemaStructure[0]; |
|
95
|
|
|
$recordName = $schemaStructure[1]; |
|
96
|
|
|
|
|
97
|
|
|
$definition = Data::fromYaml(file_get_contents($this->path . $fileName)); |
|
98
|
|
|
|
|
99
|
|
|
// Check if there is data in the override file for the current record. |
|
100
|
|
|
if (isset($overrideData[$dataTypeHandle][$recordName])) { |
|
101
|
|
|
$definition = array_replace_recursive( |
|
102
|
|
|
$definition, |
|
103
|
|
|
$overrideData[$dataTypeHandle][$recordName] |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$definitions[$dataTypeHandle][$recordName] = $definition; |
|
108
|
|
|
} |
|
109
|
|
|
>>>>>>> Stashed changes |
|
110
|
|
|
|
|
111
|
|
|
foreach ($dataTypes as $dataTypeHandle) { |
|
112
|
|
|
$dataType = $this->module->getDataType($dataTypeHandle); |
|
113
|
|
|
if (null == $dataType) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$mapper = $dataType->getMapperHandle(); |
|
118
|
|
|
if (!$this->module->checkMapper($mapper)) { |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
Schematic::info('Importing '.$dataTypeHandle); |
|
123
|
|
|
Schematic::$force = $this->force; |
|
124
|
|
|
if (array_key_exists($dataTypeHandle, $definitions) && is_array($definitions[$dataTypeHandle])) { |
|
125
|
|
|
$records = $dataType->getRecords(); |
|
126
|
|
|
try { |
|
127
|
|
|
$this->module->$mapper->import($definitions[$dataTypeHandle], $records); |
|
128
|
|
|
$dataType->afterImport(); |
|
129
|
|
|
} catch (WrongEditionException $e) { |
|
130
|
|
|
Schematic::error('Craft Pro is required for datatype '.$dataTypeHandle); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|