autocracy is obsolete
authorMichael Wallner <mike@php.net>
Mon, 20 Jul 2015 10:07:57 +0000 (12:07 +0200)
committerMichael Wallner <mike@php.net>
Mon, 20 Jul 2015 10:07:57 +0000 (12:07 +0200)
12 files changed:
VERSION
composer.json
mdref/Action.php
mdref/BaseUrl.php [new file with mode: 0644]
mdref/Entry.php
mdref/Exception.php [new file with mode: 0644]
mdref/ExceptionHandler.php
public/index.js
public/index.php
views/index.phtml
views/mdref.phtml
views/sidebar.phtml

diff --git a/VERSION b/VERSION
index 3eefcb9dd5b38e2c1dc061052455dd97bcd51e6c..cd5ac039d67e0bdadb17976e4ac39f0ffe6bb6e4 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.0.0
+2.0
index e8a17617c254c83bb28d9435424c25838e73f394..726f6d3cf881e05e1c0ef046f1c12ae059adaf44 100644 (file)
@@ -2,7 +2,4 @@
     "name": "m6w6/mdref",
     "description": "Quick Markdown Reference Browser",
     "license": "BSD-2-Clause",
-    "require": {
-        "m6w6/autocracy": "dev-master"
-    }
 }
index 37b3d51321ebab4400b8400420b621f91e07d5ec..e35b3f5f43bf5f2b9d59141e8cbcb70835cf2011 100644 (file)
@@ -2,29 +2,46 @@
 
 namespace mdref;
 
-use http\Controller\Observer;
+use http\Env\Request;
+use http\Env\Response;
 
 /**
  * Request handler
  */
