Skip to content

Commit f5f6095

Browse files
committed
Updated core to better comply with a PSR-6 specification
1 parent 93d1ec8 commit f5f6095

11 files changed

Lines changed: 68 additions & 70 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
##### 1 october 2021
33
- __Migration guide__
44
- Read the [migration guide](./docs/migration/MigratingFromV8ToV9.md) to upgrade from V8 to V9
5+
- __PSR-6__
6+
- Upgraded `psr/cache` dependency to `^3.0` (for PHP-8 types)
7+
- `\Psr\Cache\CacheItemInterface::get()` slightly changed to fully comply with missing PSR-6 specification: If the cache item is **NOT** hit, this method will return `NULL`.
8+
- __PSR-16__
9+
- _To be written when the PSR-16 will be upgraded for PHP-8_
510
- __API__
611
- Upgraded Phpfastcache API `4.0.0` ([see changes](CHANGELOG_API.md))
712
- Renamed `Api::getPhpFastCacheVersion()` to `Api::getPhpfastcacheVersion()`
@@ -21,7 +26,6 @@
2126
- Typed every class properties of the library
2227
- Migrated many Closure to arrow functions
2328
- Updated parameters & return type hint to use benefit of covariance and contravariance
24-
- Upgraded `psr/cache` dependency to `^3.0`
2529
- Removed embedded Autoload, Phpfastcache is now only Composer-compatible.
2630
- Removed embedded dependencies (`psr/cache`, `psr/simple-cache`)
2731
- __Helpers__
@@ -38,6 +42,8 @@
3842
- Updated tests to work with new core/drivers changes
3943
- Removed Autoload test since its support has been removed and now only managed by Composer
4044
- Increased tests reliability and code coverage for better catching any eventual regression
45+
- __Item__
46+
- `\Psr\Cache\CacheItemInterface::set` will not accept `\Closure` object anymore as method unique parameter
4147
- __Drivers__
4248
- Added `Arangodb` driver support
4349
- Added `Dynamodb` driver support

lib/Phpfastcache/Api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public static function getPhpfastcacheVersion(bool $fallbackOnChangelog = true,
9393
$semverRegexp = '/^([\d]+)\.([\d]+)\.([\d]+)(?:-([\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*))?(?:\+[\dA-Za-z-]+)?$/';
9494
$changelog = \explode("\n", self::getPhpfastcacheChangelog());
9595
foreach ($changelog as $line) {
96-
$trimmedLine = trim($line, " \t\n\r\0\x0B#");
97-
if (str_starts_with($line, '#') && \preg_match($semverRegexp, $trimmedLine)) {
96+
$trimmedLine = \trim($line, " \t\n\r\0\x0B#");
97+
if (\str_starts_with($line, '#') && \preg_match($semverRegexp, $trimmedLine)) {
9898
return $trimmedLine;
9999
}
100100
}

lib/Phpfastcache/Config/ConfigurationOption.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(array $parameters = [])
6464
'Unknown configuration option name "%s" for the config class "%s". Allowed configurations options are "%s"',
6565
$configKey,
6666
$this::class,
67-
\implode('", "', array_keys($this->toArray())),
67+
\implode('", "', \array_keys($this->toArray())),
6868
)
6969
);
7070
}
@@ -81,7 +81,7 @@ public function __construct(array $parameters = [])
8181

8282
public function toArray(): array
8383
{
84-
return get_object_vars($this);
84+
return \get_object_vars($this);
8585
}
8686

8787
/**
@@ -90,7 +90,7 @@ public function toArray(): array
9090
*/
9191
public function isValidOption(string $optionName): bool
9292
{
93-
return property_exists($this, $optionName);
93+
return \property_exists($this, $optionName);
9494
}
9595

