3e318be34d4d45af301d5da2309fa35adcea7d90
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
= [$this->reference
, "formatString"];
40 $pld->file
= [$this->reference
, "formatFile"];
42 $pld->ref
= implode("/", $this->baseUrl
->params(
43 $this->baseUrl
->mod($ctl->getRequest()->getRequestUrl())));
45 $pld->refs
= $this->reference
;
46 $pld->baseUrl
= $this->baseUrl
;
48 } catch (\Exception
$e) {
56 * Redirect to canononical url
57 * @param \http\Controller $ctl
60 private function serveCanonical($ctl, $cnn) {
61 $ctl->detachAll(Observer\View
::class);
62 $ctl->getResponse()->setHeader("Location", $this->baseUrl
->mod($cnn));
63 $ctl->getResponse()->setResponseCode(301);
68 * @param \http\Controller $ctl
70 private function serveStylesheet($ctl) {
71 $ctl->detachAll(Observer\View
::class);
72 $ctl->getResponse()->setHeader("Content-Type", "text/css");
73 $ctl->getResponse()->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.css", "r")));
78 * @param \http\Controller $ctl
80 private function serveJavascript($ctl) {
81 $ctl->detachAll(Observer\View
::class);
82 $ctl->getResponse()->setHeader("Content-Type", "application/javascript");
83 $ctl->getResponse()->setBody(new \http\Message\
Body(fopen(ROOT
."/public/index.js", "r")));
88 * @param \http\Controller $ctl
89 * @param \stdClass $pld
90 * @throws \http\Controller\Exception
92 private function servePreset($ctl, $pld) {
97 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
100 $this->serveStylesheet($ctl);
103 $this->serveJavascript($ctl);
106 throw new \http\Controller\
Exception(404, "$pld->ref not found");
111 * Implements Observer
112 * @param \SplSubject $ctl \http\Controller
114 public function update(\SplSubject
$ctl) {
115 /* @var http\Controller $ctl */
116 $pld = $this->createPayload($ctl);
117 $ctl[Observer\View
::class] = function() use($pld) {
121 if (!isset($pld->ref
) ||
!strlen($pld->ref
)) {
127 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
130 $this->serveCanonical($ctl, $cnn);
133 $pld->entry
= $repo->getEntry($pld->ref
);
136 $this->servePreset($ctl, $pld);