summaryrefslogtreecommitdiff
path: root/src/lib/Controller.class.php
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2011-08-01 01:22:36 -0400
committerLuke Shumaker <LukeShu@sbcglobal.net>2011-08-01 01:22:36 -0400
commit09dfe32eb6b538225686fd6ed0220240010bc574 (patch)
tree29c1afc5e79519ba8689a3d5d170c312d3cf5033 /src/lib/Controller.class.php
initial commit.
Partway through a rewrite. I have some old files I didn't want to entirely delete.
Diffstat (limited to 'src/lib/Controller.class.php')
-rw-r--r--src/lib/Controller.class.php80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/lib/Controller.class.php b/src/lib/Controller.class.php
new file mode 100644
index 0000000..592ea2c
--- /dev/null
+++ b/src/lib/Controller.class.php
@@ -0,0 +1,80 @@
+<?php
+
+class Controller {
+ /**
+ * Find the best view file to include based on file extension and HTTP
+ * 'Accept' headers.
+ */
+ private function _resolveView($view) {
+ require_once('Mime.class.php');
+ require_once('HTTP_Accept.class.php');
+
+ // Make a list of candidate views
+ $glob_string = VIEWPATH.'/pages/'.$view.'.*.php';
+ $files = glob($glob_string);
+
+ // Return false if there were no candidate views.
+ if (count($files) < 1) return false;
+
+ // $prefs is a associative array where the key is the file
+ // extension, and the value is how much we like that extension.
+ // Higher numbers are better.
+ $prefs = array();
+
+ // $accept will tell us how much we like a given mime type,
+ // based on the ACCEPT constant.
+ $accept = new HTTP_Accept(ACCEPT);
+
+ // Loop through the candidate views, and record how much we
+ // like each.
+ foreach ($files as $file) {
+ $ext = preg_replace('@[^.]*\.(.*)\.php$@','$1', $file);
+ $mimes = Mime::ext2mime($ext);
+ foreach ($mimes as $mime) {
+ $quality = $accept->getQuality($mime);
+ if (isset($final[$ext])) {
+ $quality = max($final[$ext], $quality);
+ }
+ $prefs[$ext] = $quality;
+ }
+ }
+
+ // Sort $prefs such that the entry with the highest value will
+ // appear first.
+ arsort($prefs);
+
+ // Return the first entry in $prefs.
+ foreach ($prefs as $ext => $quality) {
+ return VIEWPATH."/pages/$view.$ext.php";
+ }
+ }
+
+ /**
+ * Show a $view, in the most appropriate format (according to file
+ * extension and HTTP Accept header). Pass the array $vars to the view.
+ */
+ protected function showView($view, $vars=null) {
+ global $VARS, $mm;
+ if ($vars===null) { $vars = array(); }
+ $VARS = $vars;
+ $VARS['template'] = $mm->template();
+ include($this->_resolveView($view));
+ unset($VARS);
+ }
+
+ // Here be default handlers ////////////////////////////////////////////
+
+ public function index($routed, $remainder) {
+ header('Content-type: text/plain');
+ echo " == Generic Controller Index == \n\n";
+ $routed_str = implode('/', $routed);
+ $remainder_str = implode('/', $remainder);
+ echo "Full path: $routed_str/$remainder_str\n";
+ echo "Controller path: $routed_str\n";
+ echo "Remainder path: $remainder_str\n";
+ }
+ public function http404($routed, $remainder) {
+ $this->showView('http404', array('routed'=>$routed,
+ 'remainder'=>$remainder));
+ }
+}