14 * @var \mdref\Reference
34 * Initialize the reference
36 public function __construct(Reference
$ref, Request
$req, Response
$res, BaseUrl
$baseUrl) {
37 $this->reference
= $ref;
38 $this->request
= $req;
39 $this->response
= $res;
40 $this->baseUrl
= $baseUrl;
45 return htmlspecialchars($txt);
49 * Create the view payload
50 * @param \http\Controller $ctl
53 private function createPayload() {
56 $pld->esc
= "htmlspecialchars";
57 $pld->anchor
= [$this->reference
, "formatAnchor"];
58 $pld->quick
= [$this->reference
, "formatString"];
59 $pld->file
= [$this->reference
, "formatFile"];
61 $pld->ref
= $this->baseUrl
->pathinfo(
62 $this->baseUrl
->mod($this->request
->getRequestUrl()));
64 $pld->refs
= $this->reference
;
65 $pld->baseUrl
= $this->baseUrl
;
71 * Redirect to canononical url
74 private function serveCanonical($cnn) {
75 $this->response
->setHeader("Location", $this->baseUrl
->mod(["path" => $cnn]));
76 $this->response
->setResponseCode(301);
77 $this->response
->send();
83 private function serveStylesheet() {
84 $this->response
->setHeader("Content-Type", "text/css");
85 $this->response
->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.css", "r")));
86 $this->response
->send();
92 private function serveJavascript() {
93 $this->response
->setHeader("Content-Type", "application/javascript");
94 $this->response
->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.js", "r")));
95 $this->response
->send();
100 * @param \stdClass $pld
101 * @return true to continue serving the payload
104 private function servePreset($pld) {
109 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
112 $this->serveStylesheet();
115 $this->serveJavascript();
118 throw new Exception(404, "$pld->ref not found");
123 private function serve() {
124 extract((array) func_get_arg(0));
125 include ROOT
."/views/layout.phtml";
126 $this->response
->send();
129 public function handle() {
132 $pld = $this->createPayload();
134 if (strlen($pld->ref
)) {
136 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
139 return $this->serveCanonical($cnn);
142 $pld->entry
= $repo->getEntry($pld->ref
);
144 } elseif (!$this->servePreset($pld)) {
149 } catch (\Exception
$e) {
150 $pld->exception
= $e;