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

/cubi/openbiz/bin/BizClassLoader.php

http://openbiz-cubi.googlecode.com/
PHP | 381 lines | 135 code | 25 blank | 221 comment | 37 complexity | 5e2e47ea1ff86dd0ab59d949d82fe2e9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPOpenBiz Framework
  4. *
  5. * This file contain BizController class, the C from MVC of phpOpenBiz framework,
  6. * and execute it. So bootstrap script simply include this file. For sample of
  7. * bootstrap script please see controller.php under baseapp/bin
  8. *
  9. * LICENSE
  10. *
  11. * This source file is subject to the BSD license that is bundled
  12. * with this package in the file LICENSE.txt.
  13. *
  14. * @package openbiz.bin
  15. * @copyright Copyright (c) 2005-2011, Rocky Swen
  16. * @license http://www.opensource.org/licenses/bsd-license.php
  17. * @link http://www.phpopenbiz.org/
  18. * @version $Id: BizController.php 4882 2012-11-30 07:48:46Z hellojixian@gmail.com $
  19. */
  20. /**
  21. * BizController is the class that dispatches client requests to proper objects
  22. *
  23. * @package openbiz.bin
  24. * @author Rocky Swen <rocky@phpopenbiz.org> and Openbiz Dev Team
  25. * @copyright Copyright (c) 2005-2011, Rocky Swen
  26. * @access public
  27. */
  28. class BizClassLoader
  29. {
  30. private static $_classNameCache = array();
  31. protected function __construct()
  32. {
  33. // $coreClassMap = include(__DIR__ . DIRECTORY_SEPARATOR . 'autoload_classmap.php' ) ;
  34. // print_r($coreClassMap);
  35. // exit;
  36. //self::registerClassMap($coreClassMap);
  37. }
  38. /**
  39. * Class autoloading
  40. * - not check $_classNameCache, because autoload called only class not yet load
  41. * - not need package
  42. * @param type $className
  43. * @return boolean
  44. */
  45. public static function autoload($className)
  46. {
  47. $filePath = self::getAutoloadLibFileWithPath($className);
  48. //var_dump( $filePath);
  49. if ($filePath)
  50. {
  51. include_once($filePath); // auto_load
  52. self::$_classNameCache[$className] = 1; //
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * Get openbiz library php file path for autoload, remove metadata package searching
  59. *
  60. * @param string $className
  61. * @return string php library file path
  62. * */
  63. public static function getAutoloadLibFileWithPath($className)
  64. {
  65. if (!$className)
  66. return;
  67. // use class map first
  68. if (@isset(self::$classMap[$className]))
  69. {
  70. return self::$classMap[$className];
  71. }
  72. // search it in cache first
  73. $cacheKey = $className . "_path";
  74. if (extension_loaded('apc') && ($filePath = apc_fetch($cacheKey)) != null)
  75. return $filePath;
  76. if (strpos($className, 'Zend') === 0)
  77. {
  78. $filePath = self::getZendFileWithPath($className);
  79. } else
  80. {
  81. $filePath = self::getCoreLibFilePath($className);
  82. }
  83. // cache it to save file search
  84. if ($filePath && extension_loaded('apc'))
  85. apc_store($cacheKey, $filePath);
  86. /* if (!file_exists($filePath)) {
  87. trigger_error("Cannot find the library file of $className", E_USER_ERROR);
  88. } */
  89. return $filePath;
  90. }
  91. public static function getZendFileWithPath($className)
  92. {
  93. // autodiscover the path from the class name
  94. $classFile = ZEND_FRWK_HOME . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  95. return $classFile;
  96. }
  97. public static function loadMetadataClass($className, $packageName = '')
  98. {
  99. if (class_exists($className, false))
  100. return true;
  101. if (isset(self::$_classNameCache[$packageName . $className]))
  102. return true;
  103. if (strpos($className, 'Zend') === 0)
  104. return true;
  105. $filePath = BizSystem::getLibFileWithPath($className, $packageName);
  106. if ($filePath)
  107. {
  108. include_once($filePath);
  109. self::$_classNameCache[$packageName . $className] = 1;
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * Get core path of class
  116. *
  117. * @param string $className class name
  118. * @return string full file name of class
  119. */
  120. public static function getCoreLibFilePath($className)
  121. {
  122. // if class not yet collect on class map, scan core path.
  123. $classFile = $className . '.php';
  124. // TODO: search the file under bin/, bin/data, bin/ui. bin/service, bin/easy, bin/easy/element.
  125. // guess class type and folder
  126. $lowClassName = strtolower($className);
  127. if (strrpos($lowClassName, 'service') > 0)
  128. $corePaths = array('service/');
  129. else if (strrpos($lowClassName, 'form') > 0 || strrpos($lowClassName, 'form') === 0)
  130. $corePaths = array('easy/');
  131. else if (strrpos($lowClassName, 'view') > 0 || strrpos($lowClassName, 'view') === 0)
  132. $corePaths = array('easy/');
  133. else if (strrpos($lowClassName, 'dataobj') > 0)
  134. $corePaths = array('data/');
  135. else
  136. $corePaths = array('easy/element/', '', 'data/', 'easy/', 'service/');
  137. //$corePaths = array('', 'data/', 'easy/', 'easy/element/', 'ui/', 'service/');
  138. foreach ($corePaths as $path)
  139. {
  140. $_classFile = OPENBIZ_BIN . $path . $classFile;
  141. //echo "file_exists($_classFile)\n";
  142. if (file_exists($_classFile))
  143. return $_classFile;
  144. }
  145. return null;
  146. }
  147. /**
  148. * Get openbiz library php file path by searching modules/package, /bin/package and /bin
  149. *
  150. * @param string $className
  151. * @return string php library file path
  152. * */
  153. public static function getLibFileWithPath($className, $packageName = "")
  154. {
  155. if (!$className)
  156. return;
  157. // use class map first
  158. if (@isset(self::$classMap[$packageName . $className]))
  159. {
  160. return self::$classMap[$packageName . $className];
  161. }
  162. // search it in cache first
  163. $cacheKey = $className . "_path";
  164. if (extension_loaded('apc') && ($filePath = apc_fetch($cacheKey)) != null)
  165. return $filePath;
  166. if (strpos($className, ".") > 0)
  167. $className = str_replace(".", "/", $className);
  168. $filePath = null;
  169. $classFile = $className . ".php";
  170. $classFile_0 = $className . ".php";
  171. // convert package name to path, add it to classfile
  172. $classFileIsFound = false;
  173. if ($packageName)
  174. {
  175. $path = str_replace(".", "/", $packageName);
  176. // check the leading char '@'
  177. $checkExtModule = true;
  178. if (strpos($path, '@') === 0)
  179. {
  180. $path = substr($path, 1);
  181. $checkExtModule = false;
  182. }
  183. // search in apphome/modules directory first, search in apphome/bin directory then
  184. $classFiles[0] = MODULE_PATH . "/" . $path . "/" . $classFile;
  185. $classFiles[1] = APP_HOME . "/bin/" . $path . "/" . $classFile;
  186. if ($checkExtModule && defined('MODULE_EX_PATH'))
  187. array_unshift($classFiles, MODULE_EX_PATH . "/" . $path . "/" . $classFile);
  188. foreach ($classFiles as $classFile)
  189. {
  190. if (file_exists($classFile))
  191. {
  192. $filePath = $classFile;
  193. $classFileIsFound = true;
  194. break;
  195. }
  196. }
  197. }
  198. if (!$classFileIsFound)
  199. $filePath = self::getCoreLibFilePath($className);
  200. // cache it to save file search
  201. if ($filePath && extension_loaded('apc'))
  202. apc_store($cacheKey, $filePath);
  203. /* if (!file_exists($filePath)) {
  204. trigger_error("Cannot find the library file of $className", E_USER_ERROR);
  205. } */
  206. return $filePath;
  207. }
  208. public static function registerClassMap($classMap) {
  209. self::$classMap = array_merge(self::$classMap, $classMap);
  210. }
  211. /**
  212. * class map for openbiz core class
  213. * @author agus suhartono
  214. * @var array
  215. */
  216. public static $classMap = array();
  217. /*
  218. public static $classMap = array(
  219. "BizController" => "/bin/BizController.php",
  220. "BizSystem" => "/bin/BizSystem.php",
  221. "ClientProxy" => "/bin/ClientProxy.php",
  222. "Configuration" => "/bin/Configuration.php",
  223. "OB_ErrorHandler" => "/bin/ErrorHandler.php",
  224. "Expression" => "/bin/Expression.php",
  225. "I18n" => "/bin/I18n.php",
  226. "ObjectFactory" => "/bin/ObjectFactory.php",
  227. "Resource" => "/bin/Resource.php",
  228. "SessionContext" => "/bin/SessionContext.php",
  229. "TypeManager" => "/bin/TypeManager.php",
  230. "UserSetting" => "/bin/UserSetting.php",
  231. "BDOException" => "/bin/sysclass_inc.php",
  232. "BFMException" => "/bin/sysclass_inc.php",
  233. "BSVCException" => "/bin/sysclass_inc.php",
  234. "MetaIterator" => "/bin/sysclass_inc.php",
  235. "MetaObject" => "/bin/sysclass_inc.php",
  236. "Parameter" => "/bin/sysclass_inc.php",
  237. "ValidationException" => "/bin/sysclass_inc.php",
  238. "iSessionObject" => "/bin/sysclass_inc.php",
  239. "iUIControl" => "/bin/sysclass_inc.php",
  240. "BizDataObj" => "/bin/data/BizDataObj.php",
  241. "BizDataObj_Abstract" => "/bin/data/BizDataObj_Abstract.php",
  242. "BizDataObj_Lite" => "/bin/data/BizDataObj_Lite.php",
  243. "BizDataSql" => "/bin/data/BizDataSql.php",
  244. "BizDataTree" => "/bin/data/BizDataTree.php",
  245. "NodeRecord" => "/bin/data/BizDataTree.php",
  246. "BizField" => "/bin/data/BizField.php",
  247. "DataRecord" => "/bin/data/DataRecord.php",
  248. "DataSet" => "/bin/data/DataSet.php",
  249. "BizDataObj_Assoc" => "/bin/data/private/BizDataObj_Assoc.php",
  250. "BizDataObj_SQLHelper" => "/bin/data/private/BizDataObj_SQLHelper.php",
  251. "BizRecord" => "/bin/data/private/BizRecord.php",
  252. "ObjReference" => "/bin/data/private/ObjReference.php",
  253. "TableJoin" => "/bin/data/private/TableJoin.php",
  254. "DynaView" => "/bin/easy/DynaView.php",
  255. "EasyForm" => "/bin/easy/EasyForm.php",
  256. "EasyFormGrouping" => "/bin/easy/EasyFormGrouping.php",
  257. "EasyFormTree" => "/bin/easy/EasyFormTree.php",
  258. "EasyFormWizard" => "/bin/easy/EasyFormWizard.php",
  259. "EasyView" => "/bin/easy/EasyView.php",
  260. "EasyViewWizard" => "/bin/easy/EasyViewWizard.php",
  261. "FormRenderer" => "/bin/easy/FormRenderer.php",
  262. "HTMLMenus" => "/bin/easy/HTMLMenus.php",
  263. "HTMLTabs" => "/bin/easy/HTMLTabs.php",
  264. "TabView" => "/bin/easy/HTMLTabs.php",
  265. "HTMLTree" => "/bin/easy/HTMLTree.php",
  266. "Panel" => "/bin/easy/Panel.php",
  267. "PickerForm" => "/bin/easy/PickerForm.php",
  268. "ViewRenderer" => "/bin/easy/ViewRenderer.php",
  269. "AutoSuggest" => "/bin/easy/element/AutoSuggest.php",
  270. "Button" => "/bin/easy/element/Button.php",
  271. "CKEditor" => "/bin/easy/element/CKEditor.php",
  272. "CheckListbox" => "/bin/easy/element/CheckListbox.php",
  273. "Checkbox" => "/bin/easy/element/Checkbox.php",
  274. "ColorPicker" => "/bin/easy/element/ColorPicker.php",
  275. "ColumnBar" => "/bin/easy/element/ColumnBar.php",
  276. "ColumnBool" => "/bin/easy/element/ColumnBool.php",
  277. "ColumnHidden" => "/bin/easy/element/ColumnHidden.php",
  278. "ColumnImage" => "/bin/easy/element/ColumnImage.php",
  279. "ColumnList" => "/bin/easy/element/ColumnList.php",
  280. "ColumnPassword" => "/bin/easy/element/ColumnPassword.php",
  281. "ColumnShare" => "/bin/easy/element/ColumnShare.php",
  282. "ColumnSorting" => "/bin/easy/element/ColumnSorting.php",
  283. "ColumnStyle" => "/bin/easy/element/ColumnStyle.php",
  284. "ColumnText" => "/bin/easy/element/ColumnText.php",
  285. "ColumnValue" => "/bin/easy/element/ColumnValue.php",
  286. "DropDownList" => "/bin/easy/element/DropDownList.php",
  287. "EditCombobox" => "/bin/easy/element/EditCombobox.php",
  288. "Element" => "/bin/easy/element/Element.php",
  289. "File" => "/bin/easy/element/File.php",
  290. "FileUploader" => "/bin/easy/element/FileUploader.php",
  291. "FormElement" => "/bin/easy/element/FormElement.php",
  292. "HTMLBlock" => "/bin/easy/element/HTMLBlock.php",
  293. "HTMLButton" => "/bin/easy/element/HTMLButton.php",
  294. "HTMLPreview" => "/bin/easy/element/HTMLPreview.php",
  295. "Hidden" => "/bin/easy/element/Hidden.php",
  296. "IDCardReader" => "/bin/easy/element/IDCardReader.php",
  297. "IFrameBox" => "/bin/easy/element/IFrameBox.php",
  298. "ImageSelector" => "/bin/easy/element/ImageSelector.php",
  299. "ImageUploader" => "/bin/easy/element/ImageUploader.php",
  300. "InputDate" => "/bin/easy/element/InputDate.php",
  301. "InputDateRangePicker" => "/bin/easy/element/InputDateRangePicker.php",
  302. "InputDatetime" => "/bin/easy/element/InputDatetime.php",
  303. "InputElement" => "/bin/easy/element/InputElement.php",
  304. "InputPassword" => "/bin/easy/element/InputPassword.php",
  305. "InputPicker" => "/bin/easy/element/InputPicker.php",
  306. "InputText" => "/bin/easy/element/InputText.php",
  307. "LabelBar" => "/bin/easy/element/LabelBar.php",
  308. "LabelBool" => "/bin/easy/element/LabelBool.php",
  309. "LabelImage" => "/bin/easy/element/LabelImage.php",
  310. "LabelList" => "/bin/easy/element/LabelList.php",
  311. "LabelPassword" => "/bin/easy/element/LabelPassword.php",
  312. "LabelText" => "/bin/easy/element/LabelText.php",
  313. "LabelTextPaging" => "/bin/easy/element/LabelTextPaging.php",
  314. "LabelTextarea" => "/bin/easy/element/LabelTextarea.php",
  315. "Listbox" => "/bin/easy/element/Listbox.php",
  316. "OptionElement" => "/bin/easy/element/OptionElement.php",
  317. "PageSelector" => "/bin/easy/element/PageSelector.php",
  318. "PagesizeSelector" => "/bin/easy/element/PagesizeSelector.php",
  319. "Password" => "/bin/easy/element/Password.php",
  320. "Radio" => "/bin/easy/element/Radio.php",
  321. "RawData" => "/bin/easy/element/RawData.php",
  322. "ResetButton" => "/bin/easy/element/ResetButton.php",
  323. "RichText" => "/bin/easy/element/RichText.php",
  324. "RowCheckbox" => "/bin/easy/element/RowCheckbox.php",
  325. "Spacer" => "/bin/easy/element/Spacer.php",
  326. "SubmitButton" => "/bin/easy/element/SubmitButton.php",
  327. "Textarea" => "/bin/easy/element/Textarea.php",
  328. "TreeLabelText" => "/bin/easy/element/TreeLabelText.php",
  329. "TreeListbox" => "/bin/easy/element/TreeListbox.php",
  330. "accessService" => "/bin/service/accessService.php",
  331. "aclService" => "/bin/service/aclService.php",
  332. "auditService" => "/bin/service/auditService.php",
  333. "authService" => "/bin/service/authService.php",
  334. "cacheService" => "/bin/service/cacheService.php",
  335. "chartService" => "/bin/service/chartService.php",
  336. "compileService" => "/bin/service/compileService.php",
  337. "cryptService" => "/bin/service/cryptService.php",
  338. "doTriggerService" => "/bin/service/doTriggerService.php",
  339. "emailService" => "/bin/service/emailService.php",
  340. "excelService" => "/bin/service/excelService.php",
  341. "genIdService" => "/bin/service/genIdService.php",
  342. "ioService" => "/bin/service/ioService.php",
  343. "localeInfoService" => "/bin/service/localeInfoService.php",
  344. "logService" => "/bin/service/logService.php",
  345. "pdfService" => "/bin/service/pdfService.php",
  346. "profileService" => "/bin/service/profileService.php",
  347. "queryService" => "/bin/service/queryService.php",
  348. "reportService" => "/bin/service/reportService.php",
  349. "securityService" => "/bin/service/securityService.php",
  350. "validateService" => "/bin/service/validateService.php",
  351. "QueryStringParam" => "/bin/util/QueryStringParam.php",
  352. "XMLParser" => "/bin/util/xmltoarray.php",
  353. "Smarty" => "/others/Smarty/libs/Smarty.class.php",
  354. );
  355. */
  356. }