5 use http\Controller\Observer
;
10 class Action
extends Observer
{
19 * @var \mdref\Reference
24 * Initialize the reference
26 protected function init() {
27 $this->reference
= new Reference(explode(PATH_SEPARATOR
, $this->refpath
));
31 * Create the view payload
32 * @param \http\Controller $ctl
35 private function createPayload(\http\Controller
$ctl) {
39 $pld->quick
= function($string) {
40 $md = \MarkdownDocument
::createFromString($string);
41 $md->compile(\MarkdownDocument
::AUTOLINK
);
42 return $md->getHtml();
45 $pld->file
= function($file) {
46 $fd = fopen($file, "r");
47 $md = \MarkdownDocument
::createFromStream($fd);
48 $md->compile(\MarkdownDocument
::AUTOLINK | \MarkdownDocument
::TOC
);
49 $html = $md->getHtml();
54 $pld->ref
= implode("/", $this->baseUrl
->params(
55 $this->baseUrl
->mod($ctl->getRequest()->getRequestUrl())));
57 $pld->refs
= $this->reference
;
58 $pld->baseUrl
= $this->baseUrl
;
60 } catch (\Exception
$e) {
68 * Redirect to canononical url
69 * @param \http\Controller $ctl
72 private function serveCanonical($ctl, $cnn) {
73 $ctl->detachAll(Observer\View
::class);
74 $ctl->getResponse()->setHeader("Location", $this->baseUrl
->mod($cnn));
75 $ctl->getResponse()->setResponseCode(301);
80 * @param \http\Controller $ctl
82 private function serveStylesheet($ctl) {
83 $ctl->detachAll(Observer\View
::class);
84 $ctl->getResponse()->setHeader("Content-Type", "text/css");
85 $ctl->getResponse()->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.css", "r")));
90 * @param \http\Controller $ctl
92 private function serveJavascript($ctl) {
93 $ctl->detachAll(Observer\View
::class);
94 $ctl->getResponse()->setHeader("Content-Type", "application/javascript");
95 $ctl->getResponse()->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.js", "r")));
100 * @param \http\Controller $ctl
101 * @param \stdClass $pld
102 * @throws \http\Controller\Exception
104 private function servePreset($ctl, $pld) {
109 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
112 $this->serveStylesheet($ctl);
115 $this->serveJavascript($ctl);
118 throw new \http\Controller\
Exception(404, "$pld->ref not found");
123 * Implements Observer
124 * @param \SplSubject $ctl \http\Controller
126 public function update(\SplSubject
$ctl) {
127 /* @var http\Controller $ctl */
128 $pld = $this->createPayload($ctl);
129 $ctl[Observer\View
::class] = function() use($pld) {
133 if (!isset($pld->ref
) ||
!strlen($pld->ref
)) {
139 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
142 $this->serveCanonical($ctl, $cnn);
145 $pld->entry
= $repo->getEntry($pld->ref
);
148 $this->servePreset($ctl, $pld);