小巧的php文檔生成類 - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 小巧的php文檔生成類 (http://www.webasp.net/article/17/16957.htm) |
| -- 作者:未知 -- 發佈日期: 2005-03-11 |
| 在項目開發中發現對php的文檔缺少管理,別人寫了一個,功能不多
<?php /** * 類名: doc * 描述: 文檔生成類 * 其他: 可以對目錄進行過濾,設置好源目錄後,請用絕對路徑指定生成目錄,模式可調,模式 * 1為常規類型,即以 斜線**開頭,以*斜線 結束 * 2為擴展類型,凡是 斜線*開頭以*斜線 結束的部分都將成為文檔的一部分 */ class doc { var $docdirname; var $docdir; /** * 函數名稱: doc() * 函數功能: 構造 * 輸入參數: none * 函數返回值: 返回值說明 * 其它說明: 2004-10-13 */ function doc() { $this->docdirname = "doc/"; } /** * 函數名稱: createDoc($root,$newdir,$mode="1",$filter=null) * 函數功能: 創建文檔 * 輸入參數: $root -------------- 源目錄 $newdir ----------- 目標目錄 $mode ------------- 模式,1為普通,2為擴展 $filter ------------ 過濾目錄 * 函數返回值: 返回值說明 * 其它說明: 2004-10-13 */ function createDoc($root,$newdir,$mode="1",$filter=null) { $getarr = $this->loopDir($root,$filter); $i = 0; $this->createFrame($newdir); foreach($getarr as $key=>$val) { if($this->getPhpFiles($val)) { $content = $this->getContent($val); $content = $this->getDoc($content,$mode); $filepath = $this->setFilepath($val,$root,$newdir); $filedir = $this->getFileDir($filepath); $this->mkdirs($filedir); $this->setDoc($filepath,$content); $data[$i]['url'] = "$filepath"; $data[$i]['name'] = "$val"; $i++; } } if(!empty($data)) { $this->createMenu($newdir,$data); $this->redirect($this->docdir); } } /** * 函數名稱: redirect($path) * 函數功能: 轉向 * 輸入參數: $path ---------------- 轉向路徑 * 函數返回值: 返回值說明 * 其它說明: 2004-10-13 */ function redirect($path) { echo "<a href=".$path." target='_blank'>生成文檔成功,點擊此處查看</a>"; } /** * 函數名稱: loopDir($root,$filter=null) * 函數功能: 遍歷目錄 * 輸入參數: $root ------------------- 源目錄 $filter ----------------- 過濾 * 函數返回值: array * 其它說明: 2004-10-13 */ function loopDir($root,$filter=null) { static $getarr=array(); $d = dir($root); while (false !== ($entry = $d->read())) { if ($entry == "." || $entry == "..") { continue; } if($this->filter($entry,$filter)) { if(is_dir($root.$entry)) { $this->loopDir($d->path.$entry."/"); } else { $getarr[] = $d->path.$entry; } } } $d->close(); Return $getarr; } /** * 函數名稱: getPhpFiles($path) * 函數功能: 提取php文檔 * 輸入參數: $path ---------------- 文檔路徑 * 函數返回值: bool * 其它說明: 2004-10-13 */ function getPhpFiles($path) { $type = preg_replace('/.*\.(.*[^\.].*)/i','\\1',$path); $type = strtolower($type); if($type=="php") { Return true; } else { Return false; } } /** * 函數名稱: getContent($path) * 函數功能: 讀取文件內容 * 輸入參數: $path ------------------- 文件路徑 * 函數返回值: string * 其它說明: 2004-10-13 */ function getContent($path) { $fp = file($path); $content = implode('',$fp); Return $content; } /** * 函數名稱: getDoc($content,$mode="1") * 函數功能: 取出php文件中的註釋 * 輸入參數: $content ------------ 文檔內容 $mode --------------- 模式,1為普通,2為擴展 * 函數返回值: string * 其它說明: 2004-10-13 */ function getDoc($content,$mode="1") { switch($mode) { case '1': $pattern = '/\/(\*)[\r\n].*\*\//isU'; break; case '2': $pattern = '/\/\*.*\*\//isU'; break; } preg_match_all($pattern,$content,$carr); $getarr = array(); foreach($carr[0] as $key=>$val) { $getarr[] = trim($val); } $str = implode("<br><br>",$getarr); $str = preg_replace('/[\r]/i','<br>',$str); $style = $this->getStyle(); $str = $this->getTable($str); $str = $style.$str; Return $str; } /** * 函數名稱: etFilepath($filepath,$oldroot,$newroot) * 函數功能: 設置生成文件的路徑 * 輸入參數: $filepath -------------- 源文件路徑 $oldroot -------------- 源目錄路徑 $newroot -------------- 目標目錄路徑 * 函數返回值: string * 其它說明: 2004-10-13 */ function setFilepath($filepath,$oldroot,$newroot) { $oldroot = str_replace('/',"\\/",$oldroot); $pattern = "/".$oldroot."(.*)/iU"; $filepath = preg_replace($pattern,'\\1',$filepath); $newpath = $newroot.$this->docdirname.$filepath;//echo "$newpath<br>"; $newpath = preg_replace('/(.*\.)(.*[^\.].*)/i','\\1htm',$newpath); Return $newpath; } /** * 函數名稱: getFileDir($path) * 函數功能: 取得文檔目錄 * 輸入參數: $path ------------- 文檔路徑 * 函數返回值: string * 其它說明: 2004-10-13 */ function getFileDir($path) { $getpath = preg_replace('/(.*)(\/.*[^\.].*)/i','\\1',$path); Return $getpath; } /** * 函數名稱: setDoc * 函數功能: 將註釋寫入指定目錄並生成頁面 * 輸入參數: $filepath --------------- 目錄路徑 $content ---------------- 寫入的內容 * 函數返回值: 返回值說明 * 其它說明: 說明 */ function setDoc($filepath,$content) { $fp = fopen($filepath,"w+"); flock($fp,LOCK_EX); fwrite($fp,$content); flock($fp, LOCK_UN); } /** * 函數名稱: mkdirs($path) * 函數功能: 創建目錄 * 輸入參數: $path ------------------- 路徑 * 函數返回值: none * 其它說明: 2004-10-13 */ function mkdirs($path) { $adir = explode('/',$path); $dirlist = ''; $rootdir = $adir[0]; array_shift ($adir); foreach($adir as $key=>$val) { if($val!='.'&&$val!='..') { $dirlist .= "/".$val; $dirpath = $rootdir.$dirlist; if(!file_exists($dirpath)&&!is_file($dirpath)) { mkdir($dirpath); chmod($dirpath,0777); } } } } /** * 函數名稱: filter($item,$arr=null) * 函數功能: 過濾 * 輸入參數: $item -------------- 內容 $arr --------------- 過濾項 * 函數返回值: bool * 其它說明: 2004-10-13 */ function filter($item,$arr=null) { $item = strtolower($item); $filter = explode(',',$arr); if($arr==null||!in_array($item,$filter)) { Return true; } else { Return false; } } /** * 函數名稱: createFrame($root) * 函數功能: 生成框架頁 * 輸入參數: $root --------------- 首頁的存放目錄 * 函數返回值: str * 其它說明: 2004-10-13 */ function createFrame($root) { $str = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>無標題文檔</title> </head> <frameset cols="150,*" frameborder="YES" border="10" framespacing="5" bordercolor="#003366"> <frame src="menu.htm" name="leftFrame" framespacing="5" frameborder="auto" border="5" bordercolor="#f5f5f5" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" > <frame src="#" name="mainFrame"> </frameset> <noframes><body> </body></noframes> </html>'; $this->docdir = $root."index.htm"; $this->setDoc($this->docdir,$str); } /** * 函數名稱: createMenu($root,$data) * 函數功能: 生成菜單 * 輸入參數: $root ------------------- 頁面存入目錄 $data ------------------- 內容 * 函數返回值: string * 其它說明: 2004-10-13 */ function createMenu($root,$data) { $path = $root."menu.htm"; $str = $this->getStyle(); $str.= "<table>"; foreach($data as $key=>$val) { $str.= "<tr><td><a href='".$val['url']."' target='mainFrame'>".$val['name']."</a></td></tr>"; } $str.= "</table>"; $this->setDoc($path,$str); } /** * 函數名稱: getStyle() * 函數功能: 樣式 * 輸入參數: none * 函數返回值: string * 其它說明: 2004-10-13 */ function getStyle() { $str = ' <style> table { font-family: "Courier New","細明體"; border-collapse: collapse; word-break:break-all; } td { font-family: "Courier New","細明體"; font-size: 12px; line-height: 22px; } </style>'; Return $str; } /** * 函數名稱: getTable($content) * 函數功能: 把內容放入table中 * 輸入參數: $content ------------ 內容 * 函數返回值: string * 其它說明: 2004-10-13 */ function getTable($content) { $str = "<table width=\"100%\" border=\"1\" bordercolor=\"#dbdbdb\" cellpadding=\"5\" cellspacing=\"0\"> <tr> <td bgcolor=\"#f5f5f5\">".$content."</td> </tr> </table>"; Return $str; } } // 使用 $d = new doc; $filter = "adodb,smarty,cvs,templates,templates_c"; $d->createDoc("e:/www/kpub20/class/","e:/www/test/aaa/",1,$filter); ?> |
| webasp.net |