1 | <?php |
||
9 | trait Cacheable |
||
10 | { |
||
11 | /** |
||
12 | * The repository cache lifetime. |
||
13 | * |
||
14 | * @var int |
||
15 | */ |
||
16 | protected $cacheLifetime; |
||
17 | |||
18 | /** |
||
19 | * The repository cache driver. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $cacheDriver; |
||
24 | |||
25 | /** |
||
26 | * Indicate if the repository cache clear is enabled. |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $cacheClearEnabled = true; |
||
31 | |||
32 | /** |
||
33 | * Generate unique query hash. |
||
34 | * |
||
35 | * @param $args |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | protected function generateCacheHash($args): string |
||
55 | |||
56 | /** |
||
57 | * Store cache keys by mimicking cache tags. |
||
58 | * |
||
59 | * @param string $class |
||
60 | * @param string $method |
||
61 | * @param string $hash |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | protected function storeCacheKeys($class, $method, $hash): void |
||
75 | |||
76 | /** |
||
77 | * Get cache keys. |
||
78 | * |
||
79 | * @param string $file |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getCacheKeys($file): array |
||
91 | |||
92 | /** |
||
93 | * Flush cache keys by mimicking cache tags. |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | protected function flushCacheKeys(): array |
||
98 | { |
||
99 | $flushedKeys = []; |
||
100 | $calledClass = static::class; |
||
101 | $config = $this->getContainer('config')->get('rinvex.repository.cache'); |
||
102 | $cacheKeys = $this->getCacheKeys($config['keys_file']); |
||
103 | |||
104 | if (isset($cacheKeys[$calledClass]) && is_array($cacheKeys[$calledClass])) { |
||
105 | foreach ($cacheKeys[$calledClass] as $cacheKey) { |
||
106 | $flushedKeys[] = $calledClass.'@'.$cacheKey; |
||
107 | } |
||
108 | |||
109 | unset($cacheKeys[$calledClass]); |
||
110 | file_put_contents($config['keys_file'], json_encode($cacheKeys)); |
||
111 | } |
||
112 | |||
113 | return $flushedKeys; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function setCacheLifetime($cacheLifetime) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function getCacheLifetime(): int |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function setCacheDriver($cacheDriver) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function getCacheDriver(): ?string |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function enableCacheClear($status = true) |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function isCacheClearEnabled(): bool |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function forgetCache() |
||
192 | |||
193 | /** |
||
194 | * Cache given callback. |
||
195 | * |
||
196 | * @param string $class |
||
197 | * @param string $method |
||
198 | * @param array $args |
||
199 | * @param \Closure $closure |
||
200 | * |
||
201 | * @return mixed |
||
202 | */ |
||
203 | protected function cacheCallback($class, $method, $args, Closure $closure) |
||
239 | |||
240 | /** |
||
241 | * Reset cached repository to its defaults. |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | protected function resetCachedRepository() |
||
254 | } |
||
255 |
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
Idable
provides a methodequalsId
that 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.