PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/Ux.php

https://bitbucket.org/web3d/uxf
PHP | 302 lines | 240 code | 13 blank | 49 comment | 10 complexity | 7d0f2e11e007217f41623dc9a22edb4e MD5 | raw file
  1. <?php
  2. //内核路径
  3. define('UX_PATH', dirname(__file__) . DIRECTORY_SEPARATOR);
  4. /**
  5. * @brief 引用内核入口文件
  6. * @author webning
  7. * @date 2010-12-02
  8. * @version 0.6
  9. */
  10. class Ux {
  11. /**
  12. * @brief 当前应用的对象
  13. */
  14. public static $app;
  15. /**
  16. * @brief 控制器所在位置
  17. */
  18. public static $_classes = array('application.controllers.*');
  19. public static $enableIncludePath=true;
  20. private static $classMap=array();
  21. private static $_aliases=array('system' => UX_PATH); // alias => path
  22. private static $_imports=array(); // alias => class name or directory
  23. private static $_includePaths; // list of include paths
  24. /**
  25. * @brief 创建Application应用
  26. * @param string $className
  27. * @param array $config
  28. * @return object Application对象
  29. */
  30. public static function createApp($className, $config) {
  31. $app = new $className($config);
  32. return $app;
  33. }
  34. /**
  35. * @brief 创建WebApplication应用
  36. * @param array $config
  37. * @return object Application对象
  38. */
  39. public static function createWebApp($config = null) {
  40. self::$app = self::createApp('UxWebApplication', $config);
  41. return self::$app;
  42. }
  43. /**
  44. * @brief 实现系统类的自动加载
  45. * @param String $className 类名称
  46. * @return bool true
  47. */
  48. public static function autoload($className) {
  49. if (isset(self::$_coreClasses[$className])) {
  50. include(UX_PATH . self::$_coreClasses[$className]);
  51. } else {
  52. if (!preg_match('|^\w+$|', $className)) {
  53. return true;
  54. }
  55. foreach (self::$_includePaths as $classPath) {
  56. $filePath = $classPath.DIRECTORY_SEPARATOR . $className . '.php';
  57. if (is_file($filePath)) {
  58. include($filePath);
  59. return true;
  60. }
  61. }
  62. }
  63. return true;
  64. }
  65. /**
  66. * @brief 用户自定义类的注册入口
  67. * @param array $classes 如:array('system.net.load.*','system.net.ftp.*');
  68. */
  69. public static function setClasses($classes) {
  70. if (is_string($classes))
  71. self::import($classes);
  72. if (is_array($classes)){
  73. foreach ($classes as $class) {
  74. self::import($class);
  75. }
  76. }
  77. }
  78. /**
  79. * 导入单个类或者整个目录
  80. *
  81. * 导入类类似于include相关类文件,导入目录则等同于将目录添加到PHP include路径
  82. *
  83. * 采用路径别名的方式书写,例如:
  84. * <ul>
  85. * <li><code>application.components.GoogleMap</code>: 导入<code>GoogleMap</code> 类.</li>
  86. * <li><code>application.components.*</code>: 导入<code>components</code> 目录.</li>
  87. * </ul>
  88. *
  89. * @param string $alias path alias to be imported
  90. * @param boolean $forceInclude 是否立刻包含该类文件,否则只有在类被真正使用时才被包含
  91. * @return string
  92. * @throws UxException
  93. */
  94. public static function import($alias,$forceInclude=false)
  95. {
  96. if(isset(self::$_imports[$alias])) // previously imported
  97. return self::$_imports[$alias];
  98. if(class_exists($alias,false) || interface_exists($alias,false))
  99. return self::$_imports[$alias]=$alias;
  100. if(($pos=strrpos($alias,'\\'))!==false) // a class name in PHP 5.3 namespace format
  101. {
  102. $namespace=str_replace('\\','.',ltrim(substr($alias,0,$pos),'\\'));
  103. if(($path=self::getPathOfAlias($namespace))!==false)
  104. {
  105. $classFile=$path.DIRECTORY_SEPARATOR.substr($alias,$pos+1).'.php';
  106. if($forceInclude)
  107. {
  108. if(is_file($classFile))
  109. require($classFile);
  110. else
  111. throw new UxException('Alias "'.$alias.'" is invalid. Make sure it points to an existing PHP file and the file is readable.');
  112. self::$_imports[$alias]=$alias;
  113. }
  114. else
  115. self::$classMap[$alias]=$classFile;
  116. return $alias;
  117. }
  118. else
  119. throw new UxException('Alias "'.$alias.'" is invalid. Make sure it points to an existing directory.');
  120. }
  121. if(($pos=strrpos($alias,'.'))===false) // a simple class name
  122. {
  123. if($forceInclude && self::autoload($alias))
  124. self::$_imports[$alias]=$alias;
  125. return $alias;
  126. }
  127. $className=(string)substr($alias,$pos+1);
  128. $isClass=$className!=='*';
  129. if($isClass && (class_exists($className,false) || interface_exists($className,false)))
  130. return self::$_imports[$alias]=$className;
  131. if(($path=self::getPathOfAlias($alias))!==false)
  132. {
  133. if($isClass)
  134. {
  135. if($forceInclude)
  136. {
  137. if(is_file($path.'.php'))
  138. require($path.'.php');
  139. else
  140. throw new UxException('Alias "'.$alias.'" is invalid. Make sure it points to an existing PHP file and the file is readable.');
  141. self::$_imports[$alias]=$className;
  142. }
  143. else
  144. self::$classMap[$className]=$path.'.php';
  145. return $className;
  146. }
  147. else // a directory
  148. {
  149. if(self::$_includePaths===null)
  150. {
  151. self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
  152. if(($pos=array_search('.',self::$_includePaths,true))!==false)
  153. unset(self::$_includePaths[$pos]);
  154. }
  155. array_unshift(self::$_includePaths,$path);
  156. if(self::$enableIncludePath && set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,self::$_includePaths))===false)
  157. self::$enableIncludePath=false;
  158. return self::$_imports[$alias]=$path;
  159. }
  160. }
  161. else
  162. throw new UxException('Alias "'.$alias.'" is invalid. Make sure it points to an existing directory or file.');
  163. }
  164. /**
  165. * 将别名转换为文件路径
  166. * 注意,该方法不检测文件路径是否存在,仅检测根别名是否可用
  167. * @param string $alias alias (如system.web.Controller)
  168. * @return mixed
  169. */
  170. public static function getPathOfAlias($alias)
  171. {
  172. if(isset(self::$_aliases[$alias]))
  173. return self::$_aliases[$alias];
  174. else if(($pos=strpos($alias,'.'))!==false)
  175. {
  176. $rootAlias=substr($alias,0,$pos);
  177. if(isset(self::$_aliases[$rootAlias]))
  178. return self::$_aliases[$alias]=rtrim(self::$_aliases[$rootAlias].DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,substr($alias,$pos+1)),'*'.DIRECTORY_SEPARATOR);
  179. /*else if(self::$_app instanceof UxWebApplication)
  180. {
  181. if(self::$_app->findModule($rootAlias)!==null)
  182. return self::getPathOfAlias($alias);
  183. }*/
  184. }
  185. return false;
  186. }
  187. /**
  188. * Create a path alias.
  189. * Note, this method neither checks the existence of the path nor normalizes the path.
  190. * @param string $alias alias to the path
  191. * @param string $path the path corresponding to the alias. If this is null, the corresponding
  192. * path alias will be removed.
  193. */
  194. public static function setPathOfAlias($alias,$path)
  195. {
  196. if(empty($path))
  197. unset(self::$_aliases[$alias]);
  198. else
  199. self::$_aliases[$alias]=rtrim($path,'\\/');
  200. }
  201. //系统内核所有类文件注册信息
  202. public static $_coreClasses = array(
  203. //core
  204. 'UxObject' => 'core/UxObject.php',
  205. 'UxAbstractApplication' => 'core/UxAbstractApplication.php',
  206. 'UxException' => 'core/UxException.php',
  207. 'IUxInterceptor' => 'core/IUxInterceptor.php',
  208. 'UxInterceptor' => 'core/UxInterceptor.php',
  209. //util
  210. 'UxFileHelper' => 'util/UxFileHelper.php',
  211. 'UxStringUtil' => 'util/UxStringUtil.php',
  212. 'UxXmlUtil' => 'util/UxXmlUtil.php',
  213. 'UxTimeHelper' => 'util/UxTimeHelper.php',
  214. 'UxImageHelper' => 'util/UxImageHelper.php',
  215. 'UxFileUploadAgent' => 'util/UxFileUploadAgent.php',
  216. 'UxHashHelper' => 'util/UxHashHelper.php',
  217. 'UxEnryptor' => 'util/UxEnryptor.php',
  218. 'UxSafeUtil' => 'util/UxSafeUtil.php',
  219. 'UxCaptcha' => 'util/UxCaptcha.php',
  220. 'UxCommonValidateHelper' => 'util/UxCommonValidateHelper.php',
  221. 'UxLangUtil' => 'util/UxLangUtil.php',
  222. 'UxSmtpHelper' => 'util/UxSmtpHelper.php',
  223. 'JSON' => 'vendor/JSON.php',
  224. //url
  225. 'UxRouter' => 'web/url/UxRouter.php',
  226. 'UxServerVars' => 'web/url/UxServerVars.php',
  227. 'IUxServerVars' => 'web/url/adapter/IUxServerVars.php',
  228. 'UxApacheServerVars' => 'web/url/adapter/UxApacheServerVars.php',
  229. 'UxIisServerVars' => 'web/url/adapter/UxIisServerVars.php',
  230. 'UxNginxServerVars' => 'web/url/adapter/UxNginxServerVars.php',
  231. //http
  232. 'UxHttpServer' => 'web/http/UxHttpServer.php',
  233. 'UxHttpClient' => 'web/http/UxHttpClient.php',
  234. 'UxHttpRequest' => 'web/http/UxHttpRequest.php',
  235. 'UxHttpCookie' => 'web/http/UxHttpCookie.php',
  236. 'UxHttpSession' => 'web/http/UxHttpSession.php',
  237. //web
  238. 'UxWebApplication' => 'web/UxWebApplication.php',
  239. 'UxHttpException' => 'web/UxHttpException.php',
  240. 'UxController' => 'web/UxController.php',
  241. 'UxClientScript' => 'web/UxClientScript.php',
  242. 'UxAbstractAction' => 'web/action/UxAbstractAction.php',
  243. 'UxInlineAction' => 'web/action/UxInlineAction.php',
  244. 'UxViewAction' => 'web/action/UxViewAction.php',
  245. 'UxCurdAction' => 'web/action/UxCurdAction.php',
  246. 'UxViewTagParser' => 'web/view/UxViewTagParser.php',
  247. //log
  248. 'UxLogger' => 'log/UxLogger.php',
  249. 'IUxLogger' => 'log/adapter/IUxLogger.php',
  250. 'UxFileLogger' => 'log/adapter/UxFileLogger.php',
  251. 'UxDbLogger' => 'log/adapter/UxDbLogger.php',
  252. //cache
  253. 'UxCache' => 'cache/UxCache.php',
  254. 'IUxCache' => 'cache/IUxCache.php',
  255. 'UxFileCacheAdapter' => 'cache/adapter/UxFileCacheAdapter.php',
  256. 'UxMemCacheAdapter' => 'cache/adapter/UxMemCacheAdapter.php',
  257. 'UxApcCacheAdapter' => 'cache/adapter/UxApcCacheAdapter.php',
  258. 'UxEacceleratorCacheAdapter' => 'cache/adapter/UxEacceleratorCacheAdapter.php',
  259. 'UxDbCacheAdapter' => 'cache/adapter/UxDbCacheAdapter.php',
  260. 'UxXcacheCacheAdapter' => 'cache/adapter/UxXcacheCacheAdapter.php',
  261. 'UxCacheSession' => 'cache/UxCacheSession.php',
  262. //db
  263. 'UxDbModel' => 'db/UxDbModel.php',
  264. 'UxDbManager' => 'db/UxDbManager.php',
  265. 'UxAbstractDbDriver' => 'db/driver/UxAbstractDbDriver.php',
  266. 'UxMysqlDbDriver' => 'db/driver/UxMysqlDbDriver.php',
  267. 'UxPagingHelper' => 'db/UxPagingHelper.php',
  268. 'UxQueryBuilder' => 'db/UxQueryBuilder.php',
  269. );
  270. }
  271. /**
  272. * @brief 实现系统内容所有类的自动加载
  273. * @param String $className
  274. */
  275. function __autoload($className) {
  276. Ux::autoload($className);
  277. }