php api文檔產生類

來源:互聯網
上載者:User
a小巧的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 " }  /**  * 函數名稱: 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("

",$getarr); $str = preg_replace('/[\r]/i','
',$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
"; $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 = ' 無標題文檔 <body> </body> '; $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.= " "; foreach($data as $key=>$val) { $str.= "
} $str.= "
"; $this->setDoc($path,$str); } /** * 函數名稱: getStyle() * 函數功能: 樣式 * 輸入參數: none * 函數傳回值: string * 其它說明: 2004-10-13 */ function getStyle() { $str = ' '; Return $str; } /** * 函數名稱: getTable($content) * 函數功能: 把內容放入table中 * 輸入參數: $content ------------ 內容 * 函數傳回值: string * 其它說明: 2004-10-13 */ function getTable($content) { $str = "
".$content."
"; Return $str; } } // 使用 $d = new doc; $filter = "adodb,smarty,cvs,templates,templates_c"; $d->createDoc("e:/www/kpub20/class/","e:/www/test/aaa/",1,$filter);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.