Skip to content

Commit f1cf5a9

Browse files
committed
refactor: clean up session management
1 parent b41cd98 commit f1cf5a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+959
-1224
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
"lint:fix": "vendor/bin/mago lint --fix --format-after-fix",
251251
"style": "composer fmt && composer lint:fix",
252252
"test": "composer phpunit",
253-
"test:stop": "composer phpunit --stop-on-error --stop-on-failure",
253+
"test:stop": "composer phpunit -- --stop-on-error --stop-on-failure",
254254
"lint": "vendor/bin/mago lint --potentially-unsafe --minimum-fail-level=note",
255255
"phpstan": "vendor/bin/phpstan analyse src tests --memory-limit=1G",
256256
"rector": "vendor/bin/rector process --no-ansi",

packages/auth/src/Authentication/AuthenticatorInitializer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
use Tempest\Container\Initializer;
1010
use Tempest\Container\Singleton;
1111
use Tempest\Http\Session\Session;
12+
use Tempest\Http\Session\SessionManager;
1213

1314
final readonly class AuthenticatorInitializer implements Initializer
1415
{
1516
#[Singleton]
1617
public function initialize(Container $container): Authenticator
1718
{
1819
return new SessionAuthenticator(
19-
authConfig: $container->get(AuthConfig::class),
20+
sessionManager: $container->get(SessionManager::class),
2021
session: $container->get(Session::class),
2122
authenticatableResolver: $container->get(AuthenticatableResolver::class),
2223
);

packages/auth/src/Authentication/SessionAuthenticator.php

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

55
namespace Tempest\Auth\Authentication;
66

7-
use Tempest\Auth\AuthConfig;
87
use Tempest\Http\Session\Session;
8+
use Tempest\Http\Session\SessionManager;
99

1010
final readonly class SessionAuthenticator implements Authenticator
1111
{
1212
public const string AUTHENTICATABLE_KEY = '#authenticatable:id';
1313
public const string AUTHENTICATABLE_CLASS = '#authenticatable:class';
1414

1515
public function __construct(
16-
private AuthConfig $authConfig,
16+
private SessionManager $sessionManager,
1717
private Session $session,
1818
private AuthenticatableResolver $authenticatableResolver,
1919
) {}
@@ -34,7 +34,8 @@ public function authenticate(Authenticatable $authenticatable): void
3434
public function deauthenticate(): void
3535
{
3636
$this->session->remove(self::AUTHENTICATABLE_KEY);
37-
$this->session->destroy();
37+
$this->session->remove(self::AUTHENTICATABLE_CLASS);
38+
$this->sessionManager->save($this->session);
3839
}
3940

4041
public function current(): ?Authenticatable

packages/core/src/AppConfig.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@ final class AppConfig
1212

1313
public string $baseUri;
1414

15-
public function __construct(
16-
public ?string $name = null,
15+
public string $name;
1716

18-
?Environment $environment = null,
17+
/** @var array<class-string<\Tempest\Core\InsightsProvider>> */
18+
public array $insightsProviders = [];
1919

20+
public function __construct(
21+
?string $name = null,
22+
?Environment $environment = null,
2023
?string $baseUri = null,
21-
22-
/**
23-
* @var array<class-string<\Tempest\Core\InsightsProvider>>
24-
*/
25-
public array $insightsProviders = [],
2624
) {
25+
$this->name = $name ?: env('NAME') ?? 'tempest';
2726
$this->environment = $environment ?? Environment::fromEnv();
28-
$this->baseUri = $baseUri ?? env('BASE_URI') ?? '';
27+
$this->baseUri = $baseUri ?: env('BASE_URI') ?? '';
2928
}
3029
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
use function Tempest\env;
88

99
return new AppConfig(
10-
name: env('APPLICATION_NAME'),
10+
name: env('APPLICATION_NAME') ?? 'tempest',
1111
baseUri: env('BASE_URI'),
1212
);

packages/core/src/FrameworkKernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public function registerExceptionHandler(): self
248248

249249
$handler = $this->container->get(ExceptionHandler::class);
250250

251+
ini_set('display_errors', 'Off');
251252
set_exception_handler($handler->handle(...));
252253
set_error_handler(function (int $code, string $message, string $filename, int $line) use ($handler): bool {
253254
$handler->handle(new ErrorException(
@@ -258,7 +259,7 @@ public function registerExceptionHandler(): self
258259
));
259260

260261
return true;
261-
}, error_levels: E_ERROR);
262+
}, error_levels: E_ALL);
262263

263264
return $this;
264265
}

packages/http/src/Cookie/Cookie.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function __construct(
2828
public ?int $maxAge = null,
2929
public ?string $domain = null,
3030
public ?string $path = '/',
31-
public bool $secure = false,
31+
public bool $secure = true,
3232
public bool $httpOnly = false,
33-
public ?SameSite $sameSite = null,
33+
public SameSite $sameSite = SameSite::LAX,
3434
) {}
3535

3636
public function withValue(string $value): self

packages/http/src/Header.php

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

55
namespace Tempest\Http;
66

7+
use BackedEnum;
8+
79
final class Header
810
{
911
public function __construct(
@@ -14,6 +16,10 @@ public function __construct(
1416

1517
public function add(mixed $value): void
1618
{
19+
if ($value instanceof BackedEnum) {
20+
$value = $value->value;
21+
}
22+
1723
$this->values[] = $value;
1824
}
1925

packages/http/src/IsResponse.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ trait IsResponse
2323
/** @var \Tempest\Http\Header[] */
2424
private(set) array $headers = [];
2525

26-
public Session $session {
27-
get => get(Session::class);
28-
}
29-
3026
public CookieManager $cookieManager {
3127
get => get(CookieManager::class);
3228
}
@@ -72,27 +68,6 @@ public function removeHeader(string $key): self
7268
return $this;
7369
}
7470

75-
public function addSession(string $name, mixed $value): self
76-
{
77-
$this->session->set($name, $value);
78-
79-
return $this;
80-
}
81-
82-
public function removeSession(string $name): self
83-
{
84-
$this->session->remove($name);
85-
86-
return $this;
87-
}
88-
89-
public function destroySession(): self
90-
{
91-
$this->session->destroy();
92-
93-
return $this;
94-
}
95-
9671
public function addCookie(Cookie $cookie): self
9772
{
9873
$this->cookieManager->add($cookie);
@@ -107,13 +82,6 @@ public function removeCookie(string $key): self
10782
return $this;
10883
}
10984

110-
public function flash(string $key, mixed $value): self
111-
{
112-
$this->session->flash($key, $value);
113-
114-
return $this;
115-
}
116-
11785
public function setContentType(ContentType $contentType): self
11886
{
11987
$this->removeHeader(ContentType::HEADER)

packages/http/src/Response.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ public function addHeader(string $key, string $value): self;
3030

3131
public function removeHeader(string $key): self;
3232

33-
public function addSession(string $name, mixed $value): self;
34-
35-
public function flash(string $key, mixed $value): self;
36-
37-
public function removeSession(string $name): self;
38-
39-
public function destroySession(): self;
40-
4133
public function addCookie(Cookie $cookie): self;
4234

4335
public function removeCookie(string $key): self;

0 commit comments

Comments
 (0)