support ext-discount and ext-cmark
[mdref/mdref] / mdref / Action.php
index e35b3f5f43bf5f2b9d59141e8cbcb70835cf2011..deb62e5a64c1260d59607950e29d0cefc8867ba6 100644 (file)
@@ -4,6 +4,7 @@ namespace mdref;
 
 use http\Env\Request;
 use http\Env\Response;
+use http\Message\Body;
 
 /**
  * Request handler
@@ -29,7 +30,7 @@ class Action {
         * @var \http\Url
         */
        private $baseUrl;
-       
+
        /**
         * Initialize the reference
         */
@@ -38,12 +39,13 @@ class Action {
                $this->request = $req;
                $this->response = $res;
                $this->baseUrl = $baseUrl;
+               ob_start($res);
        }
 
        function esc($txt) {
                return htmlspecialchars($txt);
        }
-       
+
        /**
         * Create the view payload
         * @param \http\Controller $ctl
@@ -51,8 +53,9 @@ class Action {
         */
        private function createPayload() {
                $pld = new \stdClass;
-               
+
                $pld->esc = "htmlspecialchars";
+               $pld->anchor = [$this->reference, "formatAnchor"];
                $pld->quick = [$this->reference, "formatString"];
                $pld->file = [$this->reference, "formatFile"];
 
@@ -61,10 +64,10 @@ class Action {
 
                $pld->refs = $this->reference;
                $pld->baseUrl = $this->baseUrl;
-                       
+
                return $pld;
        }
-       
+
        /**
         * Redirect to canononical url
         * @param string $cnn
@@ -72,27 +75,46 @@ class Action {
        private function serveCanonical($cnn) {
                $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
                $this->response->setResponseCode(301);
+               $this->response->send();
        }
-       
+
        /**
         * Serve index.css
         */
        private function serveStylesheet() {
                $this->response->setHeader("Content-Type", "text/css");
-               $this->esponse->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r")));
+               $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r")));
+               $this->response->send();
        }
-       
+
        /**
         * Serve index.js
         */
        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();
        }
-       
+
+       /**
+        * Server a PHP stub
+        */
+       private function serveStub() {
+               $name = $this->request->getQuery("ref", "s");
+               $repo = $this->reference->getRepoForEntry($name);
+               if (!$repo->hasStub($stub)) {
+                       throw new Exception(404, "Stub not found");
+               }
+               $this->response->setHeader("Content-Type", "application/x-php");
+               $this->response->setContentDisposition(["attachment" => ["filename" => "$name.stub.php"]]);
+               $this->response->setBody(new Body(fopen($stub, "r")));
+               $this->response->send();
+       }
+
        /**
         * Serve a preset
         * @param \stdClass $pld
+        * @return true to continue serving the payload
         * @throws Exception
         */
        private function servePreset($pld) {
@@ -101,23 +123,28 @@ class Action {
                case "LICENSE":
                case "VERSION":
                        $pld->text = file_get_contents(ROOT."/$pld->ref");
-                       break;
+                       return true;
                case "index.css":
-                       $this->serveStylesheet($ctl);
+                       $this->serveStylesheet();
                        break;
                case "index.js":
-                       $this->serveJavascript($ctl);
+                       $this->serveJavascript();
+                       break;
+               case "stub":
+                       $this->serveStub();
                        break;
                default:
                        throw new Exception(404, "$pld->ref not found");
                }
+               return false;
        }
 
        private function serve() {
                extract((array) func_get_arg(0));
                include ROOT."/views/layout.phtml";
+               $this->response->send();
        }
-       
+
        public function handle() {
                try {
 
@@ -133,15 +160,15 @@ class Action {
                                                /* direct match */
                                                $pld->entry = $repo->getEntry($pld->ref);
                                        }
-                               } else {
-                                       return $this->servePreset($pld);
+                               } elseif (!$this->servePreset($pld)) {
+                                       return;
                                }
                        }
-               
+
                } catch (\Exception $e) {
                        $pld->exception = $e;
                }
 
                $this->serve($pld);
        }
-}
\ No newline at end of file
+}