05e007fe37b0ea176dda8d9be8b738c2e273294e
[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->quick = [$this->reference, "formatString"];
58 $pld->file = [$this->reference, "formatFile"];
59
60 $pld->ref = $this->baseUrl->pathinfo(
61 $this->baseUrl->mod($this->request->getRequestUrl()));
62
63 $pld->refs = $this->reference;
64 $pld->baseUrl = $this->baseUrl;
65
66 return $pld;
67 }
68
69 /**
70 * Redirect to canononical url
71 * @param string $cnn
72 */
73 private function serveCanonical($cnn) {
74 $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
75 $this->response->setResponseCode(301);
76 $this->response->send();
77 }
78
79 /**
80 * Serve index.css
81 */
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();
86 }
87
88 /**
89 * Serve index.js
90 */
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();
95 }
96
97 /**
98 * Serve a preset
99 * @param \stdClass $pld
100 * @throws Exception
101 */
102 private function servePreset($pld) {
103 switch ($pld->ref) {
104 case "AUTHORS":
105 case "LICENSE":
106 case "VERSION":
107 $pld->text = file_get_contents(ROOT."/$pld->ref");
108 break;
109 case "index.css":
110 $this->serveStylesheet();
111 break;
112 case "index.js":
113 $this->serveJavascript();
114 break;
115 default:
116 throw new Exception(404, "$pld->ref not found");
117 }
118 }
119
120 private function serve() {
121 extract((array) func_get_arg(0));
122 include ROOT."/views/layout.phtml";
123 $this->response->send();
124 }
125
126 public function handle() {
127 try {
128
129 $pld = $this->createPayload();
130
131 if (strlen($pld->ref)) {
132 $cnn = null;
133 if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) {
134 if (strlen($cnn)) {
135 /* redirect */
136 return $this->serveCanonical($cnn);
137 } else {
138 /* direct match */
139 $pld->entry = $repo->getEntry($pld->ref);
140 }
141 } else {
142 return $this->servePreset($pld);
143 }
144 }
145
146 } catch (\Exception $e) {
147 $pld->exception = $e;
148 }
149
150 $this->serve($pld);
151 }
152 }