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\Cache\CacheItemPoolInterface; |
17
|
|
|
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface; |
18
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
19
|
|
|
use Symfony\Component\Cache\Psr16Cache; |
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
|
|
|
* |
39
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
40
|
|
|
*/ |
41
|
3 |
|
public function getCache() |
42
|
|
|
{ |
43
|
3 |
|
if ($this->cache) { |
44
|
2 |
|
return $this->cache; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) { |
48
|
1 |
|
$this->setCache($this->app['cache']); |
49
|
|
|
|
50
|
|
|
// Fix PHPStan error |
51
|
1 |
|
assert($this->cache instanceof \Psr\SimpleCache\CacheInterface); |
52
|
|
|
|
53
|
1 |
|
return $this->cache; |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
return $this->cache = $this->createDefaultCache(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set cache instance. |
61
|
|
|
* |
62
|
|
|
* @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
* |
66
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
67
|
|
|
*/ |
68
|
2 |
|
public function setCache($cache) |
69
|
|
|
{ |
70
|
2 |
|
if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) { |
71
|
1 |
|
throw new InvalidArgumentException( |
72
|
1 |
|
\sprintf('The cache instance must implements %s or %s interface.', |
73
|
1 |
|
SimpleCacheInterface::class, CacheItemPoolInterface::class |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
if ($cache instanceof CacheItemPoolInterface) { |
79
|
2 |
|
if (!$this->isSymfony43()) { |
80
|
|
|
throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class)); |
81
|
|
|
} |
82
|
2 |
|
$cache = new Psr16Cache($cache); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$this->cache = $cache; |
86
|
|
|
|
87
|
2 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return \Psr\SimpleCache\CacheInterface |
92
|
|
|
*/ |
93
|
3 |
|
protected function createDefaultCache() |
94
|
|
|
{ |
95
|
3 |
|
if ($this->isSymfony43()) { |
96
|
3 |
|
return new Psr16Cache(new FilesystemAdapter('easywechat', 1500)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return new FilesystemCache(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
3 |
|
protected function isSymfony43(): bool |
106
|
|
|
{ |
107
|
3 |
|
return \class_exists('Symfony\Component\Cache\Psr16Cache'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|