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->quick
= [$this->reference
, "formatString"];
58 $pld->file
= [$this->reference
, "formatFile"];
60 $pld->ref
= $this->baseUrl
->pathinfo(
61 $this->baseUrl
->mod($this->request
->getRequestUrl()));
63 $pld->refs
= $this->reference
;
64 $pld->baseUrl
= $this->baseUrl
;
70 * Redirect to canononical url
73 private function serveCanonical($cnn) {
74 $this->response
->setHeader("Location", $this->baseUrl
->mod(["path" => $cnn]));
75 $this->response
->setResponseCode(301);
76 $this->response
->send();
82 private function serveStylesheet() {
83 $this->response
->setHeader("Content-Type", "text/css");
84 $this->response
->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.css", "r")));
85 $this->response
->send();
91 private function serveJavascript() {
92 $this->response
->setHeader("Content-Type", "application/javascript");
93 $this->response
->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.js", "r")));
94 $this->response
->send();
99 * @param \stdClass $pld
100 * @return true to continue serving the payload
103 private function servePreset($pld) {
108 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
111 $this->serveStylesheet();
114 $this->serveJavascript();
117 throw new Exception(404, "$pld->ref not found");
122 private function serve() {
123 extract((array) func_get_arg(0));
124 include ROOT
."/views/layout.phtml";
125 $this->response
->send();
128 public function handle() {
131 $pld = $this->createPayload();
133 if (strlen($pld->ref
)) {
135 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
138 return $this->serveCanonical($cnn);
141 $pld->entry
= $repo->getEntry($pld->ref
);
143 } elseif (!$this->servePreset($pld)) {
148 } catch (\Exception
$e) {
149 $pld->exception
= $e;