-class Action extends Observer {
-       /**
-        * Reference paths
-        * @var string
-        */
-       protected $refpath;
-       
+class Action {
        /**
         * The reference
         * @var \mdref\Reference
         */
        private $reference;
+
+       /**
+        * @var \http\Request
+        */
+       private $request;
+
+       /**
+        * @var \http\Response
+        */
+       private $response;
+
+       /**
+        * @var \http\Url
+        */
+       private $baseUrl;
        
        /**
         * Initialize the reference
         */
-       protected function init() {
-               $this->reference = new Reference(explode(PATH_SEPARATOR, $this->refpath));
+       public function __construct(Reference $ref, Request $req, Response $res, BaseUrl $baseUrl) {
+               $this->reference = $ref;
+               $this->request = $req;
+               $this->response = $res;
+               $this->baseUrl = $baseUrl;
+       }
+
+       function esc($txt) {
+               return htmlspecialchars($txt);
        }
        
        /**
@@ -32,64 +49,53 @@ class Action extends Observer {
         * @param \http\Controller $ctl
         * @return \stdClass
         */
-       private function createPayload(\http\Controller $ctl) {
+       private function createPayload() {
                $pld = new \stdClass;
                
-               try {
-                       $pld->quick = [$this->reference, "formatString"];
-                       $pld->file = [$this->reference, "formatFile"];
-                       
-                       $pld->ref = implode("/",  $this->baseUrl->params(
-                               $this->baseUrl->mod($ctl->getRequest()->getRequestUrl())));
-                       
-                       $pld->refs = $this->reference;
-                       $pld->baseUrl = $this->baseUrl;
+               $pld->esc = "htmlspecialchars";
+               $pld->quick = [$this->reference, "formatString"];
+               $pld->file = [$this->reference, "formatFile"];
+
+               $pld->ref = $this->baseUrl->pathinfo(
+                       $this->baseUrl->mod($this->request->getRequestUrl()));
+
+               $pld->refs = $this->reference;
+               $pld->baseUrl = $this->baseUrl;
                        
-               } catch (\Exception $e) {
-                       $pld->exception = $e;
-               }
-               
                return $pld;
        }
        
        /**
         * Redirect to canononical url
-        * @param \http\Controller $ctl
         * @param string $cnn
         */
-       private function serveCanonical($ctl, $cnn) {
-               $ctl->detachAll(Observer\View::class);
-               $ctl->getResponse()->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
-               $ctl->getResponse()->setResponseCode(301);
+       private function serveCanonical($cnn) {
+               $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
+               $this->response->setResponseCode(301);
        }
        
        /**
         * Serve index.css
-        * @param \http\Controller $ctl
         */
-       private function serveStylesheet($ctl) {
-               $ctl->detachAll(Observer\View::class);
-               $ctl->getResponse()->setHeader("Content-Type", "text/css");
-               $ctl->getResponse()->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r")));
+       private function serveStylesheet() {
+               $this->response->setHeader("Content-Type", "text/css");
+               $this->esponse->setBody(new \http\Message\Body(fopen(ROOT."/public/index.css", "r")));
        }
        
        /**
         * Serve index.js
-        * @param \http\Controller $ctl
         */
-       private function serveJavascript($ctl) {
-               $ctl->detachAll(Observer\View::class);
-               $ctl->getResponse()->setHeader("Content-Type", "application/javascript");
-               $ctl->getResponse()->setBody(new \http\Message\Body(fopen(ROOT."/public/index.js", "r")));
+       private function serveJavascript() {
+               $this->response->setHeader("Content-Type", "application/javascript");
+               $this->response->setBody(new \http\Message\Body(fopen(ROOT."/public/index.js", "r")));
        }
        
        /**
         * Serve a preset
-        * @param \http\Controller $ctl
         * @param \stdClass $pld
-        * @throws \http\Controller\Exception
+        * @throws Exception
         */
-       private function servePreset($ctl, $pld) {
+       private function servePreset($pld) {
                switch ($pld->ref) {
                case "AUTHORS":
                case "LICENSE":
@@ -103,38 +109,39 @@ class Action extends Observer {
                        $this->serveJavascript($ctl);
                        break;
                default:
-                       throw new \http\Controller\Exception(404, "$pld->ref not found");
+                       throw new Exception(404, "$pld->ref not found");
                }
        }
+
+       private function serve() {
+               extract((array) func_get_arg(0));
+               include ROOT."/views/layout.phtml";
+       }
        
-       /**
-        * Implements Observer
-        * @param \SplSubject $ctl \http\Controller
-        */
-       public function update(\SplSubject $ctl) {
-               /* @var http\Controller $ctl */
-               $pld = $this->createPayload($ctl);
-               $ctl[Observer\View::class] = function() use($pld) {
-                       return $pld;
-               };
-               
-               if (!isset($pld->ref) || !strlen($pld->ref)) {
-                       /* front page */
-                       return;
-               }
-               
-               $cnn = null;
-               if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) {
-                       if (strlen($cnn)) {
-                               /* redirect */
-                               $this->serveCanonical($ctl, $cnn);
-                       } else {
-                               /* direct match */
-                               $pld->entry = $repo->getEntry($pld->ref);
+       public function handle() {
+               try {
+
+                       $pld = $this->createPayload();
+
+                       if (strlen($pld->ref)) {
+                               $cnn = null;
+                               if (($repo = $this->reference->getRepoForEntry($pld->ref, $cnn))) {
+                                       if (strlen($cnn)) {
+                                               /* redirect */
+                                               return $this->serveCanonical($cnn);
+                                       } else {
+                                               /* direct match */
+                                               $pld->entry = $repo->getEntry($pld->ref);
+                                       }
+                               } else {
+                                       return $this->servePreset($pld);
+                               }
                        }
-               } else {
-                       $this->servePreset($ctl, $pld);
+               
+               } catch (\Exception $e) {
+                       $pld->exception = $e;
                }
-       }
 
+               $this->serve($pld);
+       }
 }
\ No newline at end of file
diff --git a/mdref/BaseUrl.php b/mdref/BaseUrl.php
new file mode 100644 (file)
index 0000000..7476a82
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace mdref;
+
+use http\Url;
+
+class BaseUrl extends Url {
+       /**
+        * Create base URL
+        * @param mixed $url
+        */
+       function __construct($url = null) {
+               $self = array(
+                       "scheme" => filter_input(INPUT_SERVER, "HTTPS", FILTER_VALIDATE_BOOLEAN) ? "https":"http",
+                       "path" => dirname(filter_input(INPUT_SERVER, "SCRIPT_NAME"))."/"
+               );
+               parent::__construct($self, $url,
+                               self::JOIN_PATH |
+                               self::SANITIZE_PATH |
+                               self::STRIP_QUERY |
+                               self::STRIP_AUTH |
+                               self::FROM_ENV
+               );
+       }
+       /**
+        * Extract path info
+        *
+        * @param mixed $url full request url
+        * @return string
+        */
+       function pathinfo($url) {
+               $url = new Url($this, $url, Url::FROM_ENV | Url::STRIP_QUERY);
+               $info = substr($url, strlen($this));
+               return urldecode($info);
+       }
+
+
+}
index 8c6673b7202e58668cf4afa559ac1d819160e0b1..db858a9c2d2a207ba694ed8b4a2518c427ff405d 100644 (file)
@@ -153,7 +153,11 @@ class Entry implements \IteratorAggregate {
         * @return \mdref\Entry
         */
        public function getParent() {
-               if ("." !== ($dirn = dirname($this->name))) {
+               switch ($dirn = dirname($this->name)) {
+               case ".":
+               case "/":
+                       break;
+               default:
                        return $this->repo->getEntry($dirn);
                }
        }
diff --git a/mdref/Exception.php b/mdref/Exception.php
new file mode 100644 (file)
index 0000000..013d861
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+namespace mdref;
+
+use http\Response;
+
+class Exception extends \Exception
+{
+       function __construct($code, $message) {
+               parent::__construct($message, $code);
+       }
+
+       function send(Response $res) {
+               $res->setResponseCode($this->code);
+               $res->setBody(new http\Message\Body);
+               $res->getBody()->append($this->message);
+       }
+}
index 13e7f18fbea7bd513878d2d475666cb8e7152fc8..aeff40bd78275707a895de14d4bde164fc78ce7e 100644 (file)
@@ -60,7 +60,7 @@ class ExceptionHandler
         * @return string
         */
        public static function htmlException(\Exception $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) {
-               if ($e instanceof \http\Controller\Exception) {
+               if ($e instanceof Exception) {
                        $code = $e->getCode() ?: 500;
                        foreach ($e->getHeaders() as $key => $val) {
                                HTTP::setResponseHeader($key, $val);
index 27e368a93ee728e229609384620d310d14213435..93128bf2542a5d1817b2111d742302c3bf081c36 100644 (file)
@@ -100,7 +100,7 @@ $(function() {
                        if (-1 !== (j = s.lastIndexOf("\\")) && s.substr(j+1,1) !== "n") {
                                t = s.substring(j+1);
                                if (!mdref.is_constant(t)) {
-                                       return "<a href=\"" + s.replace(/\\/g, "/").replace(/::|$/, "#") + "\">";
+                                       return "<a href=\"" + s.replace(/\\/g, "/").replace(/::/, "#") + "\">";
                                }
                                return "<a href=\"" + s.substring(0,j).replace(/\\/g, "/") + "#" + t + "\">";
                        }
@@ -130,7 +130,7 @@ $(function() {
                        $n.text().split(/([^a-zA-Z0-9_\\\$:]+)/).forEach(function(v) {
                                var t;
                                
-                               if ((t = mdref.type(v, nn))) {
+                               if ((t = mdref.type(v.replace(/:$/, ""), nn))) {
                                        a.push($(t).text(v));
                                } else if (a.length && a[a.length-1].nodeName === "#text") {
                                        /* if we already have a text node and the next is also gonna be a text
index 6c477eeee40c3f17b1b0e15ee3f0a4f678a494b3..853c0916398ec77e7878c34c8ec99ff8d5aaa2e0 100644 (file)
@@ -1,7 +1,14 @@
 <?php
 
+use mdref\Action;
+use mdref\BaseUrl;
+use mdref\Reference;
+use mdref\ExceptionHandler;
+
+use http\Env\Request;
+use http\Env\Response;
+
 define("ROOT", dirname(__DIR__));
-define("REFS", getenv("REFPATH") ?: implode(PATH_SEPARATOR, glob(ROOT."/refs/*")));
 
 #ini_set("open_basedir", ROOT.":".REFS);
 
@@ -9,19 +16,12 @@ $loader = require ROOT . "/vendor/autoload.php";
 /* @var $loader \Composer\Autoload\ClassLoader */
 $loader->add("mdref", ROOT);
 
-use http\Controller;
-use http\Controller\Url;
-use http\Controller\Observer\Layout;
-
-use mdref\ExceptionHandler;
-use mdref\Action;
-
 new ExceptionHandler;
 
-$ctl = new Controller;
-$ctl->setDependency("baseUrl", new Url)
-       ->attach(new Action(["refpath" => REFS]))
-       ->attach(new Layout)
-       ->notify()
-       ->getResponse()
-       ->send();
+$reference = new Reference(($refs = getenv("REFPATH")) ? explode(PATH_SEPARATOR, $refs) : glob(ROOT."/refs/*"));
+$action = new Action($reference, new Request, new Response, new BaseUrl);
+
+ob_start($response);
+$action->handle();
+ob_end_flush();
+$response->send();
index 6b4f2b1dd65fa4f153285ae53c5d88ba79347add..acab9e27e026f215aa44922877d3da0819f7a57f 100644 (file)
@@ -3,13 +3,13 @@
 <?php if (isset($html)) : ?>
        <?= $html ?>
 <?php elseif (isset($text)) : ?>
-       <p style="white-space:pre-wrap"><?= $view->esc($text) ?></p>
+       <p style="white-space:pre-wrap"><?= $esc($text) ?></p>
 <?php elseif (isset($refs)) : ?>
        <?php foreach ($refs as $repo) : /* @var \mdref\Repo $repo */ ?>
                <?php foreach ($repo as $entry) : /* @var \mdref\Entry $entry */ ?>
                <h2>
-                       <a href="<?= $view->esc($entry->getName()) ?>"
-                       ><?= $view->esc($entry->getTitle()) ?></a></h2>
+                       <a href="<?= $esc($entry->getName()) ?>"
+                       ><?= $esc($entry->getTitle()) ?></a></h2>
                <div><?= $quick($entry->getIntro()) ?></div>
                <?php endforeach; ?>
        <?php endforeach; ?>
index e50a008227bee5d9e440f7c70ed96783dc2618da..a9e34ef3a0918c417bc90be91f34604a76d8f637 100644 (file)
@@ -5,9 +5,9 @@
 <ul>
        <?php foreach($entry as $sub) : if (!$sub->isFunction()) continue; ?>
        <li>
-               <h3><a href="<?= $view->esc($sub->getName()) ?>"><?= $view->esc($sub) ?></a></h3>
+               <h3><a href="<?= $esc($sub->getName()) ?>"><?= $esc($sub) ?></a></h3>
                <p><?= $quick($sub->getDescription()) ?></p>
-               <p><?= $view->esc($sub->getTitle()) ?></p>
+               <p><?= $esc($sub->getTitle()) ?></p>
        </li>
        <?php endforeach; ?>
 </ul>
@@ -18,9 +18,9 @@
 <ul>
        <?php foreach ($entry as $sub) : if (!$sub->isNsClass()) continue; ?>
        <li>
-               <h3><a href="<?= $view->esc($sub->getName()) ?>"><?= $view->esc($sub) ?></a></h3>
+               <h3><a href="<?= $esc($sub->getName()) ?>"><?= $esc($sub) ?></a></h3>
                <p><?= $quick($sub->getDescription()) ?></p>
-               <p><?= $view->esc($sub->getTitle()) ?></p>
+               <p><?= $esc($sub->getTitle()) ?></p>
        </li>
        <?php endforeach; ?>
 </ul>
index 4ecce30d2af920594baec58a5a66197fc6189bee..35647b86d81cb67c1c1e31fb13d61a463a22309a 100644 (file)
@@ -1,7 +1,7 @@
 <div class="sidebar">
        <?php 
                if (isset($baseUrl) || !isset($entry)) {
-                       $up = "";
+                       $up = "./";
                } else {
                        $up = str_repeat("../", count($entry->getParents()));
                }
                                <li>
                                        <?php foreach ($entry->getParents() as $parent) if ($parent->isFile()) : ?>
                                        &uarr; 
-                                               <a href="<?= $up.$view->esc($parent->getName()) ?>">
-                                                       <?= $view->esc($entry->getRepo()->getEntry($parent)) ?>
+                                               <a href="<?= $up.$esc($parent->getName()) ?>">
+                                                       <?= $esc($entry->getRepo()->getEntry($parent)) ?>
                                                </a>
                                                <ul>
                                                        <li>
                                        <?php endif; ?>
-                                                       &circlearrowright; <a href="<?= $up.$view->esc($entry->getName()) ?>"
-                                                       ><?= $view->esc($entry) ?></a>
+                                                       &circlearrowright; <a href="<?= $up.$esc($entry->getName()) ?>"
+                                                       ><?= $esc($entry) ?></a>
                                                        <ul>
                                                                <?php foreach ($entry as $sub) : /* @var \mdref\Entry $sub */ ?>
                                                                <li>
-                                                                       &rdsh; <a href="<?= $up.$view->esc($sub->getName()) ?>"
-                                                                       ><?= $view->esc($sub) ?></a>
+                                                                       &rdsh; <a href="<?= $up.$esc($sub->getName()) ?>"
+                                                                       ><?= $esc($sub) ?></a>
                                                                </li>
                                                                <?php endforeach; ?>
                                                        </ul>
@@ -40,8 +40,8 @@
                                <?php foreach ($refs as $repo) : /* @var \mdref\Repo $repo */ ?>
                                        <?php foreach ($repo as $sub) : /* @var \mdref\Entry $entry */ ?>
                                                <li>
-                                                       &rdsh; <a href="<?= $view->esc($sub->getName()) ?>"
-                                                       ><?= $view->esc($sub->getTitle()) ?></a>
+                                                       &rdsh; <a href="<?= $esc($sub->getName()) ?>"
+                                                       ><?= $esc($sub->getTitle()) ?></a>
                                                </li>
                                        <?php endforeach; ?>
                                <?php endforeach; ?>