/Upload/DoYouHaoBaby/LibPHP/App/Lib/Package.class.php
PHP | 341 lines | 297 code | 32 blank | 12 comment | 53 complexity | e842eee05c11bfe9e4bdb8206f252c32 MD5 | raw file
- <?php
- /* [DoYouHaoBaby!] (C)Dianniu From 2010.
- ????($) */
-
- !defined('DYHB_PATH') && exit;
-
- class Package{
-
- static private $OBJECTS=array();
- static private $_arrClassRegex=array('/^(.+)\.class\.php$/i','/^(.+)\.interface\.php$/i');
- static private $_bAutoLoad=true;
- static private $CLASS_PATH='Class.inc';
- static private $_arrImportedPackDir=array();
- static private $_arrClassFilePat=array('%DirPath%/%ClassName%.class.php');
- static private $_arrInterPat=array('%DirPath%/%ClassName%.interface.php');
- static private $_arrInsPat=array('%DirPath%/%ClassName%.ins.php');
- static private $_arrNotFiles=array();
- static private $_arrTempPackagePath='';
- static private $CLASS_PATHS=array(
- 'CORE'=>'LibPHP/Core',
- 'MODEL'=>'LibPHP/Core/Model',
- 'CONTROLLER'=>'LibPHP/Core/Controller',
- 'CHECK'=>'LibPHP/Core/Check',
- 'VIEW'=>'LibPHP/Core/View',
- 'VIEWHTML'=>'LibPHP/Core/View/ViewHtml',
- 'APP'=>'LibPHP/App',
- 'CONFIG'=>'LibPHP/App/Lib/Config',
- 'HTMLRESOURCE'=>'LibPHP/App/Lib/HtmlResource',
- 'CACHE'=>'LibPHP/App/Package/Cache',
- 'DS'=>'LibPHP/App/Package/Ds',
- 'TEMPLATE'=>'LibPHP/App/Package/Template',
- 'TEMPLATEHTML'=>'LibPHP/App/Package/Template/TemplateHtml',
- 'COMPILER'=>'LibPHP/App/Package/Template/TemplateHtml/Compiler',
- );
-
- static public function import($sPackage,$bForce=false){
- if(!is_dir($sPackage)){
- G::E("Package:'{$sPackage}' does not exists.");
- }
-
- // ???
- self::$_arrTempPackagePath=$sPackagePath=realpath($sPackage).'/';
- $sClassPathFile=$sPackagePath.self::$CLASS_PATH;
-
- if($bForce || !is_file($sClassPathFile)){
- $arrFileStores=array();
-
- // ???
- $arrClassPath=self::viewClass($sPackagePath);
- foreach($arrClassPath as $arrMap){
- $arrFileStores[$arrMap['class']]=$arrMap['file'];
- $arrKeys[]=$arrMap['class'];
- }
-
- // ?????????
- if(!empty($arrKeys) && count($arrKeys) != count(array_unique($arrKeys))){
- $arrDiffKeys=array();
- $arrDiffUnique=array_unique($arrKeys);
- foreach($arrDiffUnique as $nKey=>$sValue){
- if(in_array($sValue,$arrKeys)){unset($arrKeys[$nKey]);}
- }
- E(G::L('????????%s','LibDyhb',null,implode(',',$arrKeys)));
- }
- foreach($arrFileStores as $nKeyFileStore=>$sFileStoreValue){
- if(in_array(DYHB_PATH.'/LibPHP/'.$sFileStoreValue,(array)(include DYHB_PATH.'/LibPHP/Common/Paths.inc.php'))){
- unset($arrFileStores[$nKeyFileStore]);
- }
- }
- $sFileContents=serialize($arrFileStores);
-
- // ?????
- if(!is_file($sClassPathFile)){
- if($hFile=fopen($sClassPathFile,'a')){
- fclose($hFile);
- chmod($sClassPathFile,0666);
- }
- else{
- return false;
- }
- }
-
- // ????
- if(!file_put_contents($sClassPathFile,$sFileContents)){
- E(G::L('???????????? Class Path ???%s','LibDyhb',null,$sClassPathFile));
- }
- }
-
- // ??Classes Path??
- self::$OBJECTS=array_merge(self::$OBJECTS,array_map(array('Package','reallyPath'),self::readCache($sClassPathFile)));
- }
-
- static public function reallyPath($sValue){
- return self::$_arrTempPackagePath.$sValue;
- }
-
- static public function readCache($sCacheFile){
- if($hFp=fopen($sCacheFile,'r')){
- $sData=fread($hFp,filesize($sCacheFile));
- fclose($hFp);
- }
- return unserialize($sData);
- }
-
- static public function addPackagePath($sPackageName,$sPackagePath){
- if(isset(self::$CLASS_PATHS[$sPackageName])){
- G::E(G::L('?%s??????%s????????','LibDyhb',null,$sPackageName,$sPackagePath));
- }
- self::$CLASS_PATHS[$sPackageName]=$sPackagePath;
- }
-
- static public function regClass($sClass,$sPath){
- if(isset(self::$OBJECTS[$sClass])){
- G::E(G::L('?%s??????%s????????','LibDyhb',null,$sClass,$sPath));
- }
- self::$OBJECTS[$sClass]=$sPath;
- }
-
- static public function importC($sClass,$sPackageName=null){
- if(self::classExists($sClass)){// ?????
- return;
- }
- if(is_file($sClass)){// ????
- require($sClass);
- return;
- }
- else{// ????
- A::STRING($sPackageName);
- if(is_dir($sPackageName)){// ?????
- $sDir=&$sPackageName;
- }
- elseif(is_string($sPackageName)){// ??????????
- $sDir=self::getPackagePath($sPackageName);
- }
- else {
- A::ASSERT_(0,G::L("??\$sPackageName({%s})???",'LibDyhb',null,$sPackageName));
- }
-
- if(is_dir($sDir )){
- $sClassFilePath=self::findClassFile($sClass,$sDir);// ???????????
- if($sClassFilePath===false){
- G::E(G::L("? %s????? ",'LibDyhb',null, $sClass).G::L('??????????????????????????%s<br/>','LibDyhb',null,implode('<br/>',self::$_arrNotFiles)));
- }
- require $sClassFilePath;// ??
- }
- else{
- G::E(G::L("??? %s????",'LibDyhb',null,$sDir));
- }
- }
- }
-
- static public function importI($sInterface,$sPackage=null){
- if(is_file($sInterface)){// ????
- require($sInterface);
- return;
- }
- else{
- if(self::classExists($sInterface,true)){return;}// ?????
- $sPackageDir=self::getPackagePath($sPackage);// ?????
- $sPath=self::findClassFile($sInterface,$sPackageDir,self::$_arrInterPat);// ???????????
- if($sPath===false){
- G::E(G::L("?? %s??????",'LibDyhb',null,$sInterface).G::L('??????????????????????????%s','LibDyhb',null,implode('<br/>',self::$_arrNotFiles)));
- }
- require $sPath;// ??
- }
- }
-
- static public function setAutoload($bAutoload){
- if(!is_bool($bAutoload)){
- $bAutoload=$bAutoload?true:false;
- }
- else{
- $bAutoload =&$bAutoload;
- }
- $bOldValue=self::$_bAutoLoad;
- self::$_bAutoLoad=$bAutoload;
- return $bOldValue;
- }
-
- static public function autoLoad($sClassName){
- if(!self::$_bAutoLoad){
- return;
- }
- if(isset(self::$OBJECTS[$sClassName]) && !self::classExists($sClassName) && !self::classExists($sClassName,true)){
- require(self::$OBJECTS[$sClassName]);
- }
- }
-
- static public function getAutoload(){
- return self::$_bAutoLoad;
- }
-
- static public function classExists($sClassName,$bInter=false,$bAutoload=false){
- $bAutoloadOld=self::setAutoload($bAutoload);
- $sFuncName=$bInter?'interface_exists':'class_exists';
- $bResult=$sFuncName($sClassName);
- self::setAutoload($bAutoloadOld);
- return $bResult;
- }
-
- static public function getPackagePath($sPackName){
- $sPackName=strtoupper($sPackName);
- if(isset(self::$CLASS_PATHS[$sPackName])){
- $sPackagePath=self::$CLASS_PATHS[$sPackName];
- if(is_dir($sPackagePath)){
- return $sPackagePath;
- }
- else{
- return DYHB_PATH.'/'.$sPackagePath;
- }
- }
- else{
- return null;
- }
- }
-
- static private function viewClass($sDirectory,$sPreFilename=''){
- $arrReturnClass=array();
- $sDirectoryPath=realpath($sDirectory).'/';
- $hDir=opendir($sDirectoryPath);
- while($sFilename=readdir($hDir)){
- $sPath=$sDirectoryPath.$sFilename;
- if(is_file($sPath)){// ??
- foreach(self::$_arrClassRegex as $sRegexp){
- $arrRes=array();// ?????
- if(preg_match($sRegexp,$sFilename,$arrRes)){
- $sClassName=isset($arrRes[1])?$arrRes[1]:null;
- if($sClassName){
- $arrReturnClass[]=array('class'=>$sClassName,'file'=>$sPreFilename.$sFilename);
- }
- }
- }
- }
- else if(is_dir($sPath)){// ??
- $sSpecialDir=array('.','..','.svn','#note','common','Widget');
- if(in_array($sFilename,$sSpecialDir)){// ??????
- unset($sSpecialDir);
- continue;
- }
- else{// ?????
- $arrReturnClass=array_merge($arrReturnClass,self::viewClass($sPath,$sPreFilename.$sFilename.'/'));
- }
- }
- else{
- A::ASSERT_(0,G::L("\$sPath: %s?????????",'LibDyhb',null,$sPath));
- }
- }
- return $arrReturnClass;
- }
-
- static public function findClassFile($sClassName,$Dirs=null,$arrPat=null){
- if($Dirs===null){// ??????
- $arrDirs=self::$_arrImportedPackDir;
- }else if(is_string($Dirs)){
- $arrDirs=array($Dirs);
- }else if(is_array($Dirs)){
- $arrDirs=&$Dirs;
- }
- if(!is_array($arrDirs)){
- G::E(G::L('?? $Dirs ??????????','LibDyhb'));
- }
- if($arrPat===null){// ??????
- $arrPat=&self::$_arrClassFilePat;
- }
- foreach($arrDirs as $sDir){
- $sDir=G::tidyPath($sDir);
- foreach($arrPat as $sPat){
- $sPath=str_replace('%ClassName%',$sClassName,$sPat);
- $sPath=str_replace('%DirPath%',$sDir,$sPath);
- $bFindout=is_file($sPath);
- if($bFindout){
- return $sPath;
- }else{
- self::$_arrNotFiles[]=$sPath;
- }
- }
- }
- return false;
- }
-
- //[RUNTIME]
- static public function importJsPackage($sPackage,$bRePack=false){
- $sPackagePath=str_replace('.','/',$sPackage);
- $sIoPath=preg_replace('/^Dyhb/',DYHB_PATH.'/LibCom/Js',$sPackagePath);
- $sPackagePath=$sIoPath.'/'.basename( $sPackagePath ).'.package.js';
- if(!is_dir($sIoPath)){
- return;
- }
-
- if($bRePack || !is_file($sPackagePath)){
- self::packupJs($sIoPath,$sPackagePath);
- }
-
- if(is_file($sPackagePath)){
- return ;
- }
- }
-
- static private function packupJs($sDir,$sPackagePath){
- $sPackOrderFile=$sDir.'/Pack.order.php';
- if(is_file($sPackOrderFile)){
- $sPackOrder=file_get_contents($sPackOrderFile);
- $sPackOrder=str_replace("\r",'',$sPackOrder);
- $arrPackOrder=explode("\n",$sPackOrder);
- }else{
- $arrPackOrder=array();
- }
-
- // ?????????
- $hFiles=opendir($sDir);
- while(($sFileName=readdir($hFiles))!==false){
- $sPath="{$sDir}/{$sFileName}";
- if(is_file($sPath)){// ??
- if(!in_array($sFileName,$arrPackOrder)){
- if(preg_match('/^.+\.(class|interface)\.js$/i',$sFileName)){
- $arrPackOrder[]=$sFileName;
- }
- }
- }
- }
-
- // ??
- $hPackage=fopen($sPackagePath,'w');
- chmod($sPackagePath,0666);
- foreach($arrPackOrder as $sFileName){
- if(!$sFileName){
- continue;
- }
- $sFilePath=$sDir.'/'.$sFileName;
- if(is_file($sFilePath)){
- $sContent=file_get_contents($sFilePath);
- $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));
- fwrite($hPackage,$sContent);
- }else{
- fwrite($hPackage,G::L("throw new Error('??JavaScript??:%s!');",'LibDyhb',null,$sFilePath));
- }
- }
- fclose($hPackage);
- }
- //[/RUNTIME]
-
- }