Skip to content

Commit f1a8095

Browse files
committed
refactor(http): split session configs by manager
1 parent 0306cbd commit f1a8095

18 files changed

+100
-51
lines changed

packages/core/src/AppConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ final class AppConfig
1313
public string $baseUri;
1414

1515
public function __construct(
16+
public ?string $name = null,
17+
1618
?Environment $environment = null,
1719

1820
?string $baseUri = null,
@@ -26,7 +28,6 @@ public function __construct(
2628
public array $insightsProviders = [],
2729
) {
2830
$this->environment = $environment ?? Environment::fromEnv();
29-
3031
$this->baseUri = $baseUri ?? env('BASE_URI') ?? '';
3132
}
3233
}

packages/core/src/Config/app.config.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44

55
use Tempest\Core\AppConfig;
66

7-
return new AppConfig();
7+
use function Tempest\env;
8+
9+
return new AppConfig(
10+
name: env('APPLICATION_NAME'),
11+
baseUri: env('BASE_URI'),
12+
);

packages/http/src/Session/CleanupSessionsCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tempest\Console\ConsoleCommand;
99
use Tempest\Console\Schedule;
1010
use Tempest\Console\Scheduler\Every;
11+
use Tempest\EventBus\EventBus;
1112

1213
use function Tempest\listen;
1314

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Tempest\Http\Session\Config;
4+
5+
use Tempest\Container\Container;
6+
use Tempest\Http\Session\Managers\FileSessionManager;
7+
use Tempest\Http\Session\Resolvers\CookieSessionIdResolver;
8+
use Tempest\Http\Session\SessionConfig;
9+
10+
final class FileSessionConfig implements SessionConfig
11+
{
12+
public function __construct(
13+
/**
14+
* Path to the sessions storage directory, relative to the internal storage.
15+
*/
16+
public string $path,
17+
18+
public int $expirationInSeconds = 60 * 60 * 24 * 30,
19+
20+
public string $idResolverClass = CookieSessionIdResolver::class,
21+
) {}
22+
23+
public function createManager(Container $container): FileSessionManager
24+
{
25+
return $container->get(FileSessionManager::class);
26+
}
27+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Tempest\Http\Session\Config\FileSessionConfig;
4+
5+
return new FileSessionConfig(
6+
path: 'sessions',
7+
);

packages/http/src/Session/Resolvers/CookieSessionIdResolver.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,37 @@
66

77
use Symfony\Component\Uid\Uuid;
88
use Tempest\Clock\Clock;
9+
use Tempest\Core\AppConfig;
910
use Tempest\Http\Cookie\CookieManager;
10-
use Tempest\Http\Session\Session;
1111
use Tempest\Http\Session\SessionConfig;
1212
use Tempest\Http\Session\SessionId;
1313
use Tempest\Http\Session\SessionIdResolver;
1414

15+
use function Tempest\Support\str;
16+
1517
final readonly class CookieSessionIdResolver implements SessionIdResolver
1618
{
1719
public function __construct(
20+
private AppConfig $appConfig,
1821
private CookieManager $cookies,
1922
private SessionConfig $sessionConfig,
2023
private Clock $clock,
2124
) {}
2225

2326
public function resolve(): SessionId
2427
{
25-
$id = $this->cookies->get(Session::ID)->value ?? null;
28+
$sessionId = str($this->appConfig->name ?? 'tempest')
29+
->snake()
30+
->append('_session_id')
31+
->toString();
32+
33+
$id = $this->cookies->get($sessionId)->value ?? null;
2634

2735
if (! $id) {
2836
$id = (string) Uuid::v4();
2937

3038
$this->cookies->set(
31-
key: Session::ID,
39+
key: $sessionId,
3240
value: $id,
3341
expiresAt: $this->clock->now()->plusSeconds($this->sessionConfig->expirationInSeconds),
3442
);

packages/http/src/Session/Resolvers/HeaderSessionIdResolver.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@
55
namespace Tempest\Http\Session\Resolvers;
66

77
use Symfony\Component\Uid\Uuid;
8+
use Tempest\Core\AppConfig;
89
use Tempest\Http\Request;
9-
use Tempest\Http\Session\Session;
1010
use Tempest\Http\Session\SessionId;
1111
use Tempest\Http\Session\SessionIdResolver;
1212

1313
final readonly class HeaderSessionIdResolver implements SessionIdResolver
1414
{
1515
public function __construct(
16+
private AppConfig $appConfig,
1617
private Request $request,
1718
) {}
1819

1920
public function resolve(): SessionId
2021
{
21-
$id = $this->request->headers[Session::ID] ?? null;
22+
$sessionId = str($this->appConfig->name ?? 'tempest')
23+
->snake()
24+
->append('_session_id')
25+
->toString();
2226

23-
return new SessionId($id ?? ((string) Uuid::v4()));
27+
return new SessionId(
28+
id: $this->request->headers[$sessionId] ?? Uuid::v4()->toString(),
29+
);
2430
}
2531
}

packages/http/src/Session/Session.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
final class Session
1212
{
13-
public const string ID = 'tempest_session_id';
14-
1513
public const string VALIDATION_ERRORS = 'validation_errors';
1614

1715
public const string ORIGINAL_VALUES = 'original_values';
Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Tempest\Http\Session;
64

7-
use Tempest\Http\Session\Managers\FileSessionManager;
8-
use Tempest\Http\Session\Resolvers\CookieSessionIdResolver;
5+
use Tempest\Container\Container;
6+
use Tempest\Http\Session\SessionIdResolver;
97

10-
final class SessionConfig
8+
interface SessionConfig
119
{
12-
public function __construct(
13-
/**
14-
* Path to the sessions storage directory, relative to the internal storage.
15-
*/
16-
public string $path = 'sessions',
17-
18-
/**
19-
* Time required for a session to expire. Defaults to one month.
20-
*/
21-
public int $expirationInSeconds = 60 * 60 * 24 * 30,
10+
/**
11+
* Time required for a session to expire. Defaults to one month.
12+
*/
13+
public int $expirationInSeconds {
14+
get;
15+
}
2216

23-
/**
24-
* @template SessionManager of \Tempest\Http\Session\SessionManager
25-
* @var class-string<SessionManager>
26-
*/
27-
public string $managerClass = FileSessionManager::class,
17+
/**
18+
* @template SessionIdResolver of \Tempest\Http\Session\SessionIdResolver
19+
* @var class-string<SessionIdResolver>
20+
*/
21+
public string $idResolverClass {
22+
get;
23+
}
2824

29-
/**
30-
* @template SessionIdResolver of \Tempest\Http\Session\SessionIdResolver
31-
* @var class-string<SessionIdResolver>
32-
*/
33-
public string $idResolverClass = CookieSessionIdResolver::class,
34-
) {}
25+
public function createManager(Container $container): SessionManager;
3526
}

packages/http/src/Session/SessionIdResolverInitializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Tempest\Container\Container;
88
use Tempest\Container\Initializer;
9+
use Tempest\Http\Session\SessionConfig;
910

1011
final readonly class SessionIdResolverInitializer implements Initializer
1112
{

0 commit comments

Comments
 (0)