5 use http\Controller\Observer
;
8 * The sole action controller of mdref
10 class Action
extends Observer
12 private function serveReference(\http\Url
$url, \http\Controller\Payload
$payload) {
13 $finder = new Finder($this->baseUrl
, REFS
);
14 $path = $finder->find($url);
15 $payload->listing
= new RefListing($path,
16 $finder->glob($path, "/[_a-zA-Z]*.md"));
17 $payload->title
= $payload->listing
->getSelf()->formatLink();
18 $payload->refs
= $finder;
19 if ($path->isFile()) {
20 $payload->html
= new Markdown($path);
21 $payload->sublisting
= new RefListing($path,
22 $finder->glob($path, "/[_a-z]*.md"));
27 private function serveInternal(\http\Url
$url, \http\Controller\Payload
$payload) {
28 $finder = new Finder($this->baseUrl
, ROOT
);
29 $path = $finder->find($url, "");
30 if ($path->isFile("")) {
31 $payload->html
= $path->toHtml();
36 private function getType($file) {
38 static $typ = array(".css" => "text/css", ".js" => "applicatin/javascript");
40 $ext = strrchr($file, ".");
41 if (isset($typ[$ext])) {
46 $inf = new \
FINFO(FILEINFO_MIME_TYPE
);
48 return $inf->file($file);
51 private function servePublic(\http\Url
$url, \http\Env\Response
$res) {
52 $finder = new Finder($this->baseUrl
, ROOT
."/public");
53 $path = $finder->find($url, "");
54 if ($path->isFile("")) {
55 $res->setHeader("Content-Type", $this->getType($path->getFullPath("")));
56 $res->setBody(new \http\Message\
Body(fopen($path->getFullPath(""),"r")));
62 * Implements \SplObserver
63 * @param \SplSubject $ctl
65 function update(\SplSubject
$ctl) {
66 /* @var \http\Controller $ctl */
68 $pld = $ctl->getPayload();
69 $pld->baseUrl
= $this->baseUrl
;
70 $url = $this->baseUrl
->mod($ctl->getRequest()->getRequestUrl());
71 $pld->permUrl
= implode("/", $this->baseUrl
->params($url));
73 if ($this->serveReference($url, $pld) ||
$this->serveInternal($url, $pld)) {
75 } elseif ($this->servePublic($url, $ctl->getResponse())) {
76 $ctl->detachAll("\\http\\Controller\\Observer\\View");
81 if (strcmp($url->path
, $this->baseUrl
->path
)) {
82 throw new \http\Controller\
Exception(404, "Could not find '$url'");
84 } catch (\Exception
$e) {
85 $ctl->getPayload()->exception
= $e;