]> andersk Git - sql-web.git/blobdiff - lib/joe/site.lib.php
git-svn-id: svn://presbrey.mit.edu/php/lib@127 a142d4bd-2cfb-0310-9673-cb33a7e74f58
[sql-web.git] / lib / joe / site.lib.php
index 3496242ae8920edab9ea0abd876dcfa3f02c4f61..cf09c22513498d6193f235fa94d976ef03f55624 100755 (executable)
@@ -4,55 +4,75 @@
  */
 
 class Site {
-       function Start() {}
-       function Finish() {}
-
-       function Call($MODULE, $METHOD) {
+       var $URI, $ARGV;
+       function __construct($uri) {
+               $this->URI = $uri;
+               $this->ARGV = array_merge(
+                       array_prepend_keys($_COOKIE,'c_'),
+                       array_prepend_keys($_FILES,'f_'),
+                       array_prepend_keys($_GET,'g_'),
+                       array_prepend_keys($_POST,'p_'));
+       }
+       function Start() {
+               if (isset($_SESSION)) {
+                       $this->ARGV = array_merge(
+                               $this->ARGV,
+                               array_prepend_keys($_SESSION,'s_'));
+               }
+       }
+       function Run($MODULE, $METHOD) {
                $nCalls = 0;
-               if (file_exists('site/'.$MODULE.'.php')) {
+               if (file_exists(strtolower('site/'.$MODULE.'.php'))) {
                        require_once strtolower('site/'.$MODULE.'.php');
                        if (class_exists($MODULE)) {
-                               $object = new $MODULE;
-                               $object->Start();
-                               $handlers = $object->get_handlers($METHOD);
-                               switch ($METHOD) {
-                                       case 'GET':
-                                       case 'get':
-                                               $argv = array_merge($_COOKIE, $_FILES, $_POST, $_GET, isset($_SESSION)?$_SESSION:array()); break;
-                                       case 'POST':
-                                       case 'post':
-                                       default:
-                                               $argv = array_merge($_COOKIE, $_FILES, $_GET, $_POST, isset($_SESSION)?$_SESSION:array()); break;
-                               }
+                               $page = new $MODULE($this, $METHOD);
+                               $page->Start();
+                               $handlers = array_intersect(
+                                       $page->get_handlers($METHOD),
+                                       array_keys($this->ARGV));
                                foreach($handlers as $handler) {
-                                       if (isset($argv[$handler]) &&
-                                               is_callable($object, $handler)) {
-                                               call_user_func_array(array($object, $handler), $argv);
+                                       if (false !== $page->Run($handler)) {
                                                $nCalls++;
                                        }
                                }
-                               if ($nCalls == 0 && is_callable($object, 'default')) {
-                                       call_user_func_array(array($object, $handler), $argv);
+                               if ($nCalls == 0 && is_callable(array($page, '_default'))) {
+                                       $page->Run('_default');
                                        $nCalls++;
                                }
-                               $object->Finish();
+                               $page->Finish();
                        }
                }
                return $nCalls;
        }
+       function Finish() {}
 }
 
 class Page {
-       function Start() {}
-       function Finish() {}
-
-       function get_handlers($method) {
-               $handlers = isset($this->handlers[$method]) ?
-                                       $this->handlers[$method] :
-                                       array('default');
+       var $SITE, $METHOD, $URI;
+       function __construct($site, $method) {
+               $this->SITE = $site;
+               $this->METHOD = $method;
+               foreach($site->ARGV as $k=>$v)
+                       $this->$k = $v;
+               $this->URI = $site->URI;
+       }
+       function get_handlers() {
+               $handlers = isset($this->handlers[$this->METHOD]) ?
+                                       $this->handlers[$this->METHOD] : array();
                return $handlers;
-               //return array_map(
-               //      create_function('$a','return "_'.$method.'_".$a;'),
-               //      $this->handlers[$method]);
        }
+       function has_handler($handler) {
+               return isset($this->handlers[$this->METHOD]) ?
+                               in_array($handler, $this->handlers[$this->METHOD]) : false;
+       }
+
+       function Start() {}
+       function Run($handler) {
+               if (is_callable(array($this, $handler))) {
+                       return array(0, call_user_func(array($this, $handler)));
+               } else {
+                       return false;
+               }
+       }
+       function Finish() {}
 }
This page took 0.035259 seconds and 4 git commands to generate.