move formatting from JS to PHP
[mdref/mdref] / mdref / Action.php
index 7e1307bffff70e85412e45a221a1589777cffa35..0c48dd6b403c448d5d6f9538b141e448aadc01d1 100644 (file)
@@ -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);
        }
 
@@ -71,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;
 
@@ -84,7 +94,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 +107,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 +120,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 +141,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 +184,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 +201,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;