05e007fe37b0ea176dda8d9be8b738c2e273294e
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
102 private function servePreset($pld) {
107 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
110 $this->serveStylesheet();
113 $this->serveJavascript();
116 throw new Exception(404, "$pld->ref not found");
120 private function serve() {
121 extract((array) func_get_arg(0));
122 include ROOT
."/views/layout.phtml";
123 $this->response
->send();
126 public function handle() {
129 $pld = $this->createPayload();
131 if (strlen($pld->ref
)) {
133 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
136 return $this->serveCanonical($cnn);
139 $pld->entry
= $repo->getEntry($pld->ref
);
142 return $this->servePreset($pld);
146 } catch (\Exception
$e) {
147 $pld->exception
= $e;