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 Illuminate\Contracts\Cache\Factory; |
23
|
|
|
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class AbstractCache |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractCache implements MethodInterceptor |
29
|
|
|
{ |
30
|
|
|
use AnnotationReaderTrait; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $join = ":"; |
34
|
|
|
|
35
|
|
|
/** @var Factory */ |
36
|
|
|
protected static $factory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $name |
40
|
|
|
* @param MethodInvocation $invocation |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
protected function generateCacheName($name, MethodInvocation $invocation) |
45
|
|
|
{ |
46
|
|
|
if (is_array($name)) { |
47
|
|
|
throw new \InvalidArgumentException('Invalid argument'); |
48
|
|
|
} |
49
|
|
|
if (is_null($name)) { |
50
|
|
|
$name = $invocation->getMethod()->name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return [$name]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param MethodInvocation $invocation |
58
|
|
|
* @param $annotation |
59
|
|
|
* @param $keys |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
protected function detectCacheKeys(MethodInvocation $invocation, $annotation, $keys) |
64
|
|
|
{ |
65
|
|
|
$arguments = $invocation->getArguments(); |
66
|
|
|
foreach ($invocation->getMethod()->getParameters() as $parameter) { |
67
|
|
|
// exclude object |
68
|
|
|
if (in_array('#' . $parameter->name, $annotation->key)) { |
69
|
|
|
if (isset($arguments[$parameter->getPosition()])) { |
70
|
|
|
if (!is_object($arguments[$parameter->getPosition()])) { |
71
|
|
|
$keys[] = $arguments[$parameter->getPosition()]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
if (!isset($arguments[$parameter->getPosition()])) { |
75
|
|
|
$keys[] = $parameter->getDefaultValue(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return $keys; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $annotation |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Contracts\Cache\Repository |
86
|
|
|
*/ |
87
|
|
|
protected function detectCacheRepository($annotation) |
88
|
|
|
{ |
89
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository $cache */ |
90
|
|
|
$cache = self::$factory->store($annotation->driver); |
91
|
|
|
if (count($annotation->tags)) { |
92
|
|
|
$cache = $cache->tags($annotation->tags); |
93
|
|
|
|
94
|
|
|
return $cache; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $cache; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* set cache instance |
102
|
|
|
* @param Factory $factory |
103
|
|
|
*/ |
104
|
|
|
public function setCache(Factory $factory) |
105
|
|
|
{ |
106
|
|
|
self::$factory = $factory; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|