|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\ember\services\traits; |
|
10
|
|
|
|
|
11
|
|
|
use craft\base\Element; |
|
12
|
|
|
use craft\base\ElementInterface; |
|
13
|
|
|
use craft\elements\db\ElementQuery; |
|
14
|
|
|
use flipbox\ember\exceptions\ElementNotFoundException; |
|
15
|
|
|
use flipbox\ember\helpers\SiteHelper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Flipbox Factory <[email protected]> |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
trait ElementAccessorByString |
|
22
|
|
|
{ |
|
23
|
|
|
use ElementAccessor { |
|
24
|
|
|
find as parentFind; |
|
25
|
|
|
findCache as parentFindCache; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var [ElementInterface[]] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $cacheByString = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract protected function stringProperty(): string; |
|
37
|
|
|
|
|
38
|
|
|
/******************************************* |
|
39
|
|
|
* STRING |
|
40
|
|
|
*******************************************/ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ElementInterface $element |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function stringValue(ElementInterface $element) |
|
47
|
|
|
{ |
|
48
|
|
|
$property = $this->stringProperty(); |
|
49
|
|
|
|
|
50
|
|
|
return $element->{$property}; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/******************************************* |
|
55
|
|
|
* FIND OVERRIDES |
|
56
|
|
|
*******************************************/ |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public function find($identifier, int $siteId = null, string $toScenario = null) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($element = $this->parentFind($identifier, $siteId, $toScenario)) { |
|
|
|
|
|
|
64
|
|
|
return $element; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!is_string($identifier)) { |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->findByString($identifier, $siteId, $toScenario); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/******************************************* |
|
75
|
|
|
* CACHE OVERRIDES |
|
76
|
|
|
*******************************************/ |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritdoc |
|
80
|
|
|
*/ |
|
81
|
|
|
public function findCache($identifier, int $siteId = null) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($element = $this->parentFindCache($identifier, $siteId)) { |
|
|
|
|
|
|
84
|
|
|
return $element; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (!is_string($identifier)) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->findCacheByString($identifier, $siteId); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function addToCache(ElementInterface $element) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->cacheById($element); |
|
100
|
|
|
$this->cacheByString($element); |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/******************************************* |
|
106
|
|
|
* FIND/GET BY STRING |
|
107
|
|
|
*******************************************/ |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $string |
|
111
|
|
|
* @param int|null $siteId |
|
112
|
|
|
* @param string|null $toScenario |
|
113
|
|
|
* @return ElementInterface|null |
|
114
|
|
|
*/ |
|
115
|
|
|
public function findByString(string $string, int $siteId = null, string $toScenario = null) |
|
116
|
|
|
{ |
|
117
|
|
|
$siteId = SiteHelper::resolveSiteId($siteId); |
|
118
|
|
|
|
|
119
|
|
|
if (!$element = $this->findCacheByString($string, $siteId)) { |
|
120
|
|
|
if (!$element = $this->freshFindByString($string, $siteId)) { |
|
121
|
|
|
$this->cacheByString[$siteId][$string] = null; |
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->addToCache($element); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $this->applyScenario($element, $toScenario); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $string |
|
133
|
|
|
* @param string|null $toScenario |
|
134
|
|
|
* @return ElementInterface|null |
|
135
|
|
|
* @throws ElementNotFoundException |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getByString(string $string, string $toScenario = null): ElementInterface |
|
138
|
|
|
{ |
|
139
|
|
|
if (!$element = $this->findByString($string, $toScenario)) { |
|
140
|
|
|
$this->notFoundByStringException($string); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $element; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $string |
|
148
|
|
|
* @param int|null $siteId |
|
149
|
|
|
* @param string $toScenario |
|
150
|
|
|
* @return ElementInterface|null |
|
151
|
|
|
*/ |
|
152
|
|
|
public function freshFindByString(string $string, int $siteId = null, string $toScenario = null) |
|
153
|
|
|
{ |
|
154
|
|
|
/** @var ElementQuery $query */ |
|
155
|
|
|
$query = $this->getQuery(); |
|
156
|
|
|
$query->{$this->stringProperty()} = $string; |
|
157
|
|
|
$query->siteId = $siteId; |
|
158
|
|
|
$query->status = null; |
|
159
|
|
|
$query->enabledForSite = false; |
|
160
|
|
|
|
|
161
|
|
|
if (!$element = $query->one()) { |
|
162
|
|
|
return null; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->applyScenario($element, $toScenario); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $string |
|
170
|
|
|
* @param string|null $toScenario |
|
171
|
|
|
* @return ElementInterface |
|
172
|
|
|
* @throws ElementNotFoundException |
|
173
|
|
|
*/ |
|
174
|
|
|
public function freshGetByString(string $string, string $toScenario = null): ElementInterface |
|
175
|
|
|
{ |
|
176
|
|
|
if (!$element = $this->freshFindByString($string, $toScenario)) { |
|
177
|
|
|
$this->notFoundByStringException($string); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $element; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/******************************************* |
|
185
|
|
|
* CACHE BY STRING |
|
186
|
|
|
*******************************************/ |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Find an existing cache by ID |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $string |
|
192
|
|
|
* @param int|null $siteId |
|
193
|
|
|
* @return ElementInterface|null |
|
194
|
|
|
*/ |
|
195
|
|
|
public function findCacheByString(string $string, int $siteId = null) |
|
196
|
|
|
{ |
|
197
|
|
|
$siteId = SiteHelper::resolveSiteId($siteId); |
|
198
|
|
|
|
|
199
|
|
|
if ($this->isCachedByString($string, $siteId)) { |
|
200
|
|
|
return $this->cacheByString[$siteId][$string]; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return null; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Identify whether in cached by ID |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $string |
|
210
|
|
|
* @param int|null $siteId |
|
211
|
|
|
* @return bool |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function isCachedByString(string $string, int $siteId = null): bool |
|
214
|
|
|
{ |
|
215
|
|
|
$siteId = SiteHelper::resolveSiteId($siteId); |
|
216
|
|
|
|
|
217
|
|
|
if (!array_key_exists($siteId, $this->cacheByString)) { |
|
218
|
|
|
$this->cacheByString[$siteId] = []; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return array_key_exists($string, $this->cacheByString[$siteId]); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param ElementInterface|Element $element |
|
226
|
|
|
* @return $this |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function cacheByString(ElementInterface $element) |
|
229
|
|
|
{ |
|
230
|
|
|
$stringValue = $this->stringValue($element); |
|
231
|
|
|
|
|
232
|
|
|
if (null === $stringValue) { |
|
233
|
|
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$siteId = $element->siteId; |
|
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
// todo - ensure siteId is set? |
|
239
|
|
|
|
|
240
|
|
|
// Check if already in cache |
|
241
|
|
|
if (!$this->isCachedByString($stringValue, $siteId)) { |
|
242
|
|
|
$this->cacheByString[$siteId][$stringValue] = $element; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/******************************************* |
|
249
|
|
|
* EXCEPTIONS |
|
250
|
|
|
*******************************************/ |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @param string|null $string |
|
254
|
|
|
* @throws ElementNotFoundException |
|
255
|
|
|
*/ |
|
256
|
|
|
protected function notFoundByStringException(string $string = null) |
|
257
|
|
|
{ |
|
258
|
|
|
throw new ElementNotFoundException( |
|
259
|
|
|
sprintf( |
|
260
|
|
|
'Element does not exist with the string "%s".', |
|
261
|
|
|
(string)$string |
|
262
|
|
|
) |
|
263
|
|
|
); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.