autocracy is obsolete
[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 }
42
43 function esc($txt) {
44 return htmlspecialchars($txt);
45 }
46
47 /**
48 * Create the view payload
49 * @param \http\Controller $ctl
50 * @return \stdClass
51 */
52 private function createPayload() {
53 $pld = new \stdClass;
54
55 $pld->esc = "htmlspecialchars";
56 $pld->quick = [$this->reference, "formatString"];
57 $pld->file = [$this->reference, "formatFile"];
58
59 $pld->ref = $this->baseUrl->pathinfo(
60 $this->baseUrl->mod($this->request->getRequestUrl()));
61
62 $pld->refs = $this->reference;
63 $pld->baseUrl = $this->baseUrl;
64
65 return $pld;
66 }
67
68 /**
69 * Redirect to canononical url
70 * @param string $cnn
71 */
72 private function serveCanonical($cnn) {
73 $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
74 $this->response->setResponseCode(301);
75 }
76
77 /**
78 * Serve index.css
79 */
80 private function serveStylesheet() {
81 $this->response->setHeader("Content-Type", "text/css");
82 $this->esponse->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r")));
83 }
84
85 /**
86 * Serve index.js
87 */
88 private function serveJavascript() {
89 $this->response->setHeader("Content-Type", "application/javascript");
90 $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.js", "r")));
91 }
92
93 /**
94 * Serve a preset
95 * @param \stdClass $pld
96 * @throws Exception
97 */
98 private function servePreset($pld) {
99 switch ($pld->ref) {
100 case "AUTHORS":
101 case "LICENSE":
102 case "VERSION":
103 $pld->text = file_get_contents(ROOT."/$pld->ref");
104 break;
105 case "index.css":
106 $this->serveStylesheet($ctl);
107 break;
108 case "index.js":
109 $this->serveJavascript($ctl);
110 break;
111 default:
112 throw new Exception(404, "$pld->ref not found");
113 }
114 }
115
116 private function serve() {
117 extract((array) func_get_arg(0));
118 include ROOT."/views/layout.phtml";
119 }
120
121 public function handle() {
122 try {
123
124 $pld = $this->createPayload();
125
126 if (strlen($pld->ref)) {
127 $cnn = null;
128 if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) {
129 if (strlen($cnn)) {
130 /* redirect */
131 return $this->serveCanonical($cnn);
132 } else {
133 /* direct match */
134 $pld->entry = $repo->getEntry($pld->ref);
135 }
136 } else {
137 return $this->servePreset($pld);
138 }
139 }
140
141 } catch (\Exception $e) {
142 $pld->exception = $e;
143 }
144
145 $this->serve($pld);
146 }
147 }