1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of phpFastCache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
10
|
|
|
* |
11
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com |
12
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phpfastcache\Core\Pool; |
18
|
|
|
|
19
|
|
|
use DateTime; |
20
|
|
|
use Exception; |
21
|
|
|
use Phpfastcache\Config\ConfigurationOption; |
22
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
23
|
|
|
use Phpfastcache\Entities\DriverIO; |
24
|
|
|
use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException, PhpfastcacheDriverConnectException}; |
25
|
|
|
use ReflectionObject; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class DriverBaseTrait |
30
|
|
|
* @package phpFastCache\Cache |
31
|
|
|
*/ |
32
|
|
|
trait DriverBaseTrait |
33
|
|
|
{ |
34
|
|
|
use ExtendedCacheItemPoolTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ConfigurationOption the options |
38
|
|
|
*/ |
39
|
|
|
protected $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $fallback = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var mixed Instance of driver service |
48
|
|
|
*/ |
49
|
|
|
protected $instance; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $driverName; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @internal This variable is read-access only |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $instanceId; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Driver constructor. |
64
|
|
|
* @param ConfigurationOption $config |
65
|
|
|
* @param string $instanceId |
66
|
|
|
* @throws PhpfastcacheDriverCheckException |
67
|
|
|
* @throws PhpfastcacheDriverConnectException |
68
|
|
|
*/ |
69
|
|
|
public function __construct(ConfigurationOption $config, $instanceId) |
70
|
|
|
{ |
71
|
|
|
$this->setConfig($config); |
72
|
|
|
$this->instanceId = $instanceId; |
73
|
|
|
$this->IO = new DriverIO(); |
74
|
|
|
|
75
|
|
|
if (!$this->driverCheck()) { |
76
|
|
|
throw new PhpfastcacheDriverCheckException(\sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$this->driverConnect(); |
81
|
|
|
} catch (Exception $e) { |
82
|
|
|
throw new PhpfastcacheDriverConnectException( |
83
|
|
|
sprintf( |
84
|
|
|
self::DRIVER_CONNECT_FAILURE, |
|
|
|
|
85
|
|
|
$this->getDriverName(), |
86
|
|
|
$e->getMessage(), |
87
|
|
|
$e->getLine() ?: 'unknown line', |
88
|
|
|
$e->getFile() ?: 'unknown file' |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getDriverName(): string |
98
|
|
|
{ |
99
|
|
|
if (!$this->driverName) { |
100
|
|
|
$this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
101
|
|
|
} |
102
|
|
|
return $this->driverName; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return ConfigurationOption |
107
|
|
|
*/ |
108
|
|
|
public function getDefaultConfig(): ConfigurationOption |
109
|
|
|
{ |
110
|
|
|
$className = self::getConfigClass(); |
111
|
|
|
return new $className; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public static function getConfigClass(): string |
118
|
|
|
{ |
119
|
|
|
$localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
120
|
|
|
if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
121
|
|
|
return $localConfigClass; |
122
|
|
|
} |
123
|
|
|
return ConfigurationOption::class; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param ExtendedCacheItemInterface $item |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function driverPreWrap(ExtendedCacheItemInterface $item): array |
131
|
|
|
{ |
132
|
|
|
$wrap = [ |
133
|
|
|
self::DRIVER_DATA_WRAPPER_INDEX => $item->get(), |
|
|
|
|
134
|
|
|
self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
|
|
|
135
|
|
|
self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
|
|
|
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
if ($this->getConfig()->isItemDetailedDate()) { |
139
|
|
|
$wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime(); |
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* If the creation date exists |
142
|
|
|
* reuse it else set a new Date |
143
|
|
|
*/ |
144
|
|
|
$wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate() ?: new DateTime(); |
|
|
|
|
145
|
|
|
} else { |
146
|
|
|
$wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = null; |
147
|
|
|
$wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $wrap; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return ConfigurationOption |
155
|
|
|
*/ |
156
|
|
|
public function getConfig(): ConfigurationOption |
157
|
|
|
{ |
158
|
|
|
return $this->config; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param ConfigurationOption $config |
163
|
|
|
*/ |
164
|
|
|
public function setConfig(ConfigurationOption $config) |
165
|
|
|
{ |
166
|
|
|
$this->config = $config; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array $wrapper |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
|
|
public function driverUnwrapData(array $wrapper) |
174
|
|
|
{ |
175
|
|
|
return $wrapper[self::DRIVER_DATA_WRAPPER_INDEX]; |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $wrapper |
181
|
|
|
* @return DateTime |
182
|
|
|
*/ |
183
|
|
|
public function driverUnwrapEdate(array $wrapper) |
184
|
|
|
{ |
185
|
|
|
return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX]; |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $wrapper |
190
|
|
|
* @return DateTime |
191
|
|
|
*/ |
192
|
|
|
public function driverUnwrapCdate(array $wrapper) |
193
|
|
|
{ |
194
|
|
|
return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX]; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array $wrapper |
199
|
|
|
* @return DateTime |
200
|
|
|
*/ |
201
|
|
|
public function driverUnwrapMdate(array $wrapper) |
202
|
|
|
{ |
203
|
|
|
return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX]; |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getInstanceId(): string |
210
|
|
|
{ |
211
|
|
|
return $this->instanceId; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Encode data types such as object/array |
217
|
|
|
* for driver that does not support |
218
|
|
|
* non-scalar value |
219
|
|
|
* @param $data |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
protected function encode($data): string |
223
|
|
|
{ |
224
|
|
|
return \serialize($data); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Decode data types such as object/array |
229
|
|
|
* for driver that does not support |
230
|
|
|
* non-scalar value |
231
|
|
|
* @param string|null $value |
232
|
|
|
* @return mixed |
233
|
|
|
*/ |
234
|
|
|
protected function decode($value) |
235
|
|
|
{ |
236
|
|
|
return \unserialize((string)$value, ['allowed_classes' => true]); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Check if phpModule or CGI |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
protected function isPHPModule(): bool |
244
|
|
|
{ |
245
|
|
|
return (\PHP_SAPI === 'apache2handler' || \strpos(\PHP_SAPI, 'handler') !== false); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|