Skip to content

Commit 9185164

Browse files
committed
feature: use type-safe RouterConfig instead of relying on GT\ConfigSection
closes #71
1 parent 58aa901 commit 9185164

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/BaseRouter.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2121
*/
2222
abstract class BaseRouter {
23+
private ?RouterConfig $config;
2324
private Assembly $viewAssembly;
2425
private Assembly $logicAssembly;
2526
private Container $container;
@@ -28,10 +29,26 @@ abstract class BaseRouter {
2829
private bool $routeCompleted;
2930

3031
public function __construct(
31-
protected ?ConfigSection $routerConfig = null,
32+
null|ConfigSection|RouterConfig $routerConfig = null,
3233
?Assembly $viewAssembly = null,
3334
?Assembly $logicAssembly = null,
3435
) {
36+
if($routerConfig instanceof ConfigSection) {
37+
trigger_deprecation(
38+
"phpgt/routing",
39+
"1.1.4",
40+
"Using ConfigSection type will be removed in a future release - use RouterConfig instead for type safety"
41+
);
42+
43+
$this->config = new RouterConfig(
44+
$routerConfig->getInt("redirect_response_code"),
45+
$routerConfig->getString("default_content_type"),
46+
);
47+
}
48+
else {
49+
$this->config = $routerConfig;
50+
}
51+
3552
$this->viewAssembly = $viewAssembly ?? new Assembly();
3653
$this->logicAssembly = $logicAssembly ?? new Assembly();
3754
$this->routeCompleted = false;
@@ -59,7 +76,8 @@ public function handleRedirects(
5976
}
6077

6178
private function determineResponseClass():string {
62-
$responseCode = $this->routerConfig?->getInt("redirect_response_code");
79+
$responseCode = $this->config->redirectResponseCode;
80+
6381
return match ($responseCode) {
6482
300 => HttpMultipleChoices::class,
6583
301 => HttpMovedPermanently::class,

src/RouterConfig.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Gt\Routing;
3+
4+
class RouterConfig {
5+
public function __construct(
6+
public readonly int $redirectResponseCode,
7+
public readonly string $defaultContentType,
8+
) {}
9+
}

0 commit comments

Comments
 (0)