Skip to content

Commit 34170f4

Browse files
committed
wip
1 parent 8f5971d commit 34170f4

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Web/Documentation/ChapterController.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
final readonly class ChapterController
2020
{
21-
#[Get('/docs/{path:.*}')]
2221
#[Get('/current/{path:.*}')]
2322
#[Get('/main/{path:.*}')]
2423
public function docsRedirect(string $path): Redirect
@@ -54,18 +53,34 @@ public function index(): Redirect
5453
));
5554
}
5655

56+
#[StaticPage(DefaultDocumentationDataProvider::class)]
57+
#[Get('/docs/{category}/{slug}')]
58+
public function default(string $category, string $slug, ChapterRepository $chapterRepository): View|Response
59+
{
60+
return $this->chapterView(Version::default(), $category, $slug, $chapterRepository) ?? new NotFound();
61+
}
62+
5763
#[StaticPage(DocumentationDataProvider::class)]
5864
#[Get('/{version}/{category}/{slug}')]
5965
public function __invoke(string $version, string $category, string $slug, ChapterRepository $chapterRepository): View|Response
6066
{
67+
if ($version === Version::default()->value) {
68+
return new Redirect(uri([self::class, 'default'], category: $category, slug: $slug));
69+
}
70+
6171
if (is_null($version = Version::tryFromString($version))) {
6272
return new NotFound();
6373
}
6474

75+
return $this->chapterView($version, $category, $slug, $chapterRepository) ?? new NotFound();
76+
}
77+
78+
private function chapterView(Version $version, string $category, string $slug, ChapterRepository $chapterRepository): ?ChapterView
79+
{
6580
$currentChapter = $chapterRepository->find($version, $category, $slug);
6681

6782
if (! $currentChapter || $currentChapter->hidden) {
68-
return new NotFound();
83+
return null;
6984
}
7085

7186
return new ChapterView(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Web\Documentation;
4+
5+
use Generator;
6+
use Tempest\Router\DataProvider;
7+
8+
final readonly class DefaultDocumentationDataProvider implements DataProvider
9+
{
10+
public function __construct(
11+
private ChapterRepository $chapterRepository,
12+
) {}
13+
14+
#[\Override]
15+
public function provide(): Generator
16+
{
17+
/** @var Chapter $chapter */
18+
foreach ($this->chapterRepository->all(Version::default()) as $chapter) {
19+
yield [
20+
'version' => $chapter->version->value,
21+
'category' => $chapter->category,
22+
'slug' => $chapter->slug,
23+
];
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)