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
|
|
|
$result = []; |
|
|
|
|
33
|
|
|
foreach ($this->getDataTypes() as $dataTypeHandle) { |
34
|
|
|
$dataType = $this->module->getDataType($dataTypeHandle); |
35
|
|
|
if (null == $dataType) { |
36
|
|
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$mapper = $dataType->getMapperHandle(); |
40
|
|
|
if (!$this->module->checkMapper($mapper)) { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$records = $dataType->getRecords(); |
45
|
|
|
$configurations[$dataTypeHandle] = $this->module->$mapper->export($records); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Parse data in the overrideFile if available. |
49
|
|
|
$overrideData = Data::parseYamlFile($this->overrideFile); |
50
|
|
|
|
51
|
|
|
// Create export directory if it doesn't exist. |
52
|
|
|
if (!file_exists($this->path)) { |
53
|
|
|
mkdir($this->path, 2775, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Export the configuration to multiple yaml files. |
57
|
|
|
foreach ($configurations as $dataTypeHandle => $configuration) { |
|
|
|
|
58
|
|
|
Schematic::info('Exporting '.$dataTypeHandle); |
59
|
|
|
foreach ($configuration as $recordName => $records) { |
60
|
|
|
// Check if there is data in the override file for the current record. |
61
|
|
|
if (isset($overrideData[$dataTypeHandle][$recordName])) { |
62
|
|
|
$records = array_replace_recursive($records, $overrideData[$dataTypeHandle][$recordName]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Export records to file. |
66
|
|
|
$fileName = $this->toSafeFileName($dataTypeHandle . '.' . $recordName . '.yml'); |
67
|
|
|
FileHelper::writeToFile($this->path . $fileName, Data::toYaml($records)); |
68
|
|
|
Schematic::info('Exported ' . $recordName . ' to ' . $fileName); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return 0; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.