Skip to content

Commit 4edbd68

Browse files
committed
Fixed #860 // Cache item throw an error on save with DateTimeImmutable date objects
1 parent a7fc02d commit 4edbd68

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

lib/Phpfastcache/Core/Item/CacheItemTrait.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function expiresAt(?\DateTimeInterface $expiration): static
102102
{
103103
if ($expiration instanceof DateTimeInterface) {
104104
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration);
105-
$this->expirationDate = $expiration;
105+
$this->expirationDate = $this->demutateDatetime($expiration);
106106
} else {
107107
throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration));
108108
}
@@ -139,4 +139,11 @@ public function expiresAfter(int|\DateInterval|null $time): static
139139

140140
return $this;
141141
}
142+
143+
protected function demutateDatetime(\DateTimeInterface $dateTime): \DateTimeInterface
144+
{
145+
return $dateTime instanceof \DateTimeImmutable
146+
? \DateTime::createFromImmutable($dateTime)
147+
: $dateTime;
148+
}
142149
}

lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function getCreationDate(): DateTimeInterface
136136
public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface
137137
{
138138
if ($this->driver->getConfig()->isItemDetailedDate()) {
139-
$this->creationDate = $date;
139+
$this->creationDate = $this->demutateDatetime($date);
140140
return $this;
141141
}
142142

@@ -163,7 +163,7 @@ public function getModificationDate(): DateTimeInterface
163163
public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface
164164
{
165165
if ($this->driver->getConfig()->isItemDetailedDate()) {
166-
$this->modificationDate = $date;
166+
$this->modificationDate = $this->demutateDatetime($date);
167167
return $this;
168168
}
169169

lib/Phpfastcache/Core/Pool/DriverBaseTrait.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ public function driverUnwrapData(array $wrapper): mixed
203203

204204
/**
205205
* @param array $wrapper
206-
* @return DateTime
206+
* @return DateTimeInterface
207207
*/
208-
public function driverUnwrapEdate(array $wrapper): \DateTime
208+
public function driverUnwrapEdate(array $wrapper): \DateTimeInterface
209209
{
210-
if ($wrapper[self::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTime) {
210+
if ($wrapper[self::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
211211
return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX];
212212
}
213213

@@ -216,11 +216,11 @@ public function driverUnwrapEdate(array $wrapper): \DateTime
216216

217217
/**
218218
* @param array $wrapper
219-
* @return DateTime|null
219+
* @return DateTimeInterface|null
220220
*/
221-
public function driverUnwrapCdate(array $wrapper): ?\DateTime
221+
public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface
222222
{
223-
if ($wrapper[self::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTime) {
223+
if ($wrapper[self::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
224224
return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX];
225225
}
226226

@@ -229,11 +229,11 @@ public function driverUnwrapCdate(array $wrapper): ?\DateTime
229229

230230
/**
231231
* @param array $wrapper
232-
* @return DateTime|null
232+
* @return DateTimeInterface|null
233233
*/
234-
public function driverUnwrapMdate(array $wrapper): ?\DateTime
234+
public function driverUnwrapMdate(array $wrapper): ?\DateTimeInterface
235235
{
236-
if ($wrapper[self::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTime) {
236+
if ($wrapper[self::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
237237
return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX];
238238
}
239239

tests/issues/Github-860.test.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
8+
use Phpfastcache\CacheManager;
9+
use Phpfastcache\Drivers\Files\Config as FilesConfig;
10+
use Phpfastcache\Tests\Helper\TestHelper;
11+
12+
chdir(__DIR__);
13+
require_once __DIR__ . '/../../vendor/autoload.php';
14+
$testHelper = new TestHelper('Github issue #860 - Cache item throw an error on save with DateTimeImmutable date objects');
15+
16+
$config = new FilesConfig();
17+
$testHelper->preConfigure($config);
18+
$cacheInstance = CacheManager::getInstance('Files', $config);
19+
$cacheInstance->clear();
20+
21+
try {
22+
$key = 'pfc_' . bin2hex(random_bytes(12));
23+
$item = $cacheInstance->getItem($key);
24+
$item->set(random_int(1000, 999999))
25+
->setExpirationDate(new DateTimeImmutable('+1 month'))
26+
->setCreationDate(new DateTimeImmutable())
27+
->setModificationDate(new DateTimeImmutable('+1 week'));
28+
$cacheInstance->save($item);
29+
$cacheInstance->detachAllItems();
30+
$item = $cacheInstance->getItem($key);
31+
$testHelper->assertPass('Github issue #860 have not regressed.');
32+
} catch (\TypeError $e) {
33+
$testHelper->assertFail('Github issue #860 have regressed, exception caught: ' . $e->getMessage());
34+
}
35+

0 commit comments

Comments
 (0)