-class Action extends Observer
-{
- private function serveReference(\http\Controller $ctl) {
- $payload = $ctl->getPayload();
- $finder = new Finder($this->baseUrl, REFS);
- $path = $finder->find(new \http\Url($ctl->getRequest()->getRequestUrl()));
- $payload->listing = new RefListing($path,
- $finder->glob($path, "/[_a-zA-Z]*.md"));
- $payload->title = $payload->listing->getSelf()->formatLink();
- $payload->refs = $finder;
- if ($path->isFile()) {
- $payload->html = new Markdown($path);
- $payload->sublisting = new RefListing($path,
- $finder->glob($path, "/[_a-z]*.md"));
- return true;
+class Action {
+ /**
+ * The reference
+ * @var \mdref\Reference
+ */
+ private $reference;
+
+ /**
+ * @var \http\Env\Request
+ */
+ private $request;
+
+ /**
+ * @var \http\Env\Response
+ */
+ private $response;
+
+ /**
+ * @var \http\Url
+ */
+ private $baseUrl;
+
+ /**
+ * Initialize the reference
+ */
+ public function __construct(Reference $ref, Request $req, Response $res, BaseUrl $baseUrl) {
+ $this->reference = $ref;
+ $this->request = $req;
+ $this->response = $res;
+ $this->baseUrl = $baseUrl;
+ ob_start($res);
+ }
+
+ /**
+ * Shorthand for \htmlspecialchars()
+ * @param $txt string
+ * @return string
+ */
+ function esc(string $txt) : string {
+ return htmlspecialchars($txt);
+ }
+
+ /**
+ * Create the view payload
+ * @return \stdClass
+ */
+ private function createPayload() : object {
+ $pld = new stdClass;
+
+ $pld->esc = "htmlspecialchars";
+ $pld->anchor = [$this->reference, "formatAnchor"];
+ $pld->quick = [$this->reference, "formatString"];
+ $pld->file = [$this->reference, "formatFile"];
+
+ $pld->ref = $this->baseUrl->pathinfo(
+ $this->baseUrl->mod($this->request->getRequestUrl()));
+
+ $pld->refs = $this->reference;
+ $pld->baseUrl = $this->baseUrl;
+
+ return $pld;
+ }
+
+ /**
+ * Redirect to canonical url
+ * @param string $cnn
+ */
+ private function serveCanonical(string $cnn) : void {
+ $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
+ $this->response->setResponseCode(301);
+ $this->response->send();
+ }
+
+ /**
+ * Serve index.css
+ */
+ 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();
+ }
+
+ /**
+ * Serve index.js
+ */
+ 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();
+ }
+
+ /**
+ * 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");