From fc6b583627d21a26de8268df800fd44fbb9ac30d Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 20 Jul 2015 12:07:57 +0200 Subject: [PATCH] autocracy is obsolete --- VERSION | 2 +- composer.json | 3 - mdref/Action.php | 147 +++++++++++++++++++------------------ mdref/BaseUrl.php | 38 ++++++++++ mdref/Entry.php | 6 +- mdref/Exception.php | 18 +++++ mdref/ExceptionHandler.php | 2 +- public/index.js | 4 +- public/index.php | 30 ++++---- views/index.phtml | 6 +- views/mdref.phtml | 8 +- views/sidebar.phtml | 18 ++--- 12 files changed, 173 insertions(+), 109 deletions(-) create mode 100644 mdref/BaseUrl.php create mode 100644 mdref/Exception.php diff --git a/VERSION b/VERSION index 3eefcb9..cd5ac03 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.0 +2.0 diff --git a/composer.json b/composer.json index e8a1761..726f6d3 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,4 @@ "name": "m6w6/mdref", "description": "Quick Markdown Reference Browser", "license": "BSD-2-Clause", - "require": { - "m6w6/autocracy": "dev-master" - } } diff --git a/mdref/Action.php b/mdref/Action.php index 37b3d51..e35b3f5 100644 --- a/mdref/Action.php +++ b/mdref/Action.php @@ -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 index 0000000..7476a82 --- /dev/null +++ b/mdref/BaseUrl.php @@ -0,0 +1,38 @@ + 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); + } + + +} diff --git a/mdref/Entry.php b/mdref/Entry.php index 8c6673b..db858a9 100644 --- a/mdref/Entry.php +++ b/mdref/Entry.php @@ -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 index 0000000..013d861 --- /dev/null +++ b/mdref/Exception.php @@ -0,0 +1,18 @@ +setResponseCode($this->code); + $res->setBody(new http\Message\Body); + $res->getBody()->append($this->message); + } +} diff --git a/mdref/ExceptionHandler.php b/mdref/ExceptionHandler.php index 13e7f18..aeff40b 100644 --- a/mdref/ExceptionHandler.php +++ b/mdref/ExceptionHandler.php @@ -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); diff --git a/public/index.js b/public/index.js index 27e368a..93128bf 100644 --- a/public/index.js +++ b/public/index.js @@ -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 ""; + return ""; } return ""; } @@ -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 diff --git a/public/index.php b/public/index.php index 6c477ee..853c091 100644 --- a/public/index.php +++ b/public/index.php @@ -1,7 +1,14 @@ 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(); diff --git a/views/index.phtml b/views/index.phtml index 6b4f2b1..acab9e2 100644 --- a/views/index.phtml +++ b/views/index.phtml @@ -3,13 +3,13 @@ -

esc($text) ?>

+

- esc($entry->getTitle()) ?>

+ getTitle()) ?>
getIntro()) ?>
diff --git a/views/mdref.phtml b/views/mdref.phtml index e50a008..a9e34ef 100644 --- a/views/mdref.phtml +++ b/views/mdref.phtml @@ -5,9 +5,9 @@ @@ -18,9 +18,9 @@ diff --git a/views/sidebar.phtml b/views/sidebar.phtml index 4ecce30..35647b8 100644 --- a/views/sidebar.phtml +++ b/views/sidebar.phtml @@ -1,7 +1,7 @@