5 use http\Controller\Observer
;
8 * The sole action controller of mdref
10 class Action
extends Observer
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"));
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->path
, $this->baseUrl
->path
)) {
36 throw new \http\Controller\
Exception(404, "Could not find '$path'");
41 * Implements \SplObserver
42 * @param \SplSubject $ctl
44 function update(\SplSubject
$ctl) {
45 /* @var \http\Controller $ctl */
47 $ctl->getPayload()->baseUrl
= $this->baseUrl
;
49 if (!$this->serveReference($ctl)) {
50 $this->serveInternal($ctl);
52 } catch (\Exception
$e) {
53 $ctl->getPayload()->exception
= $e;