X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=mdref%2FAction.php;h=0c48dd6b403c448d5d6f9538b141e448aadc01d1;hb=a94ecb1009610a763dd74a7eebc4754b6643d832;hp=deb62e5a64c1260d59607950e29d0cefc8867ba6;hpb=29d603eb211a184106cf3caa224777615bcd61a8;p=mdref%2Fmdref diff --git a/mdref/Action.php b/mdref/Action.php index deb62e5..0c48dd6 100644 --- a/mdref/Action.php +++ b/mdref/Action.php @@ -5,6 +5,11 @@ 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 @@ -17,15 +22,20 @@ class Action { private $reference; /** - * @var \http\Request + * @var \http\Env\Request */ private $request; /** - * @var \http\Response + * @var \http\Env\Response */ private $response; + /** + * @var resource + */ + private $output; + /** * @var \http\Url */ @@ -34,25 +44,30 @@ class Action { /** * Initialize the reference */ - public function __construct(Reference $ref, Request $req, Response $res, BaseUrl $baseUrl) { + 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); } - 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"]; @@ -62,6 +77,10 @@ class Action { $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; @@ -69,37 +88,51 @@ 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(); + if (is_resource($this->output)) { + $this->response->send($this->output); + } else { + $this->response->send(); + } } /** * 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->send(); + $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(); + } } /** * 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->send(); + $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() { + private function serveStub() : void { $name = $this->request->getQuery("ref", "s"); $repo = $this->reference->getRepoForEntry($name); if (!$repo->hasStub($stub)) { @@ -107,17 +140,21 @@ class Action { } $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(); + $this->response->setBody(new Body(\fopen($stub, "r"))); + if (is_resource($this->output)) { + $this->response->send($this->output); + } else { + $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": @@ -139,23 +176,38 @@ class Action { 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->send(); + $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(); + } } - public function handle() { + /** + * Request handler + */ + public function handle() : void { try { - $pld = $this->createPayload(); - if (strlen($pld->ref)) { + if (isset($pld->ref) && strlen($pld->ref)) { $cnn = null; if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) { - if (strlen($cnn)) { + if (isset($cnn) && strlen($cnn)) { /* redirect */ - return $this->serveCanonical($cnn); + $this->serveCanonical($cnn); + return; } else { /* direct match */ $pld->entry = $repo->getEntry($pld->ref);