forked from tempestphp/tempest-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapterController.php
More file actions
77 lines (64 loc) · 2.19 KB
/
ChapterController.php
File metadata and controls
77 lines (64 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace App\Web\Documentation;
use Tempest\Http\Response;
use Tempest\Http\Responses\NotFound;
use Tempest\Http\Responses\Redirect;
use Tempest\Router\Get;
use Tempest\Router\StaticPage;
use Tempest\Support\Str\ImmutableString;
use Tempest\View\View;
use function Tempest\Support\arr;
use function Tempest\Support\Str\before_first;
use function Tempest\uri;
final readonly class ChapterController
{
#[Get('/docs/{path:.*}')]
#[Get('/current/{path:.*}')]
#[Get('/main/{path:.*}')]
public function docsRedirect(string $path): Redirect
{
return new Redirect(sprintf('/%s/%s', Version::default()->value, $path));
}
#[Get('/docs')]
#[Get('/documentation')]
#[Get('/main/framework/getting-started')]
public function index(): Redirect
{
$version = Version::default();
$category = arr(glob(__DIR__ . "/content/{$version->value}/*", flags: GLOB_ONLYDIR))
->sort()
->mapFirstTo(ImmutableString::class)
->basename()
->toString();
$slug = arr(glob(__DIR__ . "/content/{$version->value}/{$category}/*.md"))
->map(fn (string $path) => before_first(basename($path), '.'))
->sort()
->mapFirstTo(ImmutableString::class)
->basename()
->toString();
return new Redirect(uri(
[self::class, '__invoke'],
version: $version,
category: $category,
slug: $slug,
));
}
#[StaticPage(DocumentationDataProvider::class)]
#[Get('/{version}/{category}/{slug}')]
public function __invoke(string $version, string $category, string $slug, ChapterRepository $chapterRepository): View|Response
{
if (is_null($version = Version::tryFromString($version))) {
return new NotFound();
}
$currentChapter = $chapterRepository->find($version, $category, $slug);
if (! $currentChapter || $currentChapter->hidden) {
return new NotFound();
}
return new ChapterView(
version: $version,
chapterRepository: $chapterRepository,
currentChapter: $currentChapter,
);
}
}