Skip to content

Commit e923aab

Browse files
committed
refactor: integrate Tempest DateTime in the other components
1 parent d71164a commit e923aab

File tree

16 files changed

+38
-43
lines changed

16 files changed

+38
-43
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"Tempest\\Vite\\": "src/Tempest/Vite/src"
120120
},
121121
"files": [
122+
"src/Tempest/Clock/src/functions.php",
122123
"src/Tempest/CommandBus/src/functions.php",
123124
"src/Tempest/Container/src/functions.php",
124125
"src/Tempest/Core/src/functions.php",

src/Tempest/Cache/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"psr/cache": "^3.0",
77
"symfony/cache": "^7.2",
88
"tempest/core": "dev-main",
9+
"tempest/clock": "dev-main",
910
"tempest/container": "dev-main",
1011
"tempest/highlight": "^2.11.2"
1112
},

src/Tempest/Cache/src/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Tempest\Cache;
66

77
use Closure;
8-
use DateTimeInterface;
98
use Psr\Cache\CacheItemInterface;
9+
use Tempest\DateTime\DateTimeInterface;
1010

1111
interface Cache
1212
{

src/Tempest/Cache/src/IsCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace Tempest\Cache;
66

77
use Closure;
8-
use DateTimeInterface;
98
use Psr\Cache\CacheItemInterface;
109
use Psr\Cache\CacheItemPoolInterface;
10+
use Tempest\DateTime\DateTimeInterface;
1111

1212
trait IsCache
1313
{
@@ -20,7 +20,7 @@ public function put(string $key, mixed $value, ?DateTimeInterface $expiresAt = n
2020
->set($value);
2121

2222
if ($expiresAt !== null) {
23-
$item = $item->expiresAt($expiresAt);
23+
$item = $item->expiresAt($expiresAt->toNativeDateTime());
2424
}
2525

2626
if ($this->isEnabled()) {

src/Tempest/Clock/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"require": {
55
"php": "^8.4",
66
"psr/clock": "^1.0.0",
7-
"tempest/highlight": "^2.11.2"
7+
"tempest/highlight": "^2.11.2",
8+
"tempest/datetime": "dev-main"
89
},
910
"autoload": {
1011
"psr-4": {

src/Tempest/Router/src/Cookie/Cookie.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Tempest\Router\Cookie;
66

7-
use DateTimeImmutable;
87
use Stringable;
8+
use Tempest\DateTime\DateTimeInterface;
99

1010
/**
1111
* @see https://github.com/httpsoft/http-cookie/blob/master/src/Cookie.php
@@ -15,7 +15,7 @@ final class Cookie implements Stringable
1515
public function __construct(
1616
public string $key,
1717
public string $value = '',
18-
public DateTimeImmutable|int|null $expiresAt = null,
18+
public DateTimeInterface|int|null $expiresAt = null,
1919
public ?int $maxAge = null,
2020
public ?string $domain = null,
2121
public ?string $path = null,
@@ -60,8 +60,8 @@ public function __toString(): string
6060

6161
public function getExpiresAtTime(): ?int
6262
{
63-
if ($this->expiresAt instanceof DateTimeImmutable) {
64-
return $this->expiresAt->getTimestamp();
63+
if ($this->expiresAt instanceof DateTimeInterface) {
64+
return $this->expiresAt->getTimestamp()->getSeconds();
6565
}
6666

6767
if (is_int($this->expiresAt)) {

src/Tempest/Router/src/Cookie/CookieManager.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Tempest\Router\Cookie;
66

7-
use DateTimeImmutable;
87
use Tempest\Clock\Clock;
8+
use Tempest\DateTime\DateTimeInterface;
99

1010
final class CookieManager
1111
{
@@ -27,11 +27,8 @@ public function get(string $key): ?Cookie
2727
return $this->cookies[$key] ?? null;
2828
}
2929

30-
public function set(
31-
string $key,
32-
string $value,
33-
DateTimeImmutable|int|null $expiresAt = null,
34-
): Cookie {
30+
public function set(string $key, string $value, DateTimeInterface|int|null $expiresAt = null): Cookie
31+
{
3532
$cookie = $this->get($key) ?? new Cookie(key: $key);
3633

3734
$cookie->value = $value;
@@ -45,7 +42,7 @@ public function set(
4542
public function add(Cookie $cookie): void
4643
{
4744
if ($cookie->expiresAt !== null) {
48-
$maxAge = $cookie->getExpiresAtTime() - $this->clock->time();
45+
$maxAge = $cookie->getExpiresAtTime() - $this->clock->timestamp();
4946

5047
$cookie->maxAge = max($maxAge, 0);
5148
}

src/Tempest/Router/src/Session/Managers/FileSessionManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public function isValid(SessionId $id): bool
6161
return false;
6262
}
6363

64-
$validUntil = $session->createdAt->getTimestamp() + $this->sessionConfig->expirationInSeconds;
65-
66-
return ($validUntil - $this->clock->time()) > 0;
64+
return $this->clock->now()->before(
65+
other: $session->createdAt->plusSeconds($this->sessionConfig->expirationInSeconds),
66+
);
6767
}
6868

6969
private function getPath(SessionId $id): string

src/Tempest/Router/src/Session/Resolvers/CookieSessionIdResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function resolve(): SessionId
3030
$this->cookies->set(
3131
key: Session::ID,
3232
value: $id,
33-
expiresAt: $this->clock->time() + $this->sessionConfig->expirationInSeconds,
33+
expiresAt: $this->clock->now()->plusSeconds($this->sessionConfig->expirationInSeconds),
3434
);
3535
}
3636

src/Tempest/Router/src/Session/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Tempest\Router\Session;
66

7-
use DateTimeImmutable;
7+
use Tempest\DateTime\DateTimeInterface;
88

99
use function Tempest\get;
1010

@@ -22,7 +22,7 @@ final class Session
2222

2323
public function __construct(
2424
public SessionId $id,
25-
public DateTimeImmutable $createdAt,
25+
public DateTimeInterface $createdAt,
2626
/** @var array<array-key, mixed> */
2727
public array $data = [],
2828
) {}

0 commit comments

Comments
 (0)