+
+ /**
+ * Serve index.js
+ * @param \http\Controller $ctl
+ */
+ private function serveJavascript($ctl) {
+ $ctl->detachAll(Observer\View::class);
+ $ctl->getResponse()->setHeader("Content-Type", "application/javascript");
+ $ctl->getResponse()->setBody(new \http\Message\Body(fopen(ROOT."/public/index.js", "r")));
+ }
+
+ /**
+ * Serve a preset
+ * @param \http\Controller $ctl
+ * @param \stdClass $pld
+ * @throws \http\Controller\Exception
+ */
+ private function servePreset($ctl, $pld) {
+ switch ($pld->ref) {
+ case "AUTHORS":
+ case "LICENSE":
+ case "VERSION":
+ $pld->text = file_get_contents(ROOT."/$pld->ref");
+ break;
+ case "index.css":
+ $this->serveStylesheet($ctl);
+ break;
+ case "index.js":
+ $this->serveJavascript($ctl);
+ break;
+ default:
+ throw new \http\Controller\Exception(404, "$pld->ref not found");
+ }
+ }
+
+ /**
+ * Implements Observer
+ * @param \SplSubject $ctl \http\Controller
+ */
+ public function update(\SplSubject $ctl) {
+ /* @var http\Controller $ctl */
+ $pld = $this->createPayload($ctl);
+ $ctl[Observer\View::class] = function() use($pld) {
+ return $pld;
+ };
+
+ if (!isset($pld->ref) || !strlen($pld->ref)) {
+ /* front page */
+ return;
+ }
+
+ $cnn = null;
+ if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) {
+ if (strlen($cnn)) {
+ /* redirect */
+ $this->serveCanonical($ctl, $cnn);
+ } else {
+ /* direct match */
+ $pld->entry = $repo->getEntry($pld->ref);
+ }
+ } else {
+ $this->servePreset($ctl, $pld);
+ }
+ }
+
+}
\ No newline at end of file