15 * @var \mdref\Reference
35 * Initialize the reference
37 public function __construct(Reference
$ref, Request
$req, Response
$res, BaseUrl
$baseUrl) {
38 $this->reference
= $ref;
39 $this->request
= $req;
40 $this->response
= $res;
41 $this->baseUrl
= $baseUrl;
46 return htmlspecialchars($txt);
50 * Create the view payload
51 * @param \http\Controller $ctl
54 private function createPayload() {
57 $pld->esc
= "htmlspecialchars";
58 $pld->anchor
= [$this->reference
, "formatAnchor"];
59 $pld->quick
= [$this->reference
, "formatString"];
60 $pld->file
= [$this->reference
, "formatFile"];
62 $pld->ref
= $this->baseUrl
->pathinfo(
63 $this->baseUrl
->mod($this->request
->getRequestUrl()));
65 $pld->refs
= $this->reference
;
66 $pld->baseUrl
= $this->baseUrl
;
72 * Redirect to canononical url
75 private function serveCanonical($cnn) {
76 $this->response
->setHeader("Location", $this->baseUrl
->mod(["path" => $cnn]));
77 $this->response
->setResponseCode(301);
78 $this->response
->send();
84 private function serveStylesheet() {
85 $this->response
->setHeader("Content-Type", "text/css");
86 $this->response
->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.css", "r")));
87 $this->response
->send();
93 private function serveJavascript() {
94 $this->response
->setHeader("Content-Type", "application/javascript");
95 $this->response
->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.js", "r")));
96 $this->response
->send();
102 private function serveStub() {
103 $name = $this->request
->getQuery("ref", "s");
104 $repo = $this->reference
->getRepoForEntry($name);
105 if (!$repo->hasStub($stub)) {
106 throw new Exception(404, "Stub not found");
108 $this->response
->setHeader("Content-Type", "application/x-php");
109 $this->response
->setContentDisposition(["attachment" => ["filename" => "$name.stub.php"]]);
110 $this->response
->setBody(new Body(fopen($stub, "r")));
111 $this->response
->send();
116 * @param \stdClass $pld
117 * @return true to continue serving the payload
120 private function servePreset($pld) {
125 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
128 $this->serveStylesheet();
131 $this->serveJavascript();
137 throw new Exception(404, "$pld->ref not found");
142 private function serve() {
143 extract((array) func_get_arg(0));
144 include ROOT
."/views/layout.phtml";
145 $this->response
->send();
148 public function handle() {
151 $pld = $this->createPayload();
153 if (strlen($pld->ref
)) {
155 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
158 return $this->serveCanonical($cnn);
161 $pld->entry
= $repo->getEntry($pld->ref
);
163 } elseif (!$this->servePreset($pld)) {
168 } catch (\Exception
$e) {
169 $pld->exception
= $e;