]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /* Generic Site Classes | |
3 | * 2007/04/09 Joe Presbrey <presbrey@mit.edu> | |
4 | */ | |
5 | ||
6 | class Site { | |
7 | var $URI, $ARGV; | |
8 | function __construct($uri) { | |
9 | $this->URI = $uri; | |
10 | $this->ARGV = array_merge( | |
11 | array_prepend_keys($_COOKIE,'c_'), | |
12 | array_prepend_keys($_FILES,'f_'), | |
13 | array_prepend_keys($_GET,'g_'), | |
14 | array_prepend_keys($_POST,'p_')); | |
15 | } | |
16 | function Start() { | |
17 | if (isset($_SESSION)) { | |
18 | $this->ARGV = array_merge( | |
19 | $this->ARGV, | |
20 | array_prepend_keys($_SESSION,'s_')); | |
21 | } | |
22 | } | |
23 | function Run($MODULE, $METHOD) { | |
24 | $nCalls = 0; | |
25 | if (file_exists(strtolower('site/'.$MODULE.'.php'))) { | |
26 | require_once strtolower('site/'.$MODULE.'.php'); | |
27 | if (class_exists($MODULE)) { | |
28 | $page = new $MODULE($this, $METHOD); | |
29 | $page->Start(); | |
30 | $handlers = array_intersect( | |
31 | $page->get_handlers($METHOD), | |
32 | array_keys($this->ARGV)); | |
33 | foreach($handlers as $handler) { | |
34 | if (false !== $page->Run($handler)) { | |
35 | $nCalls++; | |
36 | } | |
37 | } | |
38 | if ($nCalls == 0 && is_callable(array($page, '_default'))) { | |
39 | $page->Run('_default'); | |
40 | $nCalls++; | |
41 | } | |
42 | $page->Finish(); | |
43 | } | |
44 | } | |
45 | return $nCalls; | |
46 | } | |
47 | function Finish() {} | |
48 | } | |
49 | ||
50 | class Page { | |
51 | var $SITE, $METHOD, $URI; | |
52 | function __construct($site, $method) { | |
53 | $this->SITE = $site; | |
54 | $this->METHOD = $method; | |
55 | foreach($site->ARGV as $k=>$v) | |
56 | $this->$k = $v; | |
57 | $this->URI = $site->URI; | |
58 | } | |
59 | function get_handlers() { | |
60 | $handlers = isset($this->handlers[$this->METHOD]) ? | |
61 | $this->handlers[$this->METHOD] : array(); | |
62 | return $handlers; | |
63 | } | |
64 | function has_handler($handler) { | |
65 | return isset($this->handlers[$this->METHOD]) ? | |
66 | in_array($handler, $this->handlers[$this->METHOD]) : false; | |
67 | } | |
68 | ||
69 | function Start() {} | |
70 | function Run($handler) { | |
71 | if (is_callable(array($this, $handler))) { | |
72 | return array(0, call_user_func(array($this, $handler))); | |
73 | } else { | |
74 | return false; | |
75 | } | |
76 | } | |
77 | function Finish() {} | |
78 | } |