X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FAction.php;h=3addfa0527fe4880259d57e3f4ab964ff9c04ae2;hb=081cec2c9cfe879588d5989bcc09a34412f1d9c9;hp=7e1307bffff70e85412e45a221a1589777cffa35;hpb=cc51febb88600aa4fcd4881d880f698b450bb3ff;p=mdref%2Fmdref diff --git a/mdref/Action.php b/mdref/Action.php index 7e1307b..3addfa0 100644 --- a/mdref/Action.php +++ b/mdref/Action.php @@ -31,6 +31,11 @@ class Action { */ private $response; + /** + * @var resource + */ + private $output; + /** * @var \http\Url */ @@ -39,11 +44,12 @@ 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); } @@ -84,7 +90,11 @@ class Action { 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(); + } } /** @@ -93,7 +103,11 @@ class Action { private function serveStylesheet() : void { $this->response->setHeader("Content-Type", "text/css"); $this->response->setBody(new Body(\fopen(ROOT."/public/index.css", "r"))); - $this->response->send(); + if (is_resource($this->output)) { + $this->response->send($this->output); + } else { + $this->response->send(); + } } /** @@ -102,7 +116,11 @@ class Action { private function serveJavascript() : void { $this->response->setHeader("Content-Type", "application/javascript"); $this->response->setBody(new Body(\fopen(ROOT."/public/index.js", "r"))); - $this->response->send(); + if (is_resource($this->output)) { + $this->response->send($this->output); + } else { + $this->response->send(); + } } /** @@ -119,7 +137,11 @@ 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(); + if (is_resource($this->output)) { + $this->response->send($this->output); + } else { + $this->response->send(); + } } /** @@ -158,7 +180,14 @@ class Action { 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(); + if (isset($exception) && $exception->getCode()) { + $this->response->setResponseCode($exception->getCode()); + } + if (is_resource($this->output)) { + $this->response->send($this->output); + } else { + $this->response->send(); + } } /** @@ -168,10 +197,10 @@ class Action { 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 */ $this->serveCanonical($cnn); return;