1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Models; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\base\Model; |
7
|
|
|
use craft\models\MatrixBlockType; |
|
|
|
|
8
|
|
|
use yii\base\Component as BaseComponent; |
9
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
10
|
|
|
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior; |
11
|
|
|
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior; |
12
|
|
|
use NerdsAndCompany\Schematic\Interfaces\ConverterInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Schematic Base Converter. |
16
|
|
|
* |
17
|
|
|
* Sync Craft Setups. |
18
|
|
|
* |
19
|
|
|
* @author Nerds & Company |
20
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
21
|
|
|
* @license MIT |
22
|
|
|
* |
23
|
|
|
* @see http://www.nerds.company |
24
|
|
|
*/ |
25
|
|
|
abstract class Base extends BaseComponent implements ConverterInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Load fieldlayout and sources behaviors. |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function behaviors() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
FieldLayoutBehavior::className(), |
|
|
|
|
36
|
|
|
SourcesBehavior::className(), |
|
|
|
|
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
abstract public function saveRecord(Model $record, array $definition): bool; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
abstract public function deleteRecord(Model $record): bool; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getRecordDefinition(Model $record): array |
54
|
|
|
{ |
55
|
|
|
$definition = [ |
56
|
|
|
'class' => get_class($record), |
57
|
|
|
'attributes' => $record->getAttributes(), |
58
|
|
|
]; |
59
|
|
|
unset($definition['attributes']['id']); |
60
|
|
|
unset($definition['attributes']['structureId']); |
61
|
|
|
unset($definition['attributes']['dateCreated']); |
62
|
|
|
unset($definition['attributes']['dateUpdated']); |
63
|
|
|
|
64
|
|
|
// Define sources |
65
|
|
|
if (isset($definition['attributes']['sources'])) { |
66
|
|
|
$definition['attributes']['sources'] = $this->getSources($definition['class'], $definition['attributes']['sources'], 'id', 'handle'); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (isset($definition['attributes']['source'])) { |
70
|
|
|
$definition['attributes']['source'] = $this->getSource($definition['class'], $definition['attributes']['sources'], 'id', 'handle'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Define field layout |
74
|
|
|
if (isset($definition['attributes']['fieldLayoutId'])) { |
75
|
|
|
if (!$record instanceof MatrixBlockType) { |
76
|
|
|
$definition['fieldLayout'] = $this->getFieldLayoutDefinition($record->getFieldLayout()); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
unset($definition['attributes']['fieldLayoutId']); |
80
|
|
|
|
81
|
|
|
// Define site settings |
82
|
|
|
if (isset($record->siteSettings)) { |
83
|
|
|
$definition['siteSettings'] = []; |
84
|
|
|
foreach ($record->getSiteSettings() as $siteSetting) { |
85
|
|
|
$definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $definition; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes): void |
96
|
|
|
{ |
97
|
|
|
// Set sources |
98
|
|
|
if (isset($definition['attributes']['sources'])) { |
99
|
|
|
$definition['attributes']['sources'] = $this->getSources($definition['class'], $definition['attributes']['sources'], 'handle', 'id'); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (isset($definition['attributes']['source'])) { |
103
|
|
|
$definition['attributes']['source'] = $this->getSource($definition['class'], $definition['attributes']['sources'], 'handle', 'id'); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$attributes = array_merge($definition['attributes'], $defaultAttributes); |
107
|
|
|
$record->setAttributes($attributes, false); |
108
|
|
|
|
109
|
|
|
// Set field layout |
110
|
|
|
if (array_key_exists('fieldLayout', $definition)) { |
111
|
|
|
$record->setFieldLayout($this->getFieldLayout($definition['fieldLayout'])); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Set site settings |
115
|
|
|
if (array_key_exists('siteSettings', $definition)) { |
116
|
|
|
$siteSettings = []; |
117
|
|
|
foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) { |
118
|
|
|
$siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']); |
119
|
|
|
$site = Craft::$app->sites->getSiteByHandle($handle); |
120
|
|
|
if ($site) { |
121
|
|
|
$siteSetting->siteId = $site->id; |
122
|
|
|
$siteSettings[$site->id] = $siteSetting; |
123
|
|
|
} else { |
124
|
|
|
Schematic::warning(' - Site '.$handle.' could not be found'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$record->setSiteSettings($siteSettings); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: