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 and LICENCE files. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Core\Pool; |
17
|
|
|
|
18
|
|
|
use DateTime; |
19
|
|
|
use DateTimeInterface; |
20
|
|
|
use Phpfastcache\Config\ConfigurationOptionInterface; |
21
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
22
|
|
|
use Phpfastcache\Util\ClassNamespaceResolverTrait; |
23
|
|
|
use Throwable; |
24
|
|
|
use Phpfastcache\Config\ConfigurationOption; |
25
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
26
|
|
|
use Phpfastcache\Entities\DriverIO; |
27
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheCoreException; |
28
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; |
29
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException; |
30
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheIOException; |
31
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
32
|
|
|
use ReflectionObject; |
33
|
|
|
|
34
|
|
|
trait DriverBaseTrait |
35
|
|
|
{ |
36
|
|
|
use DriverPoolAbstractTrait; |
37
|
|
|
use ClassNamespaceResolverTrait; |
38
|
|
|
|
39
|
|
|
protected static array $cacheItemClasses = []; |
40
|
|
|
|
41
|
|
|
protected ConfigurationOptionInterface $config; |
42
|
|
|
|
43
|
|
|
protected object|array|null $instance; |
|
|
|
|
44
|
|
|
|
45
|
|
|
protected string $driverName; |
46
|
|
|
|
47
|
|
|
protected string $instanceId; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Driver constructor. |
51
|
|
|
* @param ConfigurationOptionInterface $config |
52
|
|
|
* @param string $instanceId |
53
|
|
|
* @throws PhpfastcacheCoreException |
54
|
|
|
* @throws PhpfastcacheDriverCheckException |
55
|
|
|
* @throws PhpfastcacheDriverConnectException |
56
|
|
|
* @throws PhpfastcacheIOException |
57
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
58
|
|
|
*/ |
59
|
|
|
public function __construct(ConfigurationOptionInterface $config, string $instanceId) |
60
|
|
|
{ |
61
|
|
|
$this->setConfig($config); |
62
|
|
|
$this->instanceId = $instanceId; |
63
|
|
|
$this->IO = new DriverIO(); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if (!$this->driverCheck()) { |
66
|
|
|
throw new PhpfastcacheDriverCheckException(\sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$this->driverConnect(); |
71
|
|
|
$config->lock($this); |
|
|
|
|
72
|
|
|
} catch (Throwable $e) { |
73
|
|
|
throw new PhpfastcacheDriverConnectException( |
74
|
|
|
sprintf( |
75
|
|
|
self::DRIVER_CONNECT_FAILURE, |
|
|
|
|
76
|
|
|
$e::class, |
77
|
|
|
$this->getDriverName(), |
78
|
|
|
$e->getMessage(), |
79
|
|
|
$e->getLine() ?: 'unknown line', |
80
|
|
|
$e->getFile() ?: 'unknown file' |
81
|
|
|
), |
82
|
|
|
0, |
83
|
|
|
$e |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getDriverName(): string |
92
|
|
|
{ |
93
|
|
|
if (!isset($this->driverName)) { |
94
|
|
|
$this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
95
|
|
|
} |
96
|
|
|
return $this->driverName; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return ConfigurationOption |
101
|
|
|
*/ |
102
|
|
|
public function getDefaultConfig(): ConfigurationOption |
103
|
|
|
{ |
104
|
|
|
$className = $this::getConfigClass(); |
105
|
|
|
|
106
|
|
|
return new $className(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public static function getConfigClass(): string |
113
|
|
|
{ |
114
|
|
|
$localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
115
|
|
|
if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
116
|
|
|
return $localConfigClass; |
117
|
|
|
} |
118
|
|
|
return ConfigurationOption::class; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function getItemClass(): string |
122
|
|
|
{ |
123
|
|
|
if (!isset(self::$cacheItemClasses[static::class])) { |
124
|
|
|
self::$cacheItemClasses[static::class] = self::getClassNamespace() . '\\' . 'Item'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return self::$cacheItemClasses[static::class]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param ExtendedCacheItemInterface $item |
133
|
|
|
* @param bool $stringifyDate |
134
|
|
|
* @return array |
135
|
|
|
* @throws PhpfastcacheLogicException |
136
|
|
|
*/ |
137
|
|
|
public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyDate = false): array |
138
|
|
|
{ |
139
|
|
|
$wrap = [ |
140
|
|
|
self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key |
|
|
|
|
141
|
|
|
self::DRIVER_DATA_WRAPPER_INDEX => $item->get(), |
|
|
|
|
142
|
|
|
self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
|
|
|
143
|
|
|
self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
|
|
|
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
if ($this->getConfig()->isItemDetailedDate()) { |
147
|
|
|
$wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime(); |
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* If the creation date exists |
150
|
|
|
* reuse it else set a new Date |
151
|
|
|
*/ |
152
|
|
|
$wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate() ?: new DateTime(); |
|
|
|
|
153
|
|
|
} else { |
154
|
|
|
$wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = null; |
155
|
|
|
$wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($stringifyDate) { |
159
|
|
|
$wrap = \array_map(static function ($value) { |
160
|
|
|
if ($value instanceof DateTimeInterface) { |
161
|
|
|
return $value->format(DateTimeInterface::W3C); |
162
|
|
|
} |
163
|
|
|
return $value; |
164
|
|
|
}, $wrap); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $wrap; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return ConfigurationOption |
172
|
|
|
*/ |
173
|
|
|
abstract public function getConfig(): ConfigurationOption; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param ConfigurationOptionInterface $config |
177
|
|
|
* @return static |
178
|
|
|
*/ |
179
|
|
|
public function setConfig(ConfigurationOptionInterface $config): static |
180
|
|
|
{ |
181
|
|
|
$this->config = $config; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $wrapper |
188
|
|
|
* @return mixed |
189
|
|
|
* @throws \Exception |
190
|
|
|
*/ |
191
|
|
|
public function driverUnwrapData(array $wrapper): mixed |
192
|
|
|
{ |
193
|
|
|
return $wrapper[self::DRIVER_DATA_WRAPPER_INDEX]; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array $wrapper |
198
|
|
|
* @return DateTime |
199
|
|
|
*/ |
200
|
|
|
public function driverUnwrapEdate(array $wrapper): \DateTime |
201
|
|
|
{ |
202
|
|
|
if ($wrapper[self::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTime) { |
|
|
|
|
203
|
|
|
return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param array $wrapper |
211
|
|
|
* @return DateTime|null |
212
|
|
|
*/ |
213
|
|
|
public function driverUnwrapCdate(array $wrapper): ?\DateTime |
214
|
|
|
{ |
215
|
|
|
if ($wrapper[self::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTime) { |
|
|
|
|
216
|
|
|
return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param array $wrapper |
224
|
|
|
* @return DateTime|null |
225
|
|
|
*/ |
226
|
|
|
public function driverUnwrapMdate(array $wrapper): ?\DateTime |
227
|
|
|
{ |
228
|
|
|
if ($wrapper[self::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTime) { |
|
|
|
|
229
|
|
|
return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX]; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
public function getInstanceId(): string |
239
|
|
|
{ |
240
|
|
|
return $this->instanceId; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Encode data types such as object/array |
245
|
|
|
* for driver that does not support |
246
|
|
|
* non-scalar value |
247
|
|
|
* @param $data |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
protected function encode($data): string |
251
|
|
|
{ |
252
|
|
|
return \serialize($data); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Decode data types such as object/array |
257
|
|
|
* for driver that does not support |
258
|
|
|
* non-scalar value |
259
|
|
|
* @param string|null $value |
260
|
|
|
* @return mixed |
261
|
|
|
*/ |
262
|
|
|
protected function decode(?string $value): mixed |
263
|
|
|
{ |
264
|
|
|
return \unserialize((string) $value, ['allowed_classes' => true]); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths