/** * 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"; }
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; }
public static function resolveRoute($request) { $matchRoute = NULL; $runMvc = NULL; //get the match URI foreach (Route::routingTable() as $route => $mvc) { $pos = strpos($request->requestURI(), $route); if ($pos !== FALSE && $pos == 0) { $runMvc = $mvc; $matchRoute = $route; break; } } //Has matches of routing table; set the parameters in the request if (!is_null($runMvc) && !is_null($matchRoute)) { $idx = strlen($matchRoute); $restURI = substr($request->requestURI(), $idx); if (substr($restURI, 0, 1) == "/") { $restURI = substr($restURI, 1); } $uriParts = explode("/", $restURI); if (count($uriParts) >= 2) { for ($i = 0; $i < count($uriParts); $i = $i + 2) { $uriParts[$i + 1] = isset($uriParts[$i + 1]) ? $uriParts[$i + 1] : NULL; $request->setParam($uriParts[$i], $uriParts[$i + 1]); $request->setGETParam($uriParts[$i], $uriParts[$i + 1]); } } $routeParameters = Route::routingParams(); if (isset($routeParameters[$matchRoute])) { $useEqual = FALSE; //case #1: ?key1=value1&key2=value2 $options = ltrim($routeParameters[$matchRoute], "?"); $kvs = explode("&", $options); foreach ($kvs as $kv) { $kvParts = explode("=", $kv); if (count($kvParts) > 1) { $request->setParam($kvParts[0], $kvParts[1]); $request->setGETParam($kvParts[0], $kvParts[1]); $useEqual = TRUE; } } //case #2: /key1/value1/key2/value2 if (!$useEqual) { $options = ltrim($routeParameters[$matchRoute], "/"); $elems = explode("/", $options); if (count($elems) > 1) { for ($i = 0; $i < count($elems); $i = $i + 2) { $request->setParam($elems[$i], $elems[$i + 1]); $request->setGETParam($elems[$i], $elems[$i + 1]); } } } } return array("mvc" => $runMvc, "request" => $request); } //No matching route - start from app name; transform directive to MVC and params $trimUri = ltrim($request->requestURI(), "/"); $uelem = explode("/", $trimUri); if (count($uelem) < 4) { //wrong route or no matching at all return NULL; } $app = Framework::app($uelem[0]); //if $app is null, no matching routes if (!is_null($app)) { $mvcVal = new MvcValue(); $mvcVal->setNames($app->relativePath(), $app->name(), $uelem[1], Route::fromHyphenToCamelCase($uelem[2], TRUE), Route::fromHyphenToCamelCase($uelem[3])); for ($i = 4; $i < count($uelem); $i = $i + 2) { $uelem[$i + 1] = isset($uelem[$i + 1]) ? $uelem[$i + 1] : NULL; $request->setParam($uelem[$i], $uelem[$i + 1]); $request->setGETParam($uelem[$i], $uelem[$i + 1]); } return array("mvc" => $mvcVal, "request" => $request); } return NULL; }