X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FAction.php;h=7e1307bffff70e85412e45a221a1589777cffa35;hb=cc51febb88600aa4fcd4881d880f698b450bb3ff;hp=df974b9f1f1595d9fb48f0ad2822475874ffbb1d;hpb=ba46a6cdffd56d99c191b8fa9a510cf7c6af45bd;p=mdref%2Fmdref diff --git a/mdref/Action.php b/mdref/Action.php index df974b9..7e1307b 100644 --- a/mdref/Action.php +++ b/mdref/Action.php @@ -4,6 +4,12 @@ namespace mdref; 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; /** * Request handler @@ -16,12 +22,12 @@ class Action { private $reference; /** - * @var \http\Request + * @var \http\Env\Request */ private $request; /** - * @var \http\Response + * @var \http\Env\Response */ private $response; @@ -41,17 +47,21 @@ class Action { ob_start($res); } - function esc($txt) { + /** + * Shorthand for \htmlspecialchars() + * @param $txt string + * @return string + */ + function esc(string $txt) : string { return htmlspecialchars($txt); } /** * Create the view payload - * @param \http\Controller $ctl * @return \stdClass */ - private function createPayload() { - $pld = new \stdClass; + private function createPayload() : object { + $pld = new stdClass; $pld->esc = "htmlspecialchars"; $pld->anchor = [$this->reference, "formatAnchor"]; @@ -68,10 +78,10 @@ class Action { } /** - * Redirect to canononical url + * Redirect to canonical url * @param string $cnn */ - private function serveCanonical($cnn) { + private function serveCanonical(string $cnn) : void { $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn])); $this->response->setResponseCode(301); $this->response->send(); @@ -80,28 +90,45 @@ class Action { /** * Serve index.css */ - private function serveStylesheet() { + private function serveStylesheet() : void { $this->response->setHeader("Content-Type", "text/css"); - $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r"))); + $this->response->setBody(new Body(\fopen(ROOT."/public/index.css", "r"))); $this->response->send(); } /** * Serve index.js */ - private function serveJavascript() { + private function serveJavascript() : void { $this->response->setHeader("Content-Type", "application/javascript"); - $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.js", "r"))); + $this->response->setBody(new Body(\fopen(ROOT."/public/index.js", "r"))); + $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"); + } + $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 + * @param object $pld * @return true to continue serving the payload * @throws Exception */ - private function servePreset($pld) { + private function servePreset(object $pld) : bool { switch ($pld->ref) { case "AUTHORS": case "LICENSE": @@ -114,21 +141,31 @@ class Action { case "index.js": $this->serveJavascript(); break; + case "stub": + $this->serveStub(); + break; default: throw new Exception(404, "$pld->ref not found"); } return false; } - private function serve() { + /** + * Serve a payload + */ + 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"); $this->response->send(); } - public function handle() { + /** + * Request handler + */ + public function handle() : void { try { - $pld = $this->createPayload(); if (strlen($pld->ref)) { @@ -136,7 +173,8 @@ class Action { if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) { if (strlen($cnn)) { /* redirect */ - return $this->serveCanonical($cnn); + $this->serveCanonical($cnn); + return; } else { /* direct match */ $pld->entry = $repo->getEntry($pld->ref);