public static function getTemplate($moduleName, $controllerName, $actionName) { if (is_null(TemplateMapper::$templates)) { TemplateMapper::templates(); } if (is_null(TemplateMapper::$templates)) { return NULL; } foreach (TemplateMapper::$templates as $mapModuleName => $controllerAction) { if ($moduleName == $mapModuleName) { if (isset(TemplateMapper::$templates[$mapModuleName]["ALL"])) { return TemplateMapper::$templates[$mapModuleName]["ALL"]["ALL"]; } foreach ($controllerAction as $mapControllerName => $action) { if ($moduleName == $mapModuleName) { if ($controllerName == $mapControllerName || $controllerName == Route::fromHyphenToCamelCase($mapControllerName, TRUE)) { if (isset(TemplateMapper::$templates[$mapModuleName][$mapControllerName]["ALL"])) { return TemplateMapper::$templates[$mapModuleName][$mapControllerName]["ALL"]; } } } foreach ($action as $mapActionName => $tpl) { if ($moduleName == $mapModuleName) { if ($controllerName == $mapControllerName || $controllerName == Route::fromHyphenToCamelCase($mapControllerName, TRUE)) { if ($actionName == $mapActionName || $actionName == Route::fromHyphenToCamelCase($mapActionName, FALSE)) { return $tpl; } } } } } } } return NULL; }
/** * 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(); } }
public function render($request, $currentMapName = NULL) { $templateScript = ""; $file = Mvc::templateDir() . DIRECTORY_SEPARATOR . $this->file(); if (file_exists($file)) { $templateScript = file_get_contents($file); $tplMapKeys = $this->getTemplateMapKeys($templateScript); $restTplMapKeys = $tplMapKeys; $mapValues = $this->currentMapValues($currentMapName); $mapRoutes = $this->currentMapRoutes($currentMapName); //replace map key with map value foreach ($tplMapKeys as $key => $tplMapKey) { if (!is_array($tplMapKey)) { $search = "%{" . $tplMapKey . "}%"; $replace = isset($mapValues[$tplMapKey]) ? $mapValues[$tplMapKey] : NULL; if (!is_null($replace)) { $templateScript = str_replace($search, $replace, $templateScript); unset($restTplMapKeys[$key]); } } } foreach ($restTplMapKeys as $key => $restTplMapKey) { if (!is_array($restTplMapKey) && isset($mapRoutes[$restTplMapKey])) { $mapRoute = $mapRoutes[$restTplMapKey]; $subMvc = new MvcValue(); $subMvc->setNames(Mvc::$currentApp->relativePath(), Mvc::$currentApp->name(), $mapRoute[0], Route::fromHyphenToCamelCase($mapRoute[1], TRUE), Route::fromHyphenToCamelCase($mapRoute[2])); $replace = SubMvcDispatch::run($request, $subMvc); $search = "%{" . $restTplMapKey . "}%"; if (!empty($replace)) { $templateScript = str_replace($search, $replace, $templateScript); } } } } return $templateScript; }
/** * This initializes what framework needs. */ public static function init() { //Get framework application names //Throw out the error if the json cannot be decoded $configArray = json_decode(file_get_contents(Framework::configFile()), TRUE); $names = array(); $apps = array(); $allAppRoutes = array(); foreach ($configArray['%applications'] as $application) { $keys = array_keys($application); $names[] = $keys[0]; $appSettings = $application[$keys[0]]; //server name & document root & relative path $serverName = $appSettings[0]; $docRoot = $appSettings[1]; $pathName = "/"; if (substr($appSettings[2], 0, 1) != "/") { $pathName .= $appSettings[2]; } $app = new Application($keys[0], $serverName, $docRoot, $pathName); //** Get the application's application config $appSpecificConfigArray = NULL; if (!is_null($app->appConfigFile())) { $appSpecificConfigArray = json_decode(file_get_contents($app->appConfigFile()), TRUE); } //Get the framework's application config $finalAppConfigArray = array(); foreach ($configArray["%application_configs"] as $appConfigArray) { if ($appConfigArray["%name"] == $keys[0]) { $finalAppConfigArray = $appConfigArray; } } //Overwrite the application_configs by application's config array if (isset($appSpecificConfigArray['%application_configs']) && isset($appSpecificConfigArray['%application_configs'][0])) { $finalAppConfigArray = array_replace_recursive($finalAppConfigArray, $appSpecificConfigArray['%application_configs'][0]); } $app->setConfig($finalAppConfigArray); $apps[$keys[0]] = $app; //Get all routes from application specific config if (isset($appSpecificConfigArray["%routes"])) { $allAppRoutes = array_replace_recursive($allAppRoutes, $appSpecificConfigArray["%routes"]); } } Framework::$appNames = $names; Framework::$apps = $apps; //process routing table here $table = array(); $routeParams = array(); if (isset($configArray['%routes'])) { //overwrite the routes $allRoutes = $configArray['%routes']; if (!empty($allAppRoutes)) { $allRoutes = array_replace_recursive($configArray['%routes'], $allAppRoutes); } foreach ($allRoutes as $routeUrl => $routeVar) { if (isset($apps[$routeVar[0]])) { $appObj = $apps[$routeVar[0]]; $mvcValue = new MvcValue(); $mvcValue->setNames($appObj->relativePath(), $routeVar[0], $routeVar[1], Route::fromHyphenToCamelCase($routeVar[2], TRUE), Route::fromHyphenToCamelCase($routeVar[3])); $table[$routeUrl] = $mvcValue; if (isset($routeVar[4])) { $routeParams[$routeUrl] = $routeVar[4]; } } } } Route::$routingTable = $table; Route::$routingParams = $routeParams; include "AutoLoader.php"; }
/** * Change hyphen name into camel case * @param string $name * @param boolean $hasFirstUppercase Decide if the first character is uppercase. * @return string A CamelCase text. */ public static function fromHyphenToCamelCase($name, $hasFirstUppercase = FALSE) { $words = explode('-', strtolower($name)); if (count($words) == 1) { $hyphenName = Route::fromCamelCaseToUcHyphen($name); $hyphenNameParts = explode("-", $hyphenName); if ($hasFirstUppercase) { $hyphenNameParts[0] = ucfirst($hyphenNameParts[0]); $oneName = join("", $hyphenNameParts); } else { if (false === function_exists('lcfirst')) { $hyphenNameParts[0] = Route::lcFirst($hyphenNameParts[0]); } else { $hyphenNameParts[0] = lcfirst($hyphenNameParts[0]); } $oneName = join("", $hyphenNameParts); } return $oneName; } $camelCaseName = ''; $index = 0; foreach ($words as $word) { if (!$hasFirstUppercase && $index == 0) { $camelCaseName .= trim($word); } else { $camelCaseName .= ucfirst(trim($word)); } $index++; } return $camelCaseName; }