diff options
-rw-r--r-- | src/Server/RequestHandlers/PhpServer.php | 45 | ||||
-rw-r--r-- | test/server.php | 1 | ||||
-rw-r--r-- | test/test.php | 4 |
3 files changed, 50 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; + } +} diff --git a/test/server.php b/test/server.php index adc9d05..2268d14 100644 --- a/test/server.php +++ b/test/server.php @@ -11,6 +11,7 @@ $server = new Server( 'key' => __DIR__ . '/key.rsa', 'passphrase' => '', ], + '0.0.0.0' ); $server->onRequest(new DocumentServer( diff --git a/test/test.php b/test/test.php new file mode 100644 index 0000000..d1bfe19 --- /dev/null +++ b/test/test.php @@ -0,0 +1,4 @@ +<?php + +echo date("c"); +echo date(); |