move formatting from JS to PHP
[mdref/mdref] / mdref / Action.php
index 46ab7557e391fd1360a790d6e3c676496ecc0f8b..0c48dd6b403c448d5d6f9538b141e448aadc01d1 100644 (file)
 
 namespace mdref;
 
-use http\Controller\Observer;
+use http\Env\Request;
+use http\Env\Response;
+use http\Message\Body;
+use stdClass;
+use function file_get_contents;
+use function htmlspecialchars;
+use function ob_start;
+use const ROOT;
 
 /**
- * The sole action controller of mdref
+ * Request handler
  */
-class Action extends Observer
-{
-       private function serveReference(\http\Url $url, \http\Controller\Payload $payload) {
-               $finder = new Finder($this->baseUrl, REFS);
-               $path = $finder->find($url);
-               $payload->listing = new RefListing($path, 
-                               $finder->glob($path, "/[_a-zA-Z]*.md"));
-               $payload->title = $payload->listing->getSelf()->formatLink();
-               $payload->refs = $finder;
-               if ($path->isFile()) {
-                       $payload->html = new Markdown($path);
-                       $payload->sublisting = new RefListing($path, 
-                                       $finder->glob($path, "/[_a-z]*.md"));
-                       return true;
+class Action {
+       /**
+        * The reference
+        * @var \mdref\Reference
+        */
+       private $reference;
+
+       /**
+        * @var \http\Env\Request
+        */
+       private $request;
+
+       /**
+        * @var \http\Env\Response
+        */
+       private $response;
+
+       /**
+        * @var resource
+        */
+       private $output;
+
+       /**
+        * @var \http\Url
+        */
+       private $baseUrl;
+
+       /**
+        * Initialize the reference
+        */
+       public function __construct(Reference $ref, Request $req, Response $res, BaseUrl $baseUrl, $output = null) {
+               $this->reference = $ref;
+               $this->request = $req;
+               $this->response = $res;
+               $this->baseUrl = $baseUrl;
+               $this->output = $output;
+               ob_start($res);
+       }
+
+       /**
+        * Shorthand for \htmlspecialchars()
+        * @param $txt string
+        * @return string
+        */
+       function esc(string $txt) : string {
+               return htmlspecialchars($txt);
+       }
+
+       /**
+        * Create the view payload
+        * @return \stdClass
+        */
+       private function createPayload() : object {
+               $pld = new stdClass;
+
+               $pld->esc = "htmlspecialchars";
+               $pld->anchor = [$this->reference, "formatAnchor"];
+               $pld->quick = [$this->reference, "formatString"];
+               $pld->file = [$this->reference, "formatFile"];
+
+               $pld->ref = $this->baseUrl->pathinfo(
+                       $this->baseUrl->mod($this->request->getRequestUrl()));
+
+               $pld->markup = function($page) use($pld) {
+                       return $this->reference->getFormatter()->markup($page, $pld);
+               };
+
+               $pld->refs = $this->reference;
+               $pld->baseUrl = $this->baseUrl;
+
+               return $pld;
+       }
+
+       /**
+        * Redirect to canonical url
+        * @param string $cnn
+        */
+       private function serveCanonical(string $cnn) : void {
+               $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
+               $this->response->setResponseCode(301);
+               if (is_resource($this->output)) {
+                       $this->response->send($this->output);
+               } else {
+                       $this->response->send();
                }
        }
-       
-       private function serveInternal(\http\Url $url, \http\Controller\Payload $payload) {
-               $finder = new Finder($this->baseUrl, ROOT);
-               $path = $finder->find($url, "");
-               if ($path->isFile("")) {
-                       $payload->html = $path->toHtml();
-                       return true;
+
+       /**
+        * Serve index.css
+        */
+       private function serveStylesheet() : void {
+               $this->response->setHeader("Content-Type", "text/css");
+               $this->response->setBody(new Body(\fopen(ROOT."/public/index.css", "r")));
+               if (is_resource($this->output)) {
+                       $this->response->send($this->output);
+               } else {
+                       $this->response->send();
                }
        }
-       
-       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];
+
+       /**
+        * Serve index.js
+        */
+       private function serveJavascript() : void {
+               $this->response->setHeader("Content-Type", "application/javascript");
+               $this->response->setBody(new Body(\fopen(ROOT."/public/index.js", "r")));
+               if (is_resource($this->output)) {
+                       $this->response->send($this->output);
+               } else {
+                       $this->response->send();
+               }
+       }
+
+       /**
+        * Server a PHP stub
+        * @throws Exception
+        *
+        */
+       private function serveStub() : void {
+               $name = $this->request->getQuery("ref", "s");
+               $repo = $this->reference->getRepoForEntry($name);
+               if (!$repo->hasStub($stub)) {
+                       throw new Exception(404, "Stub not found");
                }
-               
-               if (!$inf) {
-                       $inf = new \FINFO(FILEINFO_MIME_TYPE);
+               $this->response->setHeader("Content-Type", "application/x-php");
+               $this->response->setContentDisposition(["attachment" => ["filename" => "$name.stub.php"]]);
+               $this->response->setBody(new Body(\fopen($stub, "r")));
+               if (is_resource($this->output)) {
+                       $this->response->send($this->output);
+               } else {
+                       $this->response->send();
                }
-               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")));
+
+       /**
+        * Serve a preset
+        * @param object $pld
+        * @return true to continue serving the payload
+        * @throws Exception
+        */
+       private function servePreset(object $pld) : bool {
+               switch ($pld->ref) {
+               case "AUTHORS":
+               case "LICENSE":
+               case "VERSION":
+                       $pld->text = file_get_contents(ROOT."/$pld->ref");
                        return true;
+               case "index.css":
+                       $this->serveStylesheet();
+                       break;
+               case "index.js":
+                       $this->serveJavascript();
+                       break;
+               case "stub":
+                       $this->serveStub();
+                       break;
+               default:
+                       throw new Exception(404, "$pld->ref not found");
                }
+               return false;
        }
 
        /**
-        * Implements \SplObserver
-        * @param \SplSubject $ctl
+        * Serve a payload
         */
-       function update(\SplSubject $ctl) {
-               /* @var \http\Controller $ctl */
+       private function serve() : void {
+               extract((array) func_get_arg(0));
+               include ROOT."/views/layout.phtml";
+               $this->response->addHeader("Link", "<" . $this->baseUrl->path . "index.css>; rel=preload; as=style");
+               $this->response->addHeader("Link", "<" . $this->baseUrl->path . "index.js>; rel=preload; as=script");
+               if (isset($exception) && $exception->getCode()) {
+                       $this->response->setResponseCode($exception->getCode());
+               }
+               if (is_resource($this->output)) {
+                       $this->response->send($this->output);
+               } else {
+                       $this->response->send();
+               }
+       }
+
+       /**
+        * Request handler
+        */
+       public function handle() : void {
                try {
-                       $pld = $ctl->getPayload();
-                       $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("\\http\\Controller\\Observer\\View");
-                               return;
-                       }
-                       
-                       /* fallthrough */
-                       if (strcmp($url->path, $this->baseUrl->path)) {
-                               throw new \http\Controller\Exception(404, "Could not find '$url'");
+                       $pld = $this->createPayload();
+
+                       if (isset($pld->ref) && strlen($pld->ref)) {
+                               $cnn = null;
+                               if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) {
+                                       if (isset($cnn) && strlen($cnn)) {
+                                               /* redirect */
+                                               $this->serveCanonical($cnn);
+                                               return;
+                                       } else {
+                                               /* direct match */
+                                               $pld->entry = $repo->getEntry($pld->ref);
+                                       }
+                               } elseif (!$this->servePreset($pld)) {
+                                       return;
+                               }
                        }
+
                } catch (\Exception $e) {
-                       $ctl->getPayload()->exception = $e;
+                       $pld->exception = $e;
                }
+
+               $this->serve($pld);
        }
 }