1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Behaviors; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use TypeError; |
7
|
|
|
use yii\base\Behavior; |
8
|
|
|
use craft\base\Model; |
9
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Schematic Sources Behavior. |
13
|
|
|
* |
14
|
|
|
* Sync Craft Setups. |
15
|
|
|
* |
16
|
|
|
* @author Nerds & Company |
17
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
18
|
|
|
* @license MIT |
19
|
|
|
* |
20
|
|
|
* @see http://www.nerds.company |
21
|
|
|
*/ |
22
|
|
|
class SourcesBehavior extends Behavior |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Recursively find sources in definition attributes. |
26
|
|
|
* |
27
|
|
|
* @param string $fieldType |
28
|
|
|
* @param array $attributes |
29
|
|
|
* @param string $indexFrom |
30
|
|
|
* @param string $indexTo |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
23 |
|
public function findSources(string $fieldType, array $attributes, string $indexFrom, string $indexTo): array |
35
|
|
|
{ |
36
|
23 |
|
foreach ($attributes as $key => $attribute) { |
37
|
23 |
|
if ($key === 'source' || $key === 'sources') { |
38
|
2 |
|
$method = 'get'.ucfirst($key); |
39
|
2 |
|
$attributes[$key] = $this->$method($fieldType, $attribute, $indexFrom, $indexTo); |
40
|
23 |
|
} elseif (is_array($attribute)) { |
41
|
23 |
|
$attributes[$key] = $this->findSources($fieldType, $attribute, $indexFrom, $indexTo); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
23 |
|
return $attributes; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
50
|
|
|
* |
51
|
|
|
* @param string $fieldType |
52
|
|
|
* @param string|array $sources |
53
|
|
|
* @param string $indexFrom |
54
|
|
|
* @param string $indexTo |
55
|
|
|
* |
56
|
|
|
* @return array|string |
57
|
|
|
*/ |
58
|
9 |
|
public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
59
|
|
|
{ |
60
|
9 |
|
$mappedSources = $sources; |
61
|
9 |
|
if (is_array($sources)) { |
62
|
7 |
|
$mappedSources = []; |
63
|
7 |
|
$sources = array_filter($sources); |
64
|
7 |
|
foreach ($sources as $source) { |
65
|
7 |
|
$mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
return $mappedSources; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
74
|
|
|
* |
75
|
|
|
* @TODO Break up and simplify this method |
76
|
|
|
* |
77
|
|
|
* @param string $fieldType |
78
|
|
|
* @param string $source |
79
|
|
|
* @param string $indexFrom |
80
|
|
|
* @param string $indexTo |
81
|
|
|
* |
82
|
|
|
* @return string|null |
83
|
|
|
* |
84
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
85
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
86
|
|
|
*/ |
87
|
9 |
|
public function getSource(string $fieldType, $source, string $indexFrom, string $indexTo) |
88
|
|
|
{ |
89
|
9 |
|
if (false === strpos($source, ':')) { |
90
|
6 |
|
return $source; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @var Model $sourceObject */ |
94
|
9 |
|
$sourceObject = null; |
95
|
|
|
|
96
|
9 |
|
list($sourceType, $sourceFrom) = explode(':', $source); |
97
|
|
|
switch ($sourceType) { |
98
|
9 |
|
case 'single': |
99
|
9 |
|
case 'section': |
100
|
9 |
|
case 'createEntries': |
101
|
9 |
|
case 'editPeerEntries': |
102
|
9 |
|
case 'deleteEntries': |
103
|
9 |
|
case 'deletePeerEntries': |
104
|
9 |
|
case 'deletePeerEntryDrafts': |
105
|
9 |
|
case 'editEntries': |
106
|
9 |
|
case 'editPeerEntryDrafts': |
107
|
9 |
|
case 'publishEntries': |
108
|
9 |
|
case 'publishPeerEntries': |
109
|
9 |
|
case 'publishPeerEntryDrafts': |
110
|
5 |
|
$service = Craft::$app->sections; |
111
|
5 |
|
$method = 'getSectionBy'; |
112
|
5 |
|
break; |
113
|
9 |
|
case 'group': |
114
|
7 |
|
case 'editCategories': |
115
|
5 |
|
$service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories; |
116
|
5 |
|
$method = 'getGroupBy'; |
117
|
5 |
|
break; |
118
|
7 |
|
case 'folder': |
119
|
5 |
|
case 'createFoldersInVolume': |
120
|
5 |
|
case 'deleteFilesAndFoldersInVolume': |
121
|
5 |
|
case 'saveAssetInVolume': |
122
|
5 |
|
case 'viewVolume': |
123
|
5 |
|
$service = Craft::$app->volumes; |
124
|
5 |
|
$method = 'getVolumeBy'; |
125
|
5 |
|
break; |
126
|
2 |
|
case 'taggroup': |
127
|
|
|
$service = Craft::$app->tags; |
128
|
|
|
$method = 'getTagGroupBy'; |
129
|
|
|
break; |
130
|
2 |
|
case 'field': |
131
|
2 |
|
$service = Craft::$app->fields; |
132
|
2 |
|
$method = 'getFieldBy'; |
133
|
2 |
|
break; |
134
|
|
|
case 'editGlobalSet': |
135
|
|
|
$service = Craft::$app->globals; |
136
|
|
|
$method = 'getSetBy'; |
137
|
|
|
break; |
138
|
|
|
case 'utility': |
139
|
|
|
return $source; |
140
|
|
|
} |
141
|
|
|
|
142
|
9 |
|
if (isset($service) && isset($method) && isset($sourceFrom)) { |
143
|
9 |
|
$method = $method.ucfirst($indexFrom); |
144
|
|
|
try { |
145
|
9 |
|
$sourceObject = $service->$method($sourceFrom); |
146
|
|
|
} catch (TypeError $e) { |
147
|
|
|
Schematic::error('An error occured mapping source '.$source.' from '.$indexFrom.' to '.$indexTo); |
148
|
|
|
Schematic::error($e->getMessage()); |
149
|
|
|
|
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
9 |
|
if ($sourceObject) { |
155
|
8 |
|
return $sourceType.':'.$sourceObject->$indexTo; |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
Schematic::warning('No mapping found for source '.$source); |
159
|
|
|
|
160
|
1 |
|
return null; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|