|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
11
|
|
|
* THE SOFTWARE. |
|
12
|
|
|
* |
|
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
14
|
|
|
* and is licensed under the MIT license. |
|
15
|
|
|
* |
|
16
|
|
|
* Copyright (c) 2015-2020 Yuuki Takezawa |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Ytake\LaravelAspect\Interceptor; |
|
21
|
|
|
|
|
22
|
|
|
use Ray\Aop\MethodInvocation; |
|
23
|
|
|
use Ytake\LaravelAspect\Annotation\Cacheable; |
|
24
|
|
|
|
|
25
|
|
|
use function is_array; |
|
26
|
|
|
use function is_null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class CacheableInterceptor |
|
30
|
|
|
*/ |
|
31
|
|
|
class CacheableInterceptor extends AbstractCache |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @param MethodInvocation $invocation |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function invoke(MethodInvocation $invocation) |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var Cacheable $annotation */ |
|
41
|
|
|
$annotation = $invocation->getMethod()->getAnnotation($this->annotation) |
|
42
|
|
|
?? new $this->annotation([]); |
|
43
|
|
|
$keys = $this->generateCacheName($annotation->cacheName, $invocation); |
|
44
|
|
|
if (!is_array($annotation->key)) { |
|
45
|
|
|
$annotation->key = [$annotation->key]; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
$keys = $this->detectCacheKeys($invocation, $annotation, $keys); |
|
48
|
|
|
// detect use cache driver |
|
49
|
|
|
$cache = $this->detectCacheRepository($annotation); |
|
50
|
|
|
$key = $this->recursiveImplode($this->join, $keys); |
|
51
|
|
|
if ($cache->has($key)) { |
|
52
|
|
|
return $cache->get($key); |
|
53
|
|
|
} |
|
54
|
|
|
$result = $invocation->proceed(); |
|
55
|
|
|
if (!$annotation->negative) { |
|
56
|
|
|
if ($result) { |
|
57
|
|
|
$cache->add($key, $result, $annotation->lifetime); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $result; |
|
61
|
|
|
} |
|
62
|
|
|
if (is_null($result)) { |
|
63
|
|
|
$cache->add($key, $result, $annotation->lifetime); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..