diff options
Diffstat (limited to 'src/Server/RequestHandlers/PhpServer.php')
-rw-r--r-- | src/Server/RequestHandlers/PhpServer.php | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/Server/RequestHandlers/PhpServer.php b/src/Server/RequestHandlers/PhpServer.php new file mode 100644 index 0000000..5447f72 --- /dev/null +++ b/src/Server/RequestHandlers/PhpServer.php @@ -0,0 +1,45 @@ +<?php + +namespace GeminiFoundation\Server\RequestHandlers; + +use GeminiFoundation\Request; +use GeminiFoundation\Response; +use GeminiFoundation\Status; + +class PhpServer extends DocumentServer +{ + public function __invoke(Response $response, Request $request): Response + { + $requestPath = rtrim(urldecode($request->getPath()), '/'); + $filePath = $this->documentRoot . $requestPath; + + $documentPath = $filePath; + if (! is_file($documentPath)) { + $documentPath = $filePath . '/' . $this->indexFile; + } + + if (is_file($documentPath) && str_ends_with($documentPath, '.php')) { + ob_start(); + try { + include $documentPath; + } catch (\Throwable $exception) { + echo <<<EXCEPTION + + ``` + $exception + ``` + + EXCEPTION; + } + $content = ob_get_clean(); + + $response->setBody($content); + $response->setStatusCode(Status::SUCCESS); + $response->setMeta('text/gemini; charset=utf-8'); + } else { + return parent::__invoke($response, $request); + } + + return $response; + } +} |