Merge remote-tracking branch 'refs/remotes/origin/master'
[mdref/mdref] / mdref / Action.php
1 <?php
2
3 namespace mdref;
4
5 use http\Controller\Observer;
6
7 /**
8 * The sole action controller of mdref
9 */
10 class Action extends Observer
11 {
12 private function serveReference(\http\Controller $ctl) {
13 $payload = $ctl->getPayload();
14 $finder = new Finder($this->baseUrl, REFS);
15 $path = $finder->find(new \http\Url($ctl->getRequest()->getRequestUrl()));
16 $payload->listing = new RefListing($path,
17 $finder->glob($path, "/[_a-zA-Z]*.md"));
18 $payload->title = $payload->listing->getSelf()->formatLink();
19 $payload->refs = $finder;
20 if ($path->isFile()) {
21 $payload->html = new Markdown($path);
22 $payload->sublisting = new RefListing($path,
23 $finder->glob($path, "/[_a-z]*.md"));
24 return true;
25 }
26 }
27
28 private function serveInternal(\http\Controller $ctl) {
29 $payload = $ctl->getPayload();
30 $finder = new Finder($this->baseUrl, ROOT);
31 $url = new \http\Url($ctl->getRequest()->getRequestUrl());
32 $path = $finder->find($url, "");
33 if ($path->isFile("")) {
34 $payload->html = $path->toHtml();
35 } else if (strcmp($url, $this->baseUrl)) {
36 throw new \http\Controller\Exception(404, "Could not find '$path'");
37 }
38 }
39
40 /**
41 * Implements \SplObserver
42 * @param \SplSubject $ctl
43 */
44 function update(\SplSubject $ctl) {
45 /* @var \http\Controller $ctl */
46 try {
47 $ctl->getPayload()->baseUrl = $this->baseUrl;
48
49 if (!$this->serveReference($ctl)) {
50 $this->serveInternal($ctl);
51 }
52 } catch (\Exception $e) {
53 $ctl->getPayload()->exception = $e;
54 }
55 }
56 }