diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2023-12-19 01:08:06 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2023-12-19 01:08:06 +0100 |
commit | 4d45cc64499de2de6f758ff7c6f33d1fdd06f73e (patch) | |
tree | f8ce28a67c114633f9ba0789e3a1ea0bb375641e /src/Server | |
parent | 878b3e840ec0a02901c9e3fb33ed0837813aaeac (diff) |
add PhpServer
Diffstat (limited to 'src/Server')
-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; + } +} |