|
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\craft\ember\queries; |
|
10
|
|
|
|
|
11
|
|
|
use yii\base\ArrayableTrait; |
|
12
|
|
|
|
|
13
|
|
|
class CacheableActiveQuery extends ActiveQuery |
|
14
|
|
|
{ |
|
15
|
|
|
use ArrayableTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array|null The cached query result |
|
19
|
|
|
* @see setCachedResult() |
|
20
|
|
|
*/ |
|
21
|
|
|
private $result; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array|null The criteria params that were set when the cached query result was set |
|
25
|
|
|
* @see setCachedResult() |
|
26
|
|
|
*/ |
|
27
|
|
|
private $resultCriteria; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritdoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public function count($q = '*', $db = null) |
|
33
|
|
|
{ |
|
34
|
|
|
// Cached? |
|
35
|
|
|
if (($cachedResult = $this->getCachedResult()) !== null) { |
|
36
|
|
|
return count($cachedResult); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return parent::count($q, $db) ?: 0; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function all($db = null) |
|
46
|
|
|
{ |
|
47
|
|
|
// Cached? |
|
48
|
|
|
if (($cachedResult = $this->getCachedResult()) !== null) { |
|
49
|
|
|
return $cachedResult; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return parent::all($db); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function one($db = null) |
|
59
|
|
|
{ |
|
60
|
|
|
if (($cachedResult = $this->getCachedResult()) !== null) { |
|
61
|
|
|
return reset($cachedResult); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return parent::one($db); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the results set by [[setCachedResult()]], if the criteria params haven’t changed since then. |
|
69
|
|
|
* |
|
70
|
|
|
* @return array|null The results, or null if setCachedResult() was never called or the criteria has |
|
71
|
|
|
* changed |
|
72
|
|
|
* @see setCachedResult() |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getCachedResult() |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->result === null) { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Make sure the criteria hasn't changed |
|
81
|
|
|
if ($this->resultCriteria !== $this->getCriteria()) { |
|
82
|
|
|
$this->result = null; |
|
83
|
|
|
|
|
84
|
|
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Sets the results. |
|
92
|
|
|
* |
|
93
|
|
|
* If this is called, [[all()]] will return these domains rather than initiating a new SQL query, |
|
94
|
|
|
* as long as none of the parameters have changed since setCachedResult() was called. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $objects The resulting objects. |
|
97
|
|
|
* |
|
98
|
|
|
* @see getCachedResult() |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setCachedResult(array $objects) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->result = $objects; |
|
103
|
|
|
$this->resultCriteria = $this->getCriteria(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Clears the results. |
|
108
|
|
|
* |
|
109
|
|
|
* @see getCachedResult() |
|
110
|
|
|
*/ |
|
111
|
|
|
public function clearCachedResult() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->result = null; |
|
114
|
|
|
$this->resultCriteria = null; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns an array of the current criteria attribute values. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getCriteria(): array |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->toArray($this->criteriaAttributes(), [], false); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
129
|
|
|
* |
|
130
|
|
|
* Returns the query's criteria attributes. |
|
131
|
|
|
* |
|
132
|
|
|
* @return string[] |
|
133
|
|
|
*/ |
|
134
|
|
|
public function criteriaAttributes(): array |
|
135
|
|
|
{ |
|
136
|
|
|
// By default, include all public, non-static properties that were defined by a sub class, and certain ones |
|
137
|
|
|
// in this class |
|
138
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
139
|
|
|
$class = new \ReflectionClass($this); |
|
140
|
|
|
$names = []; |
|
141
|
|
|
|
|
142
|
|
|
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
|
143
|
|
|
if (!$property->isStatic()) { |
|
144
|
|
|
$dec = $property->getDeclaringClass(); |
|
145
|
|
|
if (($dec->getName() === self::class || $dec->isSubclassOf(self::class)) |
|
146
|
|
|
) { |
|
147
|
|
|
$names[] = $property->getName(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $names; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|