private static function parse() { $splitFlag = SlightPHP::getSplitFlag(); $splitFlag = $splitFlag[0]; foreach (self::$_Routes as $route) { $pattern = $route['pattern']; foreach ($route as $k => $v) { if (preg_match("/:\\w+/", $k)) { $pattern = str_replace("{$k}", "({$v})", $pattern); } } if (preg_match_all("/{$pattern}/sm", !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : $_SERVER['REQUEST_URI'], $_m)) { array_shift($_m); $params = array(); if (!empty($_m)) { foreach ($_m as $_m2) { $params[] = $_m2[0]; } } $params = implode($splitFlag, $params); $zone = empty($route['zone']) ? SlightPHP::getDefaultZone() : $route['zone']; $page = empty($route['page']) ? SlightPHP::getDefaultPage() : $route['page']; $entry = empty($route['entry']) ? SlightPHP::getDefaultEntry() : $route['entry']; $PATH_INFO = "{$zone}{$splitFlag}{$page}{$splitFlag}{$entry}{$splitFlag}{$params}"; SlightPHP::setPathInfo($PATH_INFO); break; } } }
function tpl_function_part($path) { return !empty($path) ? SlightPHP::run($path) : ""; }
/** * Smarty {part} function part * * Type: function<br> * Name: part<br> * Purpose: include slightphp part<br> * @author Hetal <hetao at hetao dot name> * @param array * @param Smarty * @return string */ function smarty_function_part($params, &$smarty) { return !empty($params['path']) ? SlightPHP::run($params['path']) : ""; }
/** * main method! * * @param string $path * @return boolean */ public static function run($path = "") { $splitFlag = preg_quote(self::$splitFlag, "/"); $path_array = array(); if (!empty($path)) { $isPart = true; if ($path[0] == "/") { $path = substr($path, 1); } $path_array = preg_split("/[{$splitFlag}\\/]/", $path, -1); } else { $isPart = false; if (!empty(self::$pathInfo)) { $url = self::$pathInfo; } else { if (!empty($_GET["PATH_INFO"])) { $url = $_GET["PATH_INFO"]; } elseif (!empty($_SERVER['PATH_INFO'])) { $url = $_SERVER["PATH_INFO"]; } elseif (!empty($_SERVER['REQUEST_URI'])) { $url = $_SERVER["REQUEST_URI"]; } $url = preg_replace("/(\\/+)/", "/", $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/" . $url); $url_parsed = parse_url($url, PHP_URL_PATH); if (!empty($url_parsed)) { $url = $url_parsed; } } if (!empty($url)) { if ($url[0] == "/") { $url = substr($url, 1); } $path_array = preg_split("/[{$splitFlag}\\/]/", $url, -1); } } $zone = !empty($path_array[0]) ? $path_array[0] : self::$defaultZone; $page = !empty($path_array[1]) ? $path_array[1] : self::$defaultPage; $entry = !empty($path_array[2]) ? $path_array[2] : self::$defaultEntry; if (self::$zoneAlias && ($key = array_search($zone, self::$zoneAlias)) !== false) { $zone = $key; } if (!$isPart) { self::$zone = $zone; self::$page = $page; self::$entry = $entry; } else { if ($zone == self::$zone && $page == self::$page && $entry == self::$entry) { self::debug("part ignored [{$path}]"); return false; } } $app_file = self::$appDir . DIRECTORY_SEPARATOR . $zone . DIRECTORY_SEPARATOR . $page . ".page.php"; if (!is_file($app_file)) { self::debug("file[{$app_file}] does not exists"); return false; } else { require_once realpath($app_file); } $method = "Page" . $entry; $classname = $zone . "_" . $page; if (!class_exists($classname, false)) { self::debug("class[{$classname}] does not exists"); return false; } $path_array[0] = $zone; $path_array[1] = $page; $path_array[2] = $entry; $classInstance = new $classname($path_array); if (!method_exists($classInstance, $method)) { self::debug("method[{$method}] does not exists in class[{$classname}]"); return false; } return call_user_func(array(&$classInstance, $method), $path_array); }
<?php /** * 入口文件 * SmPSS(Supermarket Purchase Sale Stock) */ require_once "global.php"; SlightPHP::setDebug(true); SlightPHP::setAppDir(ROOT_APP); SlightPHP::setSplitFlag("-_."); SDb::setConfigFile(ROOT_CONFIG . "/db.ini.php"); if (($r = SlightPHP::run()) === false) { header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); include './app/v/' . base_Constant::TEMP_DIR . '/common/404.html'; } else { echo $r; exit; }
/** * main method! * * @param string $path * @return boolean */ public static function run($path = "") { $splitFlag = preg_quote(self::$splitFlag, "/"); $path_array = array(); if (!empty($path)) { $isPart = true; } else { $isPart = false; if (!empty($_SERVER['PATH_INFO'])) { $path = $_SERVER["PATH_INFO"]; } elseif (!empty($_SERVER['REQUEST_URI'])) { $path = $_SERVER["REQUEST_URI"]; } else { self::debug("path not set in params or server.path_info, server.request_uri"); return false; } } /* Skip leading / */ $len = strlen($path); $start = 0; for ($start = 0; $start < $len; $start++) { if ($path[$start] != '/') { break; } } $url_parsed = parse_url(substr($path, $start), PHP_URL_PATH); if (!empty($url_parsed)) { $url = $url_parsed; } else { $url = $path; } self::$pathInfo = $url; $path_array = preg_split("/[{$splitFlag}\\/]/", $url, -1); $zone = !empty($path_array[0]) ? $path_array[0] : self::$defaultZone; $page = !empty($path_array[1]) ? $path_array[1] : self::$defaultPage; $entry = !empty($path_array[2]) ? $path_array[2] : self::$defaultEntry; if (self::$zoneAlias && ($key = array_search($zone, self::$zoneAlias)) !== false) { $zone = $key; } if (!$isPart) { self::$zone = $zone; self::$page = $page; self::$entry = $entry; } else { if ($zone == self::$zone && $page == self::$page && $entry == self::$entry) { self::debug("part ignored [{$path}]"); return false; } } $app_file = self::$appDir . DIRECTORY_SEPARATOR . $zone . DIRECTORY_SEPARATOR . $page . ".page.php"; if (!is_file($app_file)) { self::debug("file[{$app_file}] does not exists"); return false; } else { require_once realpath($app_file); } $method = "Page" . $entry; $classname = $zone . "_" . $page; if (!class_exists($classname, false)) { self::debug("class[{$classname}] does not exists"); return false; } $path_array[0] = $zone; $path_array[1] = $page; $path_array[2] = $entry; $classInstance = new $classname($path_array); if (!method_exists($classInstance, $method)) { self::debug("method[{$method}] does not exists in class[{$classname}]"); return false; } return call_user_func(array(&$classInstance, $method), $path_array); }