PageRenderTime 83ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Upload/DoYouHaoBaby/LibPHP/App/Lib/Package.class.php

http://dyhb-frame.googlecode.com/
PHP | 341 lines | 297 code | 32 blank | 12 comment | 53 complexity | e842eee05c11bfe9e4bdb8206f252c32 MD5 | raw file
  1. <?php
  2. /* [DoYouHaoBaby!] (C)Dianniu From 2010.
  3. ????($) */
  4. !defined('DYHB_PATH') && exit;
  5. class Package{
  6. static private $OBJECTS=array();
  7. static private $_arrClassRegex=array('/^(.+)\.class\.php$/i','/^(.+)\.interface\.php$/i');
  8. static private $_bAutoLoad=true;
  9. static private $CLASS_PATH='Class.inc';
  10. static private $_arrImportedPackDir=array();
  11. static private $_arrClassFilePat=array('%DirPath%/%ClassName%.class.php');
  12. static private $_arrInterPat=array('%DirPath%/%ClassName%.interface.php');
  13. static private $_arrInsPat=array('%DirPath%/%ClassName%.ins.php');
  14. static private $_arrNotFiles=array();
  15. static private $_arrTempPackagePath='';
  16. static private $CLASS_PATHS=array(
  17. 'CORE'=>'LibPHP/Core',
  18. 'MODEL'=>'LibPHP/Core/Model',
  19. 'CONTROLLER'=>'LibPHP/Core/Controller',
  20. 'CHECK'=>'LibPHP/Core/Check',
  21. 'VIEW'=>'LibPHP/Core/View',
  22. 'VIEWHTML'=>'LibPHP/Core/View/ViewHtml',
  23. 'APP'=>'LibPHP/App',
  24. 'CONFIG'=>'LibPHP/App/Lib/Config',
  25. 'HTMLRESOURCE'=>'LibPHP/App/Lib/HtmlResource',
  26. 'CACHE'=>'LibPHP/App/Package/Cache',
  27. 'DS'=>'LibPHP/App/Package/Ds',
  28. 'TEMPLATE'=>'LibPHP/App/Package/Template',
  29. 'TEMPLATEHTML'=>'LibPHP/App/Package/Template/TemplateHtml',
  30. 'COMPILER'=>'LibPHP/App/Package/Template/TemplateHtml/Compiler',
  31. );
  32. static public function import($sPackage,$bForce=false){
  33. if(!is_dir($sPackage)){
  34. G::E("Package:'{$sPackage}' does not exists.");
  35. }
  36. // ???
  37. self::$_arrTempPackagePath=$sPackagePath=realpath($sPackage).'/';
  38. $sClassPathFile=$sPackagePath.self::$CLASS_PATH;
  39. if($bForce || !is_file($sClassPathFile)){
  40. $arrFileStores=array();
  41. // ???
  42. $arrClassPath=self::viewClass($sPackagePath);
  43. foreach($arrClassPath as $arrMap){
  44. $arrFileStores[$arrMap['class']]=$arrMap['file'];
  45. $arrKeys[]=$arrMap['class'];
  46. }
  47. // ?????????
  48. if(!empty($arrKeys) && count($arrKeys) != count(array_unique($arrKeys))){
  49. $arrDiffKeys=array();
  50. $arrDiffUnique=array_unique($arrKeys);
  51. foreach($arrDiffUnique as $nKey=>$sValue){
  52. if(in_array($sValue,$arrKeys)){unset($arrKeys[$nKey]);}
  53. }
  54. E(G::L('????????%s','LibDyhb',null,implode(',',$arrKeys)));
  55. }
  56. foreach($arrFileStores as $nKeyFileStore=>$sFileStoreValue){
  57. if(in_array(DYHB_PATH.'/LibPHP/'.$sFileStoreValue,(array)(include DYHB_PATH.'/LibPHP/Common/Paths.inc.php'))){
  58. unset($arrFileStores[$nKeyFileStore]);
  59. }
  60. }
  61. $sFileContents=serialize($arrFileStores);
  62. // ?????
  63. if(!is_file($sClassPathFile)){
  64. if($hFile=fopen($sClassPathFile,'a')){
  65. fclose($hFile);
  66. chmod($sClassPathFile,0666);
  67. }
  68. else{
  69. return false;
  70. }
  71. }
  72. // ????
  73. if(!file_put_contents($sClassPathFile,$sFileContents)){
  74. E(G::L('???????????? Class Path ???%s','LibDyhb',null,$sClassPathFile));
  75. }
  76. }
  77. // ??Classes Path??
  78. self::$OBJECTS=array_merge(self::$OBJECTS,array_map(array('Package','reallyPath'),self::readCache($sClassPathFile)));
  79. }
  80. static public function reallyPath($sValue){
  81. return self::$_arrTempPackagePath.$sValue;
  82. }
  83. static public function readCache($sCacheFile){
  84. if($hFp=fopen($sCacheFile,'r')){
  85. $sData=fread($hFp,filesize($sCacheFile));
  86. fclose($hFp);
  87. }
  88. return unserialize($sData);
  89. }
  90. static public function addPackagePath($sPackageName,$sPackagePath){
  91. if(isset(self::$CLASS_PATHS[$sPackageName])){
  92. G::E(G::L('?%s??????%s????????','LibDyhb',null,$sPackageName,$sPackagePath));
  93. }
  94. self::$CLASS_PATHS[$sPackageName]=$sPackagePath;
  95. }
  96. static public function regClass($sClass,$sPath){
  97. if(isset(self::$OBJECTS[$sClass])){
  98. G::E(G::L('?%s??????%s????????','LibDyhb',null,$sClass,$sPath));
  99. }
  100. self::$OBJECTS[$sClass]=$sPath;
  101. }
  102. static public function importC($sClass,$sPackageName=null){
  103. if(self::classExists($sClass)){// ?????
  104. return;
  105. }
  106. if(is_file($sClass)){// ????
  107. require($sClass);
  108. return;
  109. }
  110. else{// ????
  111. A::STRING($sPackageName);
  112. if(is_dir($sPackageName)){// ?????
  113. $sDir=&$sPackageName;
  114. }
  115. elseif(is_string($sPackageName)){// ??????????
  116. $sDir=self::getPackagePath($sPackageName);
  117. }
  118. else {
  119. A::ASSERT_(0,G::L("??\$sPackageName({%s})???",'LibDyhb',null,$sPackageName));
  120. }
  121. if(is_dir($sDir )){
  122. $sClassFilePath=self::findClassFile($sClass,$sDir);// ???????????
  123. if($sClassFilePath===false){
  124. G::E(G::L("? %s????? ",'LibDyhb',null, $sClass).G::L('??????????????????????????%s<br/>','LibDyhb',null,implode('<br/>',self::$_arrNotFiles)));
  125. }
  126. require $sClassFilePath;// ??
  127. }
  128. else{
  129. G::E(G::L("??? %s????",'LibDyhb',null,$sDir));
  130. }
  131. }
  132. }
  133. static public function importI($sInterface,$sPackage=null){
  134. if(is_file($sInterface)){// ????
  135. require($sInterface);
  136. return;
  137. }
  138. else{
  139. if(self::classExists($sInterface,true)){return;}// ?????
  140. $sPackageDir=self::getPackagePath($sPackage);// ?????
  141. $sPath=self::findClassFile($sInterface,$sPackageDir,self::$_arrInterPat);// ???????????
  142. if($sPath===false){
  143. G::E(G::L("?? %s??????",'LibDyhb',null,$sInterface).G::L('??????????????????????????%s','LibDyhb',null,implode('<br/>',self::$_arrNotFiles)));
  144. }
  145. require $sPath;// ??
  146. }
  147. }
  148. static public function setAutoload($bAutoload){
  149. if(!is_bool($bAutoload)){
  150. $bAutoload=$bAutoload?true:false;
  151. }
  152. else{
  153. $bAutoload =&$bAutoload;
  154. }
  155. $bOldValue=self::$_bAutoLoad;
  156. self::$_bAutoLoad=$bAutoload;
  157. return $bOldValue;
  158. }
  159. static public function autoLoad($sClassName){
  160. if(!self::$_bAutoLoad){
  161. return;
  162. }
  163. if(isset(self::$OBJECTS[$sClassName]) && !self::classExists($sClassName) && !self::classExists($sClassName,true)){
  164. require(self::$OBJECTS[$sClassName]);
  165. }
  166. }
  167. static public function getAutoload(){
  168. return self::$_bAutoLoad;
  169. }
  170. static public function classExists($sClassName,$bInter=false,$bAutoload=false){
  171. $bAutoloadOld=self::setAutoload($bAutoload);
  172. $sFuncName=$bInter?'interface_exists':'class_exists';
  173. $bResult=$sFuncName($sClassName);
  174. self::setAutoload($bAutoloadOld);
  175. return $bResult;
  176. }
  177. static public function getPackagePath($sPackName){
  178. $sPackName=strtoupper($sPackName);
  179. if(isset(self::$CLASS_PATHS[$sPackName])){
  180. $sPackagePath=self::$CLASS_PATHS[$sPackName];
  181. if(is_dir($sPackagePath)){
  182. return $sPackagePath;
  183. }
  184. else{
  185. return DYHB_PATH.'/'.$sPackagePath;
  186. }
  187. }
  188. else{
  189. return null;
  190. }
  191. }
  192. static private function viewClass($sDirectory,$sPreFilename=''){
  193. $arrReturnClass=array();
  194. $sDirectoryPath=realpath($sDirectory).'/';
  195. $hDir=opendir($sDirectoryPath);
  196. while($sFilename=readdir($hDir)){
  197. $sPath=$sDirectoryPath.$sFilename;
  198. if(is_file($sPath)){// ??
  199. foreach(self::$_arrClassRegex as $sRegexp){
  200. $arrRes=array();// ?????
  201. if(preg_match($sRegexp,$sFilename,$arrRes)){
  202. $sClassName=isset($arrRes[1])?$arrRes[1]:null;
  203. if($sClassName){
  204. $arrReturnClass[]=array('class'=>$sClassName,'file'=>$sPreFilename.$sFilename);
  205. }
  206. }
  207. }
  208. }
  209. else if(is_dir($sPath)){// ??
  210. $sSpecialDir=array('.','..','.svn','#note','common','Widget');
  211. if(in_array($sFilename,$sSpecialDir)){// ??????
  212. unset($sSpecialDir);
  213. continue;
  214. }
  215. else{// ?????
  216. $arrReturnClass=array_merge($arrReturnClass,self::viewClass($sPath,$sPreFilename.$sFilename.'/'));
  217. }
  218. }
  219. else{
  220. A::ASSERT_(0,G::L("\$sPath: %s?????????",'LibDyhb',null,$sPath));
  221. }
  222. }
  223. return $arrReturnClass;
  224. }
  225. static public function findClassFile($sClassName,$Dirs=null,$arrPat=null){
  226. if($Dirs===null){// ??????
  227. $arrDirs=self::$_arrImportedPackDir;
  228. }else if(is_string($Dirs)){
  229. $arrDirs=array($Dirs);
  230. }else if(is_array($Dirs)){
  231. $arrDirs=&$Dirs;
  232. }
  233. if(!is_array($arrDirs)){
  234. G::E(G::L('?? $Dirs ??????????','LibDyhb'));
  235. }
  236. if($arrPat===null){// ??????
  237. $arrPat=&self::$_arrClassFilePat;
  238. }
  239. foreach($arrDirs as $sDir){
  240. $sDir=G::tidyPath($sDir);
  241. foreach($arrPat as $sPat){
  242. $sPath=str_replace('%ClassName%',$sClassName,$sPat);
  243. $sPath=str_replace('%DirPath%',$sDir,$sPath);
  244. $bFindout=is_file($sPath);
  245. if($bFindout){
  246. return $sPath;
  247. }else{
  248. self::$_arrNotFiles[]=$sPath;
  249. }
  250. }
  251. }
  252. return false;
  253. }
  254. //[RUNTIME]
  255. static public function importJsPackage($sPackage,$bRePack=false){
  256. $sPackagePath=str_replace('.','/',$sPackage);
  257. $sIoPath=preg_replace('/^Dyhb/',DYHB_PATH.'/LibCom/Js',$sPackagePath);
  258. $sPackagePath=$sIoPath.'/'.basename( $sPackagePath ).'.package.js';
  259. if(!is_dir($sIoPath)){
  260. return;
  261. }
  262. if($bRePack || !is_file($sPackagePath)){
  263. self::packupJs($sIoPath,$sPackagePath);
  264. }
  265. if(is_file($sPackagePath)){
  266. return ;
  267. }
  268. }
  269. static private function packupJs($sDir,$sPackagePath){
  270. $sPackOrderFile=$sDir.'/Pack.order.php';
  271. if(is_file($sPackOrderFile)){
  272. $sPackOrder=file_get_contents($sPackOrderFile);
  273. $sPackOrder=str_replace("\r",'',$sPackOrder);
  274. $arrPackOrder=explode("\n",$sPackOrder);
  275. }else{
  276. $arrPackOrder=array();
  277. }
  278. // ?????????
  279. $hFiles=opendir($sDir);
  280. while(($sFileName=readdir($hFiles))!==false){
  281. $sPath="{$sDir}/{$sFileName}";
  282. if(is_file($sPath)){// ??
  283. if(!in_array($sFileName,$arrPackOrder)){
  284. if(preg_match('/^.+\.(class|interface)\.js$/i',$sFileName)){
  285. $arrPackOrder[]=$sFileName;
  286. }
  287. }
  288. }
  289. }
  290. // ??
  291. $hPackage=fopen($sPackagePath,'w');
  292. chmod($sPackagePath,0666);
  293. foreach($arrPackOrder as $sFileName){
  294. if(!$sFileName){
  295. continue;
  296. }
  297. $sFilePath=$sDir.'/'.$sFileName;
  298. if(is_file($sFilePath)){
  299. $sContent=file_get_contents($sFilePath);
  300. $sContent=trim(preg_replace(array('/(^|\r|\n)\/\*.+?(\r|\n)\*\/(\r|\n)/is','/\/\/note.+?(\r|\n)/i','/\/\/debug.+?(\r|\n)/i','/(^|\r|\n)(\s|\t)+/','/(\r|\n)/',"/\/\*(.*?)\*\//ies"),'',$sContent));
  301. fwrite($hPackage,$sContent);
  302. }else{
  303. fwrite($hPackage,G::L("throw new Error('??JavaScript??:%s!');",'LibDyhb',null,$sFilePath));
  304. }
  305. }
  306. fclose($hPackage);
  307. }
  308. //[/RUNTIME]
  309. }