|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyWeChat\Kernel\Traits; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
|
15
|
|
|
use EasyWeChat\Kernel\ServiceContainer; |
|
16
|
|
|
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface; |
|
17
|
|
|
use Symfony\Component\Cache\Adapter\AbstractAdapter; |
|
18
|
|
|
use Symfony\Component\Cache\Psr16Cache; |
|
19
|
|
|
use \Psr\Cache\CacheItemPoolInterface; |
|
20
|
|
|
use Symfony\Component\Cache\Simple\FilesystemCache; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Trait InteractsWithCache. |
|
24
|
|
|
* |
|
25
|
|
|
* @author overtrue <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
trait InteractsWithCache |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Psr\SimpleCache\CacheInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $cache; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get cache instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @return \Psr\SimpleCache\CacheInterface |
|
38
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function getCache() |
|
41
|
|
|
{ |
|
42
|
3 |
|
if ($this->cache) { |
|
43
|
2 |
|
return $this->cache; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) { |
|
47
|
1 |
|
$this->setCache($this->app['cache']); |
|
48
|
1 |
|
return $this->cache; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
return $this->cache = $this->createDefaultCache(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set cache instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache |
|
58
|
|
|
* |
|
59
|
|
|
* @return $this |
|
60
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function setCache($cache) |
|
63
|
|
|
{ |
|
64
|
2 |
|
if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) { |
|
65
|
|
|
throw new InvalidArgumentException( |
|
66
|
|
|
\sprintf('The cache instance must implements %s or %s interface.', |
|
67
|
|
|
SimpleCacheInterface::class, CacheItemPoolInterface::class |
|
68
|
|
|
) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
if ($cache instanceof CacheItemPoolInterface) { |
|
73
|
|
|
if (!$this->isSymfony43()) { |
|
74
|
|
|
throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class)); |
|
75
|
|
|
} |
|
76
|
|
|
$cache = new Psr16Cache($cache); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
$this->cache = $cache; |
|
80
|
|
|
|
|
81
|
2 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \Psr\SimpleCache\CacheInterface |
|
86
|
|
|
*/ |
|
87
|
3 |
|
protected function createDefaultCache() |
|
88
|
|
|
{ |
|
89
|
3 |
|
if ($this->isSymfony43()) { |
|
90
|
|
|
return new Psr16Cache(AbstractAdapter::createSystemCache('easywechat', 2700, '1.0', \sys_get_temp_dir())); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
return new FilesystemCache(); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
3 |
|
protected function isSymfony43(): bool |
|
100
|
|
|
{ |
|
101
|
3 |
|
return \class_exists('Symfony\Component\Cache\Psr16Cache'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|