|
9 | 9 | use Tempest\Http\Responses\Redirect; |
10 | 10 | use Tempest\Router\Get; |
11 | 11 | use Tempest\Router\StaticPage; |
| 12 | +use Tempest\Support\Arr\ImmutableArray; |
12 | 13 | use Tempest\Support\Str\ImmutableString; |
13 | 14 | use Tempest\View\View; |
14 | 15 |
|
|
20 | 21 | { |
21 | 22 | #[Get('/current/{path:.*}')] |
22 | 23 | #[Get('/main/{path:.*}')] |
23 | | - public function docsRedirect(string $path): Redirect |
| 24 | + #[Get('/docs/{path:.*}')] |
| 25 | + public function redirect(string $path): Redirect |
24 | 26 | { |
25 | | - return new Redirect(sprintf('/%s/%s', Version::default()->value, $path)); |
| 27 | + return new Redirect(sprintf('/%s/%s', Version::default()->getUrlSegment(), $path)); |
26 | 28 | } |
27 | 29 |
|
28 | | - #[Get('/docs')] |
29 | 30 | #[Get('/documentation')] |
30 | | - #[Get('/main/framework/getting-started')] |
31 | 31 | public function index(): Redirect |
32 | 32 | { |
33 | 33 | $version = Version::default(); |
34 | 34 |
|
35 | | - $category = arr(glob(__DIR__ . "/content/{$version->value}/*", flags: GLOB_ONLYDIR)) |
| 35 | + $category = arr(glob(__DIR__ . "/content/{$version->getUrlSegment()}/*", flags: GLOB_ONLYDIR)) |
| 36 | + ->tap(fn (ImmutableArray $files) => $files->isEmpty() ? throw new \RuntimeException('Documentation has not been fetched. Run `tempest docs:pull`.') : null) |
36 | 37 | ->sort() |
37 | 38 | ->mapFirstTo(ImmutableString::class) |
38 | 39 | ->basename() |
39 | 40 | ->toString(); |
40 | 41 |
|
41 | | - $slug = arr(glob(__DIR__ . "/content/{$version->value}/{$category}/*.md")) |
| 42 | + $slug = arr(glob(__DIR__ . "/content/{$version->getUrlSegment()}/{$category}/*.md")) |
42 | 43 | ->map(fn (string $path) => before_first(basename($path), '.')) |
43 | 44 | ->sort() |
44 | 45 | ->mapFirstTo(ImmutableString::class) |
45 | 46 | ->basename() |
46 | 47 | ->toString(); |
47 | 48 |
|
48 | 49 | return new Redirect(uri( |
49 | | - [self::class, 'default'], |
| 50 | + [self::class, '__invoke'], |
| 51 | + version: $version, |
50 | 52 | category: str_replace('0-', '', $category), |
51 | 53 | slug: str_replace('01-', '', $slug), |
52 | 54 | )); |
53 | 55 | } |
54 | 56 |
|
55 | | - #[StaticPage(DefaultDocumentationDataProvider::class)] |
56 | | - #[Get('/docs/{category}/{slug}')] |
57 | | - public function default(string $category, string $slug, ChapterRepository $chapterRepository): View|Response |
58 | | - { |
59 | | - return $this->chapterView(Version::default(), $category, $slug, $chapterRepository) ?? new NotFound(); |
60 | | - } |
61 | | - |
62 | 57 | #[StaticPage(DocumentationDataProvider::class)] |
63 | 58 | #[Get('/{version}/{category}/{slug}')] |
64 | 59 | public function __invoke(string $version, string $category, string $slug, ChapterRepository $chapterRepository): View|Response |
65 | 60 | { |
66 | | - if ($version === Version::default()->value) { |
67 | | - return new Redirect(uri([self::class, 'default'], category: $category, slug: $slug)); |
68 | | - } |
69 | | - |
70 | 61 | if (is_null($version = Version::tryFromString($version))) { |
71 | 62 | return new NotFound(); |
72 | 63 | } |
73 | 64 |
|
74 | | - return $this->chapterView($version, $category, $slug, $chapterRepository) ?? new NotFound(); |
| 65 | + return $this->chapterView($version, $category, $slug, $chapterRepository) ?? new Redirect(uri(self::class, 'index')); |
75 | 66 | } |
76 | 67 |
|
77 | 68 | private function chapterView(Version $version, string $category, string $slug, ChapterRepository $chapterRepository): ?ChapterView |
|
0 commit comments