X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FAction.php;h=05e007fe37b0ea176dda8d9be8b738c2e273294e;hb=904fc60f05894d6207e9ae85abda8c94283f7d28;hp=dcd8fcbed025d24f4725c9b9c0976a2c19d8e2ba;hpb=3d804b7c153990d081d5acfaee7e594873666f13;p=mdref%2Fmdref diff --git a/mdref/Action.php b/mdref/Action.php index dcd8fcb..05e007f 100644 --- a/mdref/Action.php +++ b/mdref/Action.php @@ -2,29 +2,47 @@ namespace mdref; -use http\Controller\Observer; +use http\Env\Request; +use http\Env\Response; /** * Request handler */ -class Action extends Observer { - /** - * Reference paths - * @var string - */ - protected $refpath; - +class Action { /** * The reference * @var \mdref\Reference */ private $reference; + + /** + * @var \http\Request + */ + private $request; + + /** + * @var \http\Response + */ + private $response; + + /** + * @var \http\Url + */ + private $baseUrl; /** * Initialize the reference */ - protected function init() { - $this->reference = new Reference(explode(PATH_SEPARATOR, $this->refpath)); + public function __construct(Reference $ref, Request $req, Response $res, BaseUrl $baseUrl) { + $this->reference = $ref; + $this->request = $req; + $this->response = $res; + $this->baseUrl = $baseUrl; + ob_start($res); + } + + function esc($txt) { + return htmlspecialchars($txt); } /** @@ -32,76 +50,56 @@ class Action extends Observer { * @param \http\Controller $ctl * @return \stdClass */ - private function createPayload(\http\Controller $ctl) { + private function createPayload() { $pld = new \stdClass; - try { - $pld->quick = function($string) { - $md = \MarkdownDocument::createFromString($string); - $md->compile(\MarkdownDocument::AUTOLINK); - return $md->getHtml(); - }; - - $pld->file = function($file) { - $fd = fopen($file, "r"); - $md = \MarkdownDocument::createFromStream($fd); - $md->compile(\MarkdownDocument::AUTOLINK | \MarkdownDocument::TOC); - $html = $md->getHtml(); - fclose($fd); - return $html; - }; - - $pld->ref = implode("/", $this->baseUrl->params( - $this->baseUrl->mod($ctl->getRequest()->getRequestUrl()))); - - $pld->refs = $this->reference; - $pld->baseUrl = $this->baseUrl; + $pld->esc = "htmlspecialchars"; + $pld->quick = [$this->reference, "formatString"]; + $pld->file = [$this->reference, "formatFile"]; + + $pld->ref = $this->baseUrl->pathinfo( + $this->baseUrl->mod($this->request->getRequestUrl())); + + $pld->refs = $this->reference; + $pld->baseUrl = $this->baseUrl; - } catch (\Exception $e) { - $pld->exception = $e; - } - return $pld; } /** * Redirect to canononical url - * @param \http\Controller $ctl * @param string $cnn */ - private function serveCanonical($ctl, $cnn) { - $ctl->detachAll(Observer\View::class); - $ctl->getResponse()->setHeader("Location", $this->baseUrl->mod($cnn)); - $ctl->getResponse()->setResponseCode(301); + private function serveCanonical($cnn) { + $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn])); + $this->response->setResponseCode(301); + $this->response->send(); } /** * Serve index.css - * @param \http\Controller $ctl */ - private function serveStylesheet($ctl) { - $ctl->detachAll(Observer\View::class); - $ctl->getResponse()->setHeader("Content-Type", "text/css"); - $ctl->getResponse()->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r"))); + private function serveStylesheet() { + $this->response->setHeader("Content-Type", "text/css"); + $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r"))); + $this->response->send(); } /** * Serve index.js - * @param \http\Controller $ctl */ - private function serveJavascript($ctl) { - $ctl->detachAll(Observer\View::class); - $ctl->getResponse()->setHeader("Content-Type", "application/javascript"); - $ctl->getResponse()->setBody(new \http\Message\Body(fopen(ROOT."/public/index.js", "r"))); + private function serveJavascript() { + $this->response->setHeader("Content-Type", "application/javascript"); + $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.js", "r"))); + $this->response->send(); } /** * Serve a preset - * @param \http\Controller $ctl * @param \stdClass $pld - * @throws \http\Controller\Exception + * @throws Exception */ - private function servePreset($ctl, $pld) { + private function servePreset($pld) { switch ($pld->ref) { case "AUTHORS": case "LICENSE": @@ -109,44 +107,46 @@ class Action extends Observer { $pld->text = file_get_contents(ROOT."/$pld->ref"); break; case "index.css": - $this->serveStylesheet($ctl); + $this->serveStylesheet(); break; case "index.js": - $this->serveJavascript($ctl); + $this->serveJavascript(); break; default: - throw new \http\Controller\Exception(404, "$pld->ref not found"); + throw new Exception(404, "$pld->ref not found"); } } + + private function serve() { + extract((array) func_get_arg(0)); + include ROOT."/views/layout.phtml"; + $this->response->send(); + } - /** - * Implements Observer - * @param \SplSubject $ctl \http\Controller - */ - public function update(\SplSubject $ctl) { - /* @var http\Controller $ctl */ - $pld = $this->createPayload($ctl); - $ctl[Observer\View::class] = function() use($pld) { - return $pld; - }; - - if (!isset($pld->ref) || !strlen($pld->ref)) { - /* front page */ - return; - } - - $cnn = null; - if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) { - if (strlen($cnn)) { - /* redirect */ - $this->serveCanonical($ctl, $cnn); - } else { - /* direct match */ - $pld->entry = $repo->getEntry($pld->ref); + public function handle() { + try { + + $pld = $this->createPayload(); + + if (strlen($pld->ref)) { + $cnn = null; + if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) { + if (strlen($cnn)) { + /* redirect */ + return $this->serveCanonical($cnn); + } else { + /* direct match */ + $pld->entry = $repo->getEntry($pld->ref); + } + } else { + return $this->servePreset($pld); + } } - } else { - $this->servePreset($ctl, $pld); + + } catch (\Exception $e) { + $pld->exception = $e; } - } + $this->serve($pld); + } } \ No newline at end of file