PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/SlightPHP.php

http://slightphp.googlecode.com/
PHP | 295 lines | 137 code | 18 blank | 140 comment | 20 complexity | f90b4c0ebf65f99e92f052daf4aaf4aa MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*{{{LICENSE
  3. +-----------------------------------------------------------------------+
  4. | SlightPHP Framework |
  5. +-----------------------------------------------------------------------+
  6. | This program is free software; you can redistribute it and/or modify |
  7. | it under the terms of the GNU General Public License as published by |
  8. | the Free Software Foundation. You should have received a copy of the |
  9. | GNU General Public License along with this program. If not, see |
  10. | http://www.gnu.org/licenses/. |
  11. | Copyright (C) 2008-2009. All Rights Reserved. |
  12. +-----------------------------------------------------------------------+
  13. | Supports: http://www.slightphp.com |
  14. +-----------------------------------------------------------------------+
  15. }}}*/
  16. /**
  17. * @package SlightPHP
  18. */
  19. if(!class_exists("SlightPHP",false)):
  20. final class SlightPHP{
  21. /**
  22. * @var string
  23. */
  24. public static $appDir=".";
  25. /**
  26. * @var string
  27. */
  28. public static $pathInfo="";
  29. /**
  30. * current zone
  31. * @var string
  32. */
  33. public static $zone;
  34. /**
  35. * @var string
  36. */
  37. public static $defaultZone="zone";
  38. /**
  39. * current page
  40. * @var string
  41. */
  42. public static $page;
  43. /**
  44. * @var string
  45. */
  46. public static $defaultPage="page";
  47. /**
  48. * current entry
  49. * @var string
  50. */
  51. public static $entry;
  52. /**
  53. * @var string
  54. */
  55. public static $defaultEntry="entry";
  56. /**
  57. * split flag of zone,classs,method
  58. *
  59. * @var string
  60. */
  61. public static $splitFlag="/";
  62. /**
  63. * zoneAlias
  64. *
  65. * @var array
  66. */
  67. public static $zoneAlias;
  68. /**
  69. * @param string $zone
  70. * @param string $alias
  71. * @return boolean
  72. */
  73. public static function setZoneAlias($zone,$alias){
  74. self::$zoneAlias[$zone]=$alias;
  75. return true;
  76. }
  77. /**
  78. * @param string $zone
  79. * @return string | boolean
  80. */
  81. public static function getZoneAlias($zone){
  82. return isset(self::$zoneAlias[$zone]) ? self::$zoneAlias[$zone] : false;
  83. }
  84. /**
  85. * defaultZone set
  86. *
  87. * @param string $zone
  88. * @return boolean
  89. */
  90. public static function setDefaultZone($zone){
  91. self::$defaultZone = $zone;
  92. return true;
  93. }
  94. /**
  95. * defaultZone get
  96. *
  97. * @return string
  98. */
  99. public static function getDefaultZone(){
  100. return self::$defaultZone;
  101. }
  102. /**
  103. * defaultClass set
  104. *
  105. * @param string $page
  106. * @return boolean
  107. */
  108. public static function setDefaultPage($page){
  109. self::$defaultPage = $page;
  110. return true;
  111. }
  112. /**
  113. * getDefaultClass get
  114. *
  115. * @return string
  116. */
  117. public static function getDefaultPage(){
  118. return self::$defaultPage;
  119. }
  120. /**
  121. * defaultMethod set
  122. *
  123. * @param string $entry
  124. * @return boolean
  125. */
  126. public static function setDefaultEntry($entry){
  127. self::$defaultEntry = $entry;
  128. return true;
  129. }
  130. /**
  131. * defaultMethod get
  132. *
  133. * @return string $method
  134. */
  135. public static function getDefaultEntry(){
  136. return self::$defaultEntry;
  137. }
  138. /**
  139. * splitFlag set
  140. *
  141. * @param string $flag
  142. * @return boolean
  143. */
  144. public static function setSplitFlag($flag){
  145. self::$splitFlag = $flag;
  146. return true;
  147. }
  148. /**
  149. * defaultMethod get
  150. *
  151. * @return string
  152. */
  153. public static function getSplitFlag(){
  154. return self::$splitFlag;
  155. }
  156. /**
  157. * appDir set && get
  158. * IMPORTANT: you must set absolute path if you use extension mode(extension=SlightPHP.so)
  159. *
  160. * @param string $dir
  161. * @return boolean
  162. */
  163. public static function setAppDir($dir){
  164. self::$appDir = $dir;
  165. return true;
  166. }
  167. public static function setPathInfo($pathInfo){
  168. self::$pathInfo = $pathInfo;
  169. return true;
  170. }
  171. /**
  172. * appDir get
  173. *
  174. * @return string
  175. */
  176. public static function getAppDir(){
  177. return self::$appDir;
  178. }
  179. /**
  180. * debug status set
  181. *
  182. * @param boolean $debug
  183. * @return boolean
  184. */
  185. public static function setDebug($debug){
  186. self::$_debug = $debug;
  187. return true;
  188. }
  189. /**
  190. * debug status get
  191. *
  192. * @return boolean
  193. */
  194. public static function getDebug(){
  195. return self::$_debug;
  196. }
  197. /**
  198. * main method!
  199. *
  200. * @param string $path
  201. * @return boolean
  202. */
  203. public static function run($path=""){
  204. $splitFlag = preg_quote(self::$splitFlag,"/");
  205. $path_array = array();
  206. if(!empty($path)){
  207. $isPart = true;
  208. if($path[0]=="/")$path=substr($path,1);
  209. $path_array = preg_split("/[$splitFlag\/]/",$path,-1);
  210. }else{
  211. $isPart = false;
  212. if(!empty(self::$pathInfo)){
  213. $url = self::$pathInfo;
  214. }else{
  215. if(!empty($_GET['PATH_INFO'])){
  216. $url = $_GET['PATH_INFO'];
  217. }else{
  218. $url = !empty($_SERVER["PATH_INFO"])?$_SERVER["PATH_INFO"]:"";
  219. }
  220. }
  221. if(!empty($url)){
  222. if($url[0]=="/")$url=substr($url,1);
  223. $path_array = preg_split("/[$splitFlag\/]/",$url,-1);
  224. }
  225. }
  226. $zone = !empty($path_array[0]) ? $path_array[0] : self::$defaultZone ;
  227. $page = !empty($path_array[1]) ? $path_array[1] : self::$defaultPage ;
  228. $entry = !empty($path_array[2]) ? $path_array[2] : self::$defaultEntry ;
  229. if(self::$zoneAlias && ($key = array_search($zone,self::$zoneAlias))!==false){
  230. $zone = $key;
  231. }
  232. if(!$isPart){
  233. self::$zone = $zone;
  234. self::$page = $page;
  235. self::$entry = $entry;
  236. }else{
  237. if($zone == self::$zone && $page == self::$page && $entry == self::$entry){
  238. self::debug("part ignored [$path]");
  239. return false;
  240. }
  241. }
  242. $app_file = self::$appDir . DIRECTORY_SEPARATOR . $zone . DIRECTORY_SEPARATOR . $page . ".page.php";
  243. if(!is_file($app_file)){
  244. self::debug("file[$app_file] does not exists");
  245. return false;
  246. }else{
  247. require_once(realpath($app_file));
  248. }
  249. $method = "Page".$entry;
  250. $classname = $zone ."_". $page;
  251. if(!class_exists($classname,false)){
  252. self::debug("class[$classname] does not exists");
  253. return false;
  254. }
  255. $path_array[0] = $zone;
  256. $path_array[1] = $page;
  257. $path_array[2] = $entry;
  258. $classInstance = new $classname($path_array);
  259. if(!method_exists($classInstance,$method)){
  260. self::debug("method[$method] does not exists in class[$classname]");
  261. return false;
  262. }
  263. return call_user_func(array(&$classInstance,$method),$path_array);
  264. }
  265. /**
  266. * @var boolean
  267. */
  268. private static $_debug=0;
  269. /*private*/
  270. private function debug($debugmsg){
  271. if(self::$_debug){
  272. error_log($debugmsg);
  273. echo "<!--slightphp debug: ".$debugmsg."-->";
  274. }
  275. }
  276. }
  277. endif;