stub file generation
[mdref/mdref] / mdref / Action.php
index df974b9f1f1595d9fb48f0ad2822475874ffbb1d..deb62e5a64c1260d59607950e29d0cefc8867ba6 100644 (file)
@@ -4,6 +4,7 @@ namespace mdref;
 
 use http\Env\Request;
 use http\Env\Response;
+use http\Message\Body;
 
 /**
  * Request handler
@@ -95,6 +96,21 @@ class Action {
                $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
@@ -114,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");
                }