00001 <?php
00002
00009 class mosController
00010 {
00016 private $commandname;
00022 private $command;
00028 private $view;
00034 private $request;
00040 private $renderer;
00041
00050 function __construct (mosRequest &$request, mosRenderer &$renderer, $commandname='command')
00051 {
00052 $this->request =& $request;
00053 $this->renderer =& $renderer;
00054 $this->commandname = $commandname;
00055 }
00056
00062 function run()
00063 {
00064 $command = $this->request->getParam($_REQUEST, $this->commandname, 'default');
00065 $this->forward($command);
00066
00067 $this->setView($this->command->getView());
00068 $this->view->render($this->request, $this->renderer);
00069 }
00070
00076 function forward($command)
00077 {
00078 $this->setCommand($command);
00079 $this->command->execute($this, $this->request);
00080 }
00081
00090 function setCommand($command)
00091 {
00092 $commandclass = $command.'Command';
00093
00094 if (class_exists($commandclass)) {
00095 $this->command = new $commandclass();
00096 } else {
00097 return trigger_error("Command class '$commandclass' not found.", E_USER_ERROR);
00098 }
00099 }
00109 function setView($viewname)
00110 {
00111 $commandname = substr(get_class($this->command), 0, -7);
00112 $viewclass = $viewname.'View';
00113 if (class_exists($viewclass)) {
00114 $this->view = new $viewclass();
00115 } else {
00116 return trigger_error("View class '$viewclass' not found.", E_USER_ERROR);
00117 }
00118 }
00125 function redirect($url)
00126 {
00127 if (headers_sent()) {
00128 echo "<script>document.location.href='$url';</script>";
00129 } else {
00130 if (ob_get_contents()) while (@ob_end_clean());
00131 header( "Location: $url" );
00132 }
00133 exit;
00134 }
00135
00136 }
00137
00142 class mosCommand
00143 {
00149 private $viewname;
00150
00156 function __construct(){}
00157
00165 function execute(mosController &$controller, mosRequest &$request)
00166 {
00167 return trigger_error('Command::execute() must be overridden.', E_USER_ERROR);
00168 }
00174 function setView($viewname)
00175 {
00176 $this->viewname = $viewname;
00177 }
00183 function getView()
00184 {
00185 return $this->viewname;
00186 }
00187 }
00188
00193 class mosView
00194 {
00195
00201 function __construct(){}
00209 function render(mosRequest &$request, mosRenderer &$renderer)
00210 {
00211 return trigger_error('View::render() must be overridden');
00212 }
00213 }
00214
00215
00216 class mosRenderer
00217 {
00218
00219 private $dir;
00220 private $vars = array();
00221 private $engine = 'php';
00222 private $template = '';
00223
00224 function __construct(){}
00225
00226 static function &getInstance($type = 'php') {
00227 static $renderer;
00228 if (is_null($renderer[$type])) {
00229 if ($type == 'php') {
00230 $renderer[$type] = new mosRenderer();
00231 } else {
00232 $classname = $type . 'Renderer';
00233 if (class_exists($classname))
00234 $renderer[$type] = new $classname();
00235 }
00236 }
00237 return $renderer[$type];
00238 }
00239
00240 function display($template, $return = false){
00241 if ($template == NULL){
00242 return trigger_error('A template has not been specified', E_USER_ERROR);
00243 }
00244 $this->template = $this->dir . $template;
00245
00246 if (is_readable($this->template)) {
00247 extract($this->getvars());
00248 if ($return) {
00249 ob_start();
00250 include_once($this->template);
00251 $ret = ob_get_contents();
00252 ob_end_clean();
00253 return $ret;
00254 } else {
00255 include_once($this->template);
00256 }
00257 } else {
00258 return trigger_error("Template file $template does not exist or is not readable", E_USER_ERROR);
00259 }
00260 return false;
00261 }
00262
00263 function fetch($template){
00264 return $this->display($template, true);
00265 }
00266
00267 function &getengine(){
00268 return $this->engine;
00269 }
00270
00271 function addvar($key, $value){
00272 $this->vars[$key] = $value;
00273 }
00274
00275 function addbyref ($key, &$value) {
00276 $this->vars[$key] = $value;
00277 }
00278
00279 function getvars($name = false){
00280 return (isset($this->vars[$name])) ? $this->vars[$name] : $this->vars;
00281 }
00282
00283 function setdir($dir){
00284 $this->dir = (substr($dir, -1) == DIRECTORY_SEPARATOR) ? $dir : $dir.DIRECTORY_SEPARATOR;
00285 }
00286
00287 function getdir(){
00288 return $this->dir;
00289 }
00290
00291 function settemplate($template){
00292 $this->template = $template;
00293 }
00294 }