|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use craft\helpers\ArrayHelper; |
|
8
|
|
|
use yii\base\Component as BaseComponent; |
|
9
|
|
|
use NerdsAndCompany\Schematic\Interfaces\MappingInterface; |
|
10
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
11
|
|
|
use NerdsAndCompany\Schematic\Converters\Base as BaseConverter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Schematic Base Service. |
|
15
|
|
|
* |
|
16
|
|
|
* Sync Craft Setups. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Nerds & Company |
|
19
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
20
|
|
|
* @license MIT |
|
21
|
|
|
* |
|
22
|
|
|
* @see http://www.nerds.company |
|
23
|
|
|
*/ |
|
24
|
|
|
class ModelProcessor extends BaseComponent implements MappingInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Get all record definitions. |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function export(array $records = []) |
|
32
|
|
|
{ |
|
33
|
|
|
$result = []; |
|
34
|
|
|
foreach ($records as $record) { |
|
35
|
|
|
$modelClass = get_class($record); |
|
36
|
|
|
$converter = $this->getConverter($modelClass); |
|
37
|
|
|
if ($converter == false) { |
|
38
|
|
|
Schematic::error('No converter found for '.$modelClass); |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
$result[$record->handle] = $converter->getRecordDefinition($record); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Import records. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $definitions |
|
51
|
|
|
* @param Model $records The existing records |
|
52
|
|
|
* @param array $defaultAttributes Default attributes to use for each record |
|
53
|
|
|
*/ |
|
54
|
|
|
public function import(array $definitions, array $records = [], array $defaultAttributes = [], $persist = true) |
|
55
|
|
|
{ |
|
56
|
|
|
$imported = []; |
|
57
|
|
|
$recordsByHandle = ArrayHelper::index($records, 'handle'); |
|
58
|
|
|
foreach ($definitions as $handle => $definition) { |
|
59
|
|
|
$modelClass = $definition['class']; |
|
60
|
|
|
$converter = $this->getConverter($modelClass); |
|
61
|
|
|
if ($converter == false) { |
|
62
|
|
|
Schematic::error('No converter found for '.$modelClass); |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$record = new $modelClass(); |
|
67
|
|
|
if (array_key_exists($handle, $recordsByHandle)) { |
|
68
|
|
|
$existing = $recordsByHandle[$handle]; |
|
69
|
|
|
if (get_class($record) == get_class($existing)) { |
|
70
|
|
|
$record = $existing; |
|
71
|
|
|
} else { |
|
72
|
|
|
$record->id = $existing->id; |
|
73
|
|
|
$record->setAttributes($existing->getAttributes()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($converter->getRecordDefinition($record) === $definition) { |
|
77
|
|
|
Schematic::info('- Skipping '.get_class($record).' '.$handle); |
|
78
|
|
|
unset($recordsByHandle[$handle]); |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
Schematic::info('- Saving '.get_class($record).' '.$handle); |
|
84
|
|
|
$converter->setRecordAttributes($record, $definition, $defaultAttributes); |
|
85
|
|
|
if (!$persist || $converter->saveRecord($record, $definition)) { |
|
86
|
|
|
$imported[] = $record; |
|
87
|
|
|
} else { |
|
88
|
|
|
$this->importError($record, $handle); |
|
89
|
|
|
} |
|
90
|
|
|
unset($recordsByHandle[$handle]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (Schematic::$force && $persist) { |
|
94
|
|
|
// Delete records not in definitions |
|
95
|
|
|
foreach ($recordsByHandle as $handle => $record) { |
|
96
|
|
|
$modelClass = get_class($record); |
|
97
|
|
|
Schematic::info('- Deleting '.get_class($record).' '.$handle); |
|
98
|
|
|
$converter = $this->getConverter($modelClass); |
|
99
|
|
|
$converter->deleteRecord($record); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $imported; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Find converter class for model. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $modelClass |
|
110
|
|
|
* |
|
111
|
|
|
* @return BaseConverter |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getConverter(string $modelClass) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($modelClass) { |
|
116
|
|
|
$converterClass = 'NerdsAndCompany\\Schematic\\Converters\\'.ucfirst(str_replace('craft\\', '', $modelClass)); |
|
117
|
|
|
if (class_exists($converterClass)) { |
|
118
|
|
|
return new $converterClass(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->getConverter(get_parent_class($modelClass)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return false; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Log an import error. |
|
129
|
|
|
* |
|
130
|
|
|
* @param Model $record |
|
131
|
|
|
* @param string $handle |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function importError(Model $record, string $handle) |
|
134
|
|
|
{ |
|
135
|
|
|
Schematic::warning('- Error importing '.get_class($record).' '.$handle); |
|
136
|
|
|
foreach ($record->getErrors() as $errors) { |
|
137
|
|
|
foreach ($errors as $error) { |
|
138
|
|
|
Schematic::error(' - '.$error); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.