no type decls, yet
[mdref/mdref] / mdref / Action.php
index 19cf9106f363c9edb6e89e1b83a9cd6fa6aa5ac0..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
         */
@@ -44,7 +45,7 @@ class Action {
        function esc($txt) {
                return htmlspecialchars($txt);
        }
-       
+
        /**
         * Create the view payload
         * @param \http\Controller $ctl
@@ -52,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"];
 
@@ -62,10 +64,10 @@ class Action {
 
                $pld->refs = $this->reference;
                $pld->baseUrl = $this->baseUrl;
-                       
+
                return $pld;
        }
-       
+
        /**
         * Redirect to canononical url
         * @param string $cnn
@@ -75,7 +77,7 @@ class Action {
                $this->response->setResponseCode(301);
                $this->response->send();
        }
-       
+
        /**
         * Serve index.css
         */
@@ -84,7 +86,7 @@ class Action {
                $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r")));
                $this->response->send();
        }
-       
+
        /**
         * Serve index.js
         */
@@ -93,7 +95,22 @@ class Action {
                $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
@@ -113,6 +130,9 @@ class Action {
                case "index.js":
                        $this->serveJavascript();
                        break;
+               case "stub":
+                       $this->serveStub();
+                       break;
                default:
                        throw new Exception(404, "$pld->ref not found");
                }
@@ -124,7 +144,7 @@ class Action {
                include ROOT."/views/layout.phtml";
                $this->response->send();
        }
-       
+
        public function handle() {
                try {
 
@@ -144,11 +164,11 @@ class Action {
                                        return;
                                }
                        }
-               
+
                } catch (\Exception $e) {
                        $pld->exception = $e;
                }
 
                $this->serve($pld);
        }
-}
\ No newline at end of file
+}