9696
/**
@@ -285,6 +285,7 @@ public function setUseStaticItemCaching(bool $useStaticItemCaching): static
285285
/**
286286
* @return object
287287
* @throws PhpfastcacheInvalidArgumentException
288+
* @throws PhpfastcacheLogicException
288289
*/
289290
public function getSuperGlobalAccessor(): object
290291
{

lib/Phpfastcache/Core/Item/CacheItemTrait.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ trait CacheItemTrait
2828
use EventManagerDispatcherTrait;
2929
use ClassNamespaceResolverTrait;
3030

31-
protected bool $fetched = false;
32-
3331
protected string $key;
3432

3533
protected mixed $data;
@@ -49,11 +47,21 @@ public function getKey(): string
4947

5048
public function get(): mixed
5149
{
50+
if (!$this->isHit()) {
51+
return null;
52+
}
53+
5254
return $this->data;
5355
}
5456

57+
/**
58+
* @throws PhpfastcacheInvalidArgumentException
59+
*/
5560
public function set(mixed $value): static
5661
{
62+
if ($value instanceof \Closure) {
63+
throw new PhpfastcacheInvalidArgumentException('The value set cannot be an instance of \\Closure.');
64+
}
5765

5866
/**
5967
* @eventName CacheSaveDeferredItem
@@ -63,12 +71,6 @@ public function set(mixed $value): static
6371
*/
6472
$this->eventManager->dispatch('CacheItemSet', $this, new EventReferenceParameter($value, true));
6573

66-
/**
67-
* The user set a value,
68-
* therefore there is no need to
69-
* fetch from source anymore
70-
*/
71-
$this->fetched = true;
7274
$this->data = $value;
7375

7476
return $this;

lib/Phpfastcache/Core/Item/ExtendedCacheItemInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ interface ExtendedCacheItemInterface extends
4242
*/
4343
public function getEncodedKey(): string;
4444

45+
/**
46+
* Returns the raw value, regardless of hit status.
47+
*
48+
* Although not part of the CacheItemInterface, this method is used by
49+
* the pool for extracting information for saving.
50+
*
51+
* @return mixed
52+
*
53+
* @internal
54+
*/
55+
public function getRawValue(): mixed;
56+
4557
/**
4658
* @return DateTimeInterface
4759
*/

lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCache
7272
}
7373

7474
/**
75-
* @return string
75+
* @inheritDoc
7676
*/
7777
public function getEncodedKey(): string
7878
{
@@ -91,24 +91,23 @@ public function getEncodedKey(): string
9191
}
9292

9393
/**
94-
* @return DateTimeInterface
94+
* @inheritDoc
95+
*/
96+
public function getRawValue(): mixed
97+
{
98+
return $this->data;
99+
}
100+
101+
/**
102+
* @inheritDoc
95103
*/
96104
public function getExpirationDate(): DateTimeInterface
97105
{
98106
return $this->expirationDate;
99107
}
100108

101109
/**
102-
* Alias of expireAt() with forced $expiration param
103-
*
104-
* @param DateTimeInterface $expiration
105-
* The point in time after which the item MUST be considered expired.
106-
* If null is passed explicitly, a default value MAY be used. If none is set,
107-
* the value should be stored permanently or for as long as the
108-
* implementation allows.
109-
*
110-
* @return ExtendedCacheItemInterface
111-
* The called object.
110+
* @inheritDoc
112111
* @throws PhpfastcacheInvalidArgumentException
113112
*/
114113
public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface
@@ -117,7 +116,7 @@ public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheI
117116
}
118117

119118
/**
120-
* @return DateTimeInterface
119+
* @inheritDoc
121120
* @throws PhpfastcacheLogicException
122121
*/
123122
public function getCreationDate(): DateTimeInterface
@@ -130,8 +129,7 @@ public function getCreationDate(): DateTimeInterface
130129
}
131130

132131
/**
133-
* @param DateTimeInterface $date
134-
* @return ExtendedCacheItemInterface
132+
* @inheritDoc
135133
* @throws PhpfastcacheLogicException
136134
*/
137135
public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface
@@ -145,7 +143,7 @@ public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInter
145143
}
146144

147145
/**
148-
* @return DateTimeInterface
146+
* @inheritDoc
149147
* @throws PhpfastcacheLogicException
150148
*/
151149
public function getModificationDate(): DateTimeInterface
@@ -158,8 +156,7 @@ public function getModificationDate(): DateTimeInterface
158156
}
159157

160158
/**
161-
* @param DateTimeInterface $date
162-
* @return ExtendedCacheItemInterface
159+
* @inheritDoc
163160
* @throws PhpfastcacheLogicException
164161
*/
165162
public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface
@@ -219,15 +216,13 @@ public function getLength(): int
219216

220217
public function increment(int $step = 1): ExtendedCacheItemInterface
221218
{
222-
$this->fetched = true;
223219
$this->data += $step;
224220

225221
return $this;
226222
}
227223

228224
public function decrement(int $step = 1): ExtendedCacheItemInterface
229225
{
230-
$this->fetched = true;
231226
$this->data -= $step;
232227

233228
return $this;

lib/Phpfastcache/Core/Pool/DriverBaseTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyD
143143
{
144144
$wrap = [
145145
self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key
146-
self::DRIVER_DATA_WRAPPER_INDEX => $item->get(),
146+
self::DRIVER_DATA_WRAPPER_INDEX => $item->getRawValue(),
147147
self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(),
148148
self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(),
149149
];

lib/Phpfastcache/Drivers/Arangodb/Driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
134134
$document = new ArangoDocument();
135135
$document->setInternalKey($item->getEncodedKey());
136136
$document->set(self::DRIVER_KEY_WRAPPER_INDEX, $item->getKey());
137-
$document->set(self::DRIVER_DATA_WRAPPER_INDEX, $this->encode($item->get()));
137+
$document->set(self::DRIVER_DATA_WRAPPER_INDEX, $this->encode($item->getRawValue()));
138138
$document->set(self::DRIVER_TAGS_WRAPPER_INDEX, $item->getTags());
139139
$document->set(self::DRIVER_EDATE_WRAPPER_INDEX, $item->getExpirationDate());
140140
$document->set(self::TTL_FIELD_NAME, $item->getExpirationDate()->getTimestamp());

lib/Phpfastcache/Drivers/Mongodb/Driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
150150
try {
151151
$set = [
152152
self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(),
153-
self::DRIVER_DATA_WRAPPER_INDEX => new Binary($this->encode($item->get()), Binary::TYPE_GENERIC),
153+
self::DRIVER_DATA_WRAPPER_INDEX => new Binary($this->encode($item->getRawValue()), Binary::TYPE_GENERIC),
154154
self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(),
155155
self::DRIVER_EDATE_WRAPPER_INDEX => new UTCDateTime($item->getExpirationDate()),
156156
];

lib/Phpfastcache/Helper/Psr16Adapter.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class Psr16Adapter implements CacheInterface
4444
* @param $driver
4545
* @param null|ConfigurationOptionInterface $config
4646
* @throws PhpfastcacheDriverCheckException
47-
* @throws PhpfastcacheInvalidArgumentException
4847
* @throws PhpfastcacheLogicException
4948
* @throws PhpfastcacheDriverException
5049
* @throws PhpfastcacheDriverNotFoundException
@@ -217,18 +216,4 @@ public function has($key): bool
217216
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
218217
}
219218
}
220-
221-
/**
222-
* Extra methods that are not part of
223-
* psr16 specifications
224-
*/
225-
226-
/**
227-
* @return ExtendedCacheItemPoolInterface
228-
* @internal
229-
*/
230-
public function getInternalCacheInstance(): ExtendedCacheItemPoolInterface
231-
{
232-
return $this->internalCacheInstance;
233-
}
234219
}

0 commit comments

Comments
 (0)