|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/craft-psr6/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/craft-psr6 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\psr6; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use Flipbox\Stash\Pool; |
|
13
|
|
|
use flipbox\craft\psr6\events\RegisterCachePools; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Stash\Driver\BlackHole; |
|
16
|
|
|
use Stash\Driver\FileSystem; |
|
17
|
|
|
use Stash\Interfaces\DriverInterface; |
|
18
|
|
|
use Stash\Interfaces\PoolInterface; |
|
19
|
|
|
use yii\base\Component; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Flipbox Factory <[email protected]> |
|
23
|
|
|
* @since 1.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class Cache extends Component |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var LoggerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
public $logger; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The event name |
|
34
|
|
|
*/ |
|
35
|
|
|
const EVENT_REGISTER_CACHE_POOLS = 'registerCachePools'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return PoolInterface[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public function findAll() |
|
41
|
|
|
{ |
|
42
|
|
|
$cacheDrivers = [ |
|
43
|
|
|
'default' => $this->getApplicationPool(), |
|
44
|
|
|
'dummy' => $this->createDummyPool(), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
$event = new RegisterCachePools([ |
|
48
|
|
|
'pools' => $cacheDrivers |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
Craft::$app->trigger( |
|
52
|
|
|
self::EVENT_REGISTER_CACHE_POOLS, |
|
53
|
|
|
$event |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
return $event->getPools(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $handle |
|
61
|
|
|
* @return PoolInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
public function get(string $handle = 'default') |
|
64
|
|
|
{ |
|
65
|
|
|
// Get all |
|
66
|
|
|
$pools = $this->findAll(); |
|
67
|
|
|
|
|
68
|
|
|
if (!array_key_exists($handle, $pools)) { |
|
69
|
|
|
Craft::error( |
|
70
|
|
|
sprintf( |
|
71
|
|
|
"Cache pool does not exist: '%s'.", |
|
72
|
|
|
$handle |
|
73
|
|
|
), |
|
74
|
|
|
'PSR-6' |
|
75
|
|
|
); |
|
76
|
|
|
return $this->createDummyPool(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $pools[$handle]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return Pool |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getApplicationPool() |
|
86
|
|
|
{ |
|
87
|
|
|
// New cache pool |
|
88
|
|
|
$pool = new Pool( |
|
89
|
|
|
$this->getApplicationDriver() |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
// Set default duration |
|
93
|
|
|
$pool->setItemDuration( |
|
94
|
|
|
Craft::$app->getConfig()->getGeneral()->cacheDuration |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
// Add logging |
|
98
|
|
|
$this->setLogger($pool); |
|
99
|
|
|
|
|
100
|
|
|
return $pool; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return DriverInterface |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getApplicationDriver() |
|
107
|
|
|
{ |
|
108
|
|
|
// Todo - support all the native Craft cache methods |
|
109
|
|
|
|
|
110
|
|
|
// File cache config |
|
111
|
|
|
$fileCacheConfig = Craft::$app->getConfig()->getFileCache(); |
|
112
|
|
|
|
|
113
|
|
|
return new FileSystem([ |
|
114
|
|
|
'path' => Craft::getAlias($fileCacheConfig->cachePath) |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return Pool |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function createDummyPool() |
|
122
|
|
|
{ |
|
123
|
|
|
// New cache pool |
|
124
|
|
|
$pool = new Pool( |
|
125
|
|
|
new BlackHole() |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
$this->setLogger($pool); |
|
129
|
|
|
|
|
130
|
|
|
return new Pool(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param \Stash\Pool $pool |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function setLogger(\Stash\Pool $pool) |
|
137
|
|
|
{ |
|
138
|
|
|
if (null === $this->logger) { |
|
139
|
|
|
$this->logger = $this->applicationLogger(false); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if ($this->isLoggerValid($this->logger)) { |
|
143
|
|
|
$pool->setLogger($this->logger); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param $logger |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
|
|
private function isLoggerValid($logger) |
|
152
|
|
|
{ |
|
153
|
|
|
return $logger && $logger instanceof LoggerInterface; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param null $default |
|
158
|
|
|
* @return null|object |
|
159
|
|
|
*/ |
|
160
|
|
|
private function applicationLogger($default = null) |
|
161
|
|
|
{ |
|
162
|
|
|
if (!Craft::$app->has('psr3-logger')) { |
|
163
|
|
|
return $default; |
|
164
|
|
|
} |
|
165
|
|
|
return Craft::$app->get('psr3-logger'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: