/** * This method forward to the action. * * @param object $Router the instance of the Router class. */ public static function run() { $request = new Request(); $request->set(); $results = Route::resolveRoute($request); if (!is_null($results)) { Mvc::setCurrentMvc($results['mvc']); Mvc::setCurrentApp(Framework::app($results['mvc']->appName())); $request = $results['request']; TemplateMapper::templates(); $tpl = TemplateMapper::getTemplate(Mvc::moduleName(), Mvc::controllerName(), Mvc::actionName()); session_start(); //Take out global, not use require_once Mvc::controllerFile(); $controller = Mvc::controllerClassName(); $action = Mvc::actionFunctionName(); self::$runController = new $controller(); self::$runController->setParams($request->params()); self::$runController->setRequest($request); self::$runController->setDefaultModel(); self::$runController->setDefaultView($tpl); //init method acts as a constructor after all the variables being set self::$runController->init(); if (method_exists(self::$runController, $action)) { self::$runController->{$action}(); } else { throw AiryException("AiryMVC: Missing Action Function."); } return self::$runController; session_write_close(); } }
/** * To deal with the database config(s) * * @return object database */ public function initDb() { $config = Mvc::currentApp()->config(); foreach ($config->db() as $dbConfig) { if ($dbConfig["%type"] != "mongo") { $database = new SqlDb(); } else { $database = new MongoDb(); } $database->initDb($dbConfig); $this->dbs[] = $database; } if ($config->dbMode() == "copy") { $masterDbConfig = $config->db(0); if ($masterDbConfig["%type"] != "mongo") { $this->db = new SqlDbCopyFarm($this->dbs); } } else { $this->db = $this->dbs[0]; } }
/** * This method render the view. * * @throws AiryException * */ public function render() { try { $viewElems = explode(DIRECTORY_SEPARATOR, Mvc::viewFile()); $viewElems[3] = lcfirst($viewElems[3]); $viewElems[4] = lcfirst($viewElems[4]); $fallBackViewFile = join(DIRECTORY_SEPARATOR, $viewElems); $viewFile = Mvc::viewFile(); if (!file_exists($viewFile)) { $viewFile = $fallBackViewFile; } if (file_exists($viewFile)) { $templateScript = ""; $viewKey = NULL; if (!is_null($this->template)) { $urlMapParam = isset($this->params[$this->template->urlMapParam()]) ? $this->params[$this->template->urlMapParam()] : NULL; $templateScript = $this->template->render($this->request, $urlMapParam); $viewKey = $this->template->viewKey(); } $viewContent = file_get_contents($viewFile); if (!is_null($viewKey)) { $viewContent = str_replace("%{" . $viewKey . "}%", $viewContent, $templateScript); } //set view variables to view if (!is_null($this->vars)) { foreach ($this->vars as $name => $value) { ${$name} = $value; } } $fp = fopen("airy.view://view_content", "r+"); fwrite($fp, $viewContent); fclose($fp); include "airy.view://view_content"; } else { throw new AiryException("No View File {$viewFile} Existed."); } } catch (Exception $e) { echo 'Exception: ', $e->getMessage(), "\n"; } }
public function map() { if (!is_null($this->map)) { return $this->map; } $file = Mvc::templateMapDir() . DIRECTORY_SEPARATOR . $this->mapFile(); if (file_exists($file)) { $mapContent = file_get_contents($file); $this->map = json_decode($mapContent, TRUE); } return $this->map; }
/** * Set the default model. */ public function setDefaultModel() { if (file_exists(Mvc::modelFile())) { require_once Mvc::modelFile(); $modelClassName = Mvc::modelClassName(); $this->model = new $modelClassName(); $this->model->initDb(); } else { error_log("expect a model file"); //@TODO: throws an exception. } }