X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FAction.php;h=d85e300fed5fc36663505d57943372efe4cd0959;hb=6a97dab17a6ee9a5a87ebb396da78fe87847794d;hp=18f60691b21b0d96a1176f43ac760509355b4c70;hpb=d5aa9488b908561542745f7e2a1f6061b63b181a;p=mdref%2Fmdref diff --git a/mdref/Action.php b/mdref/Action.php index 18f6069..d85e300 100644 --- a/mdref/Action.php +++ b/mdref/Action.php @@ -9,12 +9,11 @@ use http\Controller\Observer; */ class Action extends Observer { - private function serveReference(\http\Controller $ctl) { - $payload = $ctl->getPayload(); + private function serveReference(\http\Url $url, \stdClass $payload) { $finder = new Finder($this->baseUrl, REFS); - $path = $finder->find(new \http\Url($ctl->getRequest()->getRequestUrl())); + $path = $finder->find($url); $payload->listing = new RefListing($path, - $finder->glob($path, "/[_a-zA-Z]*.md")); + $finder->glob($path, "/[:_a-zA-Z]*.md")); $payload->title = $payload->listing->getSelf()->formatLink(); $payload->refs = $finder; if ($path->isFile()) { @@ -25,15 +24,37 @@ class Action extends Observer } } - private function serveInternal(\http\Controller $ctl) { - $payload = $ctl->getPayload(); + private function serveInternal(\http\Url $url, \stdClass $payload) { $finder = new Finder($this->baseUrl, ROOT); - $url = new \http\Url($ctl->getRequest()->getRequestUrl()); $path = $finder->find($url, ""); if ($path->isFile("")) { $payload->html = $path->toHtml(); - } else if (strcmp($url->path, $this->baseUrl->path)) { - throw new \http\Controller\Exception(404, "Could not find '$path'"); + return true; + } + } + + private function getType($file) { + static $inf = null; + static $typ = array(".css" => "text/css", ".js" => "applicatin/javascript"); + + $ext = strrchr($file, "."); + if (isset($typ[$ext])) { + return $typ[$ext]; + } + + if (!$inf) { + $inf = new \FINFO(FILEINFO_MIME_TYPE); + } + return $inf->file($file); + } + + private function servePublic(\http\Url $url, \http\Env\Response $res) { + $finder = new Finder($this->baseUrl, ROOT."/public"); + $path = $finder->find($url, ""); + if ($path->isFile("")) { + $res->setHeader("Content-Type", $this->getType($path->getFullPath(""))); + $res->setBody(new \http\Message\Body(fopen($path->getFullPath(""),"r"))); + return true; } } @@ -44,13 +65,29 @@ class Action extends Observer function update(\SplSubject $ctl) { /* @var \http\Controller $ctl */ try { - $ctl->getPayload()->baseUrl = $this->baseUrl; - - if (!$this->serveReference($ctl)) { - $this->serveInternal($ctl); + $pld = new \stdClass; + $ctl[Observer\View::class] = function() use($pld) { + return $pld; + }; + + $pld->baseUrl = $this->baseUrl; + $url = $this->baseUrl->mod($ctl->getRequest()->getRequestUrl()); + $pld->permUrl = implode("/", $this->baseUrl->params($url)); + if ($this->serveReference($url, $pld) || $this->serveInternal($url, $pld)) { + return; + } elseif ($this->servePublic($url, $ctl->getResponse())) { + $ctl->detachAll(Observer\View::class); + return; + } + + /* fallthrough */ + if (strcmp($url->path, $this->baseUrl->path)) { + throw new \http\Controller\Exception(404, "Could not find '$url'"); } - } catch (\Exception $e) { - $ctl->getPayload()->exception = $e; + } catch (\Exception $exception) { + $ctl[Observer\View::class] = function() use($exception) { + return compact("exception"); + }; } } }