]>
Commit | Line | Data |
---|---|---|
93b640b0 JP |
1 | <?php |
2 | /* Generic Site Classes | |
3 | * 2007/04/09 Joe Presbrey <presbrey@mit.edu> | |
4 | */ | |
5 | ||
6 | class Site { | |
7 | function Start() {} | |
93b640b0 JP |
8 | function Call($MODULE, $METHOD) { |
9 | $nCalls = 0; | |
b842dc21 | 10 | if (file_exists(strtolower('site/'.$MODULE.'.php'))) { |
93b640b0 JP |
11 | require_once strtolower('site/'.$MODULE.'.php'); |
12 | if (class_exists($MODULE)) { | |
b842dc21 JP |
13 | $page = new $MODULE($this, $METHOD); |
14 | $page->Start(); | |
15 | $handlers = $page->get_handlers($METHOD); | |
93b640b0 JP |
16 | switch ($METHOD) { |
17 | case 'GET': | |
18 | case 'get': | |
19 | $argv = array_merge($_COOKIE, $_FILES, $_POST, $_GET, isset($_SESSION)?$_SESSION:array()); break; | |
20 | case 'POST': | |
21 | case 'post': | |
22 | default: | |
23 | $argv = array_merge($_COOKIE, $_FILES, $_GET, $_POST, isset($_SESSION)?$_SESSION:array()); break; | |
24 | } | |
25 | foreach($handlers as $handler) { | |
26 | if (isset($argv[$handler]) && | |
b842dc21 | 27 | false !== $page->Call($handler, $argv)) { |
93b640b0 JP |
28 | $nCalls++; |
29 | } | |
30 | } | |
9d568c7b | 31 | if ($nCalls == 0 && is_callable(array($page, '_default'))) { |
b842dc21 | 32 | $page->Call('_default', $argv); |
93b640b0 JP |
33 | $nCalls++; |
34 | } | |
b842dc21 | 35 | $page->Finish(); |
93b640b0 JP |
36 | } |
37 | } | |
38 | return $nCalls; | |
39 | } | |
b842dc21 | 40 | function Finish() {} |
93b640b0 JP |
41 | } |
42 | ||
43 | class Page { | |
b842dc21 JP |
44 | var $site, $method; |
45 | function __construct($site, $method) { | |
46 | $this->site = $site; | |
47 | $this->method = $method; | |
48 | } | |
49 | function get_handlers() { | |
50 | $handlers = isset($this->handlers[$this->method]) ? | |
51 | $this->handlers[$this->method] : array(); | |
93b640b0 | 52 | return $handlers; |
93b640b0 | 53 | } |
b842dc21 JP |
54 | function has_handler($handler) { |
55 | return isset($this->handlers[$this->method]) ? in_array($handler, $this->handlers[$this->method]) : false; | |
56 | } | |
57 | ||
58 | function Start() {} | |
59 | function Call($handler, $argv) { | |
60 | if (is_callable(array($this, $handler))) | |
61 | return array(0, call_user_func_array(array($this, $handler), $argv)); | |
62 | else return false; | |
63 | } | |
64 | function Finish() {} | |
93b640b0 | 65 | } |