|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
10
|
|
|
* THE SOFTWARE. |
|
11
|
|
|
* |
|
12
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
13
|
|
|
* and is licensed under the MIT license. |
|
14
|
|
|
* |
|
15
|
|
|
* Copyright (c) 2015-2016 Yuuki Takezawa |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace Ytake\LaravelAspect\Interceptor; |
|
19
|
|
|
|
|
20
|
|
|
use Ray\Aop\MethodInvocation; |
|
21
|
|
|
use Ray\Aop\MethodInterceptor; |
|
22
|
|
|
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class AbstractCache |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractCache implements MethodInterceptor |
|
28
|
|
|
{ |
|
29
|
|
|
use AnnotationReaderTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $join = ":"; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $name |
|
36
|
|
|
* @param MethodInvocation $invocation |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function generateCacheName($name, MethodInvocation $invocation) |
|
41
|
|
|
{ |
|
42
|
|
|
if (is_array($name)) { |
|
43
|
|
|
throw new \InvalidArgumentException('Invalid argument'); |
|
44
|
|
|
} |
|
45
|
|
|
if (is_null($name)) { |
|
46
|
|
|
$name = $invocation->getMethod()->name; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return [$name]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param MethodInvocation $invocation |
|
54
|
|
|
* @param $annotation |
|
55
|
|
|
* @param $keys |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function detectCacheKeys(MethodInvocation $invocation, $annotation, $keys) |
|
60
|
|
|
{ |
|
61
|
|
|
$arguments = $invocation->getArguments(); |
|
62
|
|
|
foreach ($invocation->getMethod()->getParameters() as $parameter) { |
|
63
|
|
|
// exclude object |
|
64
|
|
|
if (in_array('#' . $parameter->name, $annotation->key)) { |
|
65
|
|
|
if (isset($arguments[$parameter->getPosition()])) { |
|
66
|
|
|
if (!is_object($arguments[$parameter->getPosition()])) { |
|
67
|
|
|
$keys[] = $arguments[$parameter->getPosition()]; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
if (!isset($arguments[$parameter->getPosition()])) { |
|
71
|
|
|
$keys[] = $parameter->getDefaultValue(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
return $keys; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $annotation |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Illuminate\Contracts\Cache\Repository |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function detectCacheRepository($annotation) |
|
84
|
|
|
{ |
|
85
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository $cache */ |
|
86
|
|
|
$cache = app('cache')->store($annotation->driver); |
|
87
|
|
|
if (count($annotation->tags)) { |
|
88
|
|
|
$cache = $cache->tags($annotation->tags); |
|
89
|
|
|
|
|
90
|
|
|
return $cache; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $cache; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|