permalinks
[mdref/mdref] / mdref / Action.php
1 <?php
2
3 namespace mdref;
4
5 use http\Env\Request;
6 use http\Env\Response;
7
8 /**
9 * Request handler
10 */
11 class Action {
12 /**
13 * The reference
14 * @var \mdref\Reference
15 */
16 private $reference;
17
18 /**
19 * @var \http\Request
20 */
21 private $request;
22
23 /**
24 * @var \http\Response
25 */
26 private $response;
27
28 /**
29 * @var \http\Url
30 */
31 private $baseUrl;
32
33 /**
34 * Initialize the reference
35 */
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;
41 ob_start($res);
42 }
43
44 function esc($txt) {
45 return htmlspecialchars($txt);
46 }
47
48 /**
49 * Create the view payload
50 * @param \http\Controller $ctl
51 * @return \stdClass
52 */
53 private function createPayload() {
54 $pld = new \stdClass;
55
56 $pld->esc = "htmlspecialchars";
57 $pld->anchor = [$this->reference, "formatAnchor"];
58 $pld->quick = [$this->reference, "formatString"];
59 $pld->file = [$this->reference, "formatFile"];
60
61 $pld->ref = $this->baseUrl->pathinfo(
62 $this->baseUrl->mod($this->request->getRequestUrl()));
63
64 $pld->refs = $this->reference;
65 $pld->baseUrl = $this->baseUrl;
66
67 return $pld;
68 }
69
70 /**
71 * Redirect to canononical url
72 * @param string $cnn
73 */
74 private function serveCanonical($cnn) {
75 $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
76 $this->response->setResponseCode(301);
77 $this->response->send();
78 }
79
80 /**
81 * Serve index.css
82 */
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();
87 }
88
89 /**
90 * Serve index.js
91 */
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();
96 }
97
98 /**
99 * Serve a preset
100 * @param \stdClass $pld
101 * @return true to continue serving the payload
102 * @throws Exception
103 */
104 private function servePreset($pld) {
105 switch ($pld->ref) {
106 case "AUTHORS":
107 case "LICENSE":
108 case "VERSION":
109 $pld->text = file_get_contents(ROOT."/$pld->ref");
110 return true;
111 case "index.css":
112 $this->serveStylesheet();
113 break;
114 case "index.js":
115 $this->serveJavascript();
116 break;
117 default:
118 throw new Exception(404, "$pld->ref not found");
119 }
120 return false;
121 }
122
123 private function serve() {
124 extract((array) func_get_arg(0));
125 include ROOT."/views/layout.phtml";
126 $this->response->send();
127 }
128
129 public function handle() {
130 try {
131
132 $pld = $this->createPayload();
133
134 if (strlen($pld->ref)) {
135 $cnn = null;
136 if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) {
137 if (strlen($cnn)) {
138 /* redirect */
139 return $this->serveCanonical($cnn);
140 } else {
141 /* direct match */
142 $pld->entry = $repo->getEntry($pld->ref);
143 }
144 } elseif (!$this->servePreset($pld)) {
145 return;
146 }
147 }
148
149 } catch (\Exception $e) {
150 $pld->exception = $e;
151 }
152
153 $this->serve($pld);
154 }
155 }