11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Xoshbin \Pertuk \Http \Controllers ;
46
7+ use Illuminate \Contracts \Filesystem \FileNotFoundException ;
8+ use Illuminate \Contracts \View \View as ViewContract ;
9+ use Illuminate \Http \JsonResponse ;
10+ use Illuminate \Http \RedirectResponse ;
511use Illuminate \Http \Request ;
12+ use Illuminate \Http \Response as HttpResponse ;
613use Illuminate \Routing \Controller ;
714use Illuminate \Support \Facades \App ;
815use Illuminate \Support \Facades \Session ;
1118
1219class DocumentController extends Controller
1320{
14- public function show (Request $ request ): \Illuminate \Http \Response |\Illuminate \Contracts \View \View |\Illuminate \Http \RedirectResponse
21+ /**
22+ * Redirect root to default locale.
23+ */
24+ public function root (): RedirectResponse
25+ {
26+ $ default = config ('pertuk.default_locale ' , 'en ' );
27+ $ prefix = config ('pertuk.route_prefix ' , 'docs ' );
28+
29+ return redirect ()->route ($ prefix .'.show ' , [
30+ 'locale ' => $ default ,
31+ 'slug ' => 'index ' ,
32+ ]);
33+ }
34+
35+ /**
36+ * Show a documentation page.
37+ */
38+ public function show (Request $ request ): HttpResponse |ViewContract |RedirectResponse
1539 {
1640 // Session starting should ideally be in middleware, leaving it for now if middleware is minimal.
1741 if (! Session::isStarted ()) {
@@ -22,8 +46,13 @@ public function show(Request $request): \Illuminate\Http\Response|\Illuminate\Co
2246 $ locale = $ request ->route ('locale ' );
2347 $ slug = $ request ->route ('slug ' ) ?? 'index ' ;
2448
49+ $ locale = is_string ($ locale ) ? $ locale : 'en ' ;
50+ $ slug = is_string ($ slug ) ? $ slug : 'index ' ;
51+ $ version = is_string ($ version ) ? $ version : null ;
52+
2553 // Validation - 404 if locale not supported
26- abort_unless (in_array ($ locale , config ('pertuk.supported_locales ' , ['en ' ])), 404 );
54+ $ supportedLocales = (array ) config ('pertuk.supported_locales ' , ['en ' ]);
55+ abort_unless (in_array ($ locale , $ supportedLocales , true ), 404 );
2756
2857 // Set application locale state
2958 App::setLocale ($ locale );
@@ -34,7 +63,7 @@ public function show(Request $request): \Illuminate\Http\Response|\Illuminate\Co
3463 // Attempt to retrieve the document
3564 try {
3665 $ data = $ docs ->get ($ locale , $ slug );
37- } catch (\ Illuminate \ Contracts \ Filesystem \ FileNotFoundException $ e ) {
66+ } catch (FileNotFoundException ) {
3867 // Fallback: If 'index' is requested but no physical index.md exists,
3968 // we render the index view with a list of all documents.
4069 if ($ slug === 'index ' ) {
@@ -52,15 +81,15 @@ public function show(Request $request): \Illuminate\Http\Response|\Illuminate\Co
5281
5382 return $ this ->responseWithCacheHeaders (
5483 response ()->view ('pertuk::show ' , $ viewData ),
55- $ data ['mtime ' ],
56- $ data ['etag ' ]
84+ ( int ) $ data ['mtime ' ],
85+ ( string ) $ data ['etag ' ]
5786 );
5887 }
5988
6089 /**
61- * Render the index page with grouped documentation items .
90+ * Helper to show the index page when index.md is missing .
6291 */
63- protected function showIndex (DocumentationService $ docs , string $ locale ): \ Illuminate \ Contracts \ View \ View
92+ protected function showIndex (DocumentationService $ docs , string $ locale ): ViewContract
6493 {
6594 $ items = $ docs ->list ($ locale );
6695
@@ -81,7 +110,7 @@ protected function showIndex(DocumentationService $docs, string $locale): \Illum
81110 /**
82111 * Attach HTTP caching headers to the response.
83112 */
84- protected function responseWithCacheHeaders (\ Illuminate \ Http \ Response $ response , int $ mtime , string $ etag ): \ Illuminate \ Http \ Response
113+ protected function responseWithCacheHeaders (HttpResponse $ response , int $ mtime , string $ etag ): HttpResponse
85114 {
86115 $ lastModified = gmdate ('D, d M Y H:i:s ' , $ mtime ).' GMT ' ;
87116
@@ -102,9 +131,14 @@ protected function responseWithCacheHeaders(\Illuminate\Http\Response $response,
102131 ]);
103132 }
104133
105- public function searchIndex (Request $ request , string $ locale ): \Illuminate \Http \JsonResponse
134+ /**
135+ * Search index endpoint.
136+ */
137+ public function searchIndex (Request $ request , string $ locale ): JsonResponse
106138 {
107139 $ version = $ request ->route ('version ' );
140+ $ version = is_string ($ version ) ? $ version : null ;
141+
108142 $ items = DocumentationService::make ($ version )->buildIndex ($ locale );
109143
110144 return response ()->json ($ items )->header ('Content-Type ' , 'application/json ' );
0 commit comments