Skip to content

Commit 7ae1246

Browse files
committed
feat: improve default redirect strategy
1 parent 8cb3622 commit 7ae1246

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/Web/Documentation/DocumentationController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ public function redirect(string $path): Redirect
2828
}
2929

3030
#[Get('/documentation')]
31-
public function index(): Redirect
31+
#[Get('/docs')]
32+
#[Get('/{version}')]
33+
public function index(?string $version): Redirect
3234
{
33-
$version = Version::default();
35+
$version = Version::tryFromString($version);
3436

3537
$category = arr(glob(__DIR__ . "/content/{$version->getUrlSegment()}/*", flags: GLOB_ONLYDIR))
3638
->tap(fn (ImmutableArray $files) => $files->isEmpty() ? throw new \RuntimeException('Documentation has not been fetched. Run `tempest docs:pull`.') : null)

src/Web/Documentation/Version.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ enum Version: string
1111
case VERSION_1 = '1.x';
1212
case VERSION_2 = '2.x';
1313

14-
public function isNext(): bool
14+
public static function default(): self
1515
{
16-
return match ($this) {
17-
self::VERSION_2 => true,
18-
default => false,
19-
};
16+
return self::VERSION_1;
2017
}
2118

22-
public function isCurrent(): bool
19+
public function isNext(): bool
2320
{
2421
return match ($this) {
25-
self::VERSION_1 => true,
22+
self::VERSION_2 => true,
2623
default => false,
2724
};
2825
}
@@ -37,28 +34,25 @@ public function getBranch(): string
3734

3835
public function getUrlSegment(): string
3936
{
40-
return match ($this) {
41-
self::VERSION_1 => '1.x',
42-
self::VERSION_2 => '2.x',
43-
};
37+
return $this->value;
4438
}
4539

4640
public function isPrevious(): bool
4741
{
4842
return ! $this->isNext() && ! $this->isCurrent();
4943
}
5044

45+
public function isCurrent(): bool
46+
{
47+
return $this === self::default();
48+
}
49+
5150
public static function tryFromString(?string $case): ?static
5251
{
5352
return match ($case) {
5453
'default', 'current', 'main', null => self::default(),
55-
'next' => self::VERSION_2,
54+
'next' => array_find(self::cases(), fn (self $version) => $version->isNext()),
5655
default => self::tryFrom($case),
5756
};
5857
}
59-
60-
public static function default(): self
61-
{
62-
return self::VERSION_1;
63-
}
6458
}

0 commit comments

Comments
 (0)