summaryrefslogtreecommitdiff
path: root/src/Server/RequestHandlers/PhpServer.php
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2023-12-19 01:08:06 +0100
committerDaniel Weipert <git@mail.dweipert.de>2023-12-19 01:08:06 +0100
commit4d45cc64499de2de6f758ff7c6f33d1fdd06f73e (patch)
treef8ce28a67c114633f9ba0789e3a1ea0bb375641e /src/Server/RequestHandlers/PhpServer.php
parent878b3e840ec0a02901c9e3fb33ed0837813aaeac (diff)
add PhpServer
Diffstat (limited to 'src/Server/RequestHandlers/PhpServer.php')
-rw-r--r--src/Server/RequestHandlers/PhpServer.php45
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;
+ }
+}