Small PHP Document Generation class

Source: Internet
Author: User
Tags filter foreach array bool explode flock php file string
The lack of management of PHP documents found in project development, others write a, the function is not much

<?php
/**
* Class Name: Doc
* Description: Document Generation class
* Other: You can filter the directory, set the source directory, please use the absolute path to specify the build directory, mode adjustable, mode
* 1 is the normal type, that is, the beginning with a slash *, ending with a * slash
* 2 is an extended type, the part of the general slash * at the end of the * slash will become part of the document
*/
Class doc
{
var $docdirname;
var $docdir;

/**
* Function name: Doc ()
* Function Function: Construction
* Input Parameters: None
* Function return Value: Return value description
* Other Notes: 2004-10-13
*/
function doc ()
{
$this->docdirname = "doc/";
}

/**
* Function Name: Createdoc ($root, $newdir, $mode = "1", $filter =null)
* Function function: Create document
* Input parameters: $root--------------Source Directory
$newdir-----------Target Directory
$mode-------------mode, 1 is normal, 2 is extended
$filter------------Filter Directory
* Function return Value: Return value description
* Other Notes: 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);
}
}

/**
* Function Name: Redirect ($path)
* Function function: Steering
* Input Parameters: $path----------------Steering path
* Function return Value: Return value description
* Other Notes: 2004-10-13
*/
function redirect ($path)
{
echo "<a href=". $path. "target= ' _blank ' > Generate document Success, click here to view </a>";
}

/**
* Function Name: Loopdir ($root, $filter =null)
* Function function: Traverse directory
* Input parameters: $root-------------------Source Directory
$filter-----------------Filtration
* Function return Value: Array
* Other Notes: 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;
}

/**
* Function Name: Getphpfiles ($path)
* Function function: Extract PHP document
* Input Parameters: $path----------------Document Path
* Function return value: BOOL
* Other Notes: 2004-10-13
*/
function Getphpfiles ($path)
{
$type = Preg_replace ('/.*\. *[^\.]. *)/I ', ' \\1 ', $path);
$type = Strtolower ($type);
if ($type = = "php")
{
return true;
}
Else
{
return false;
}
}

/**
* Function Name: getcontent ($path)
* Function Function: Read the contents of the file
* Input Parameters: $path-------------------file path
* Function return Value: string
* Other Notes: 2004-10-13
*/
function GetContent ($path)
{
$fp = file ($path);
$content = Implode (", $fp);
return $content;
}

/**
* Function Name: Getdoc ($content, $mode = "1")
* Function Function: Remove the comments in the PHP file
* Input Parameters: $content------------Document Contents
$mode---------------mode, 1 is normal, 2 is extended
* Function return Value: string
* Other Notes: 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;
}

/**
* Function Name: Etfilepath ($filepath, $oldroot, $newroot)
* Function function: Set the path of the build file
* Input parameter: $filepath--------------source file path
$oldroot--------------Source directory path
$newroot--------------Destination directory path
* Function return Value: string
* Other Notes: 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;
}

/**
* Function Name: Getfiledir ($path)
* Function function: Get the document Directory
* Input Parameters: $path-------------Document Path
* Function return Value: string
* Other Notes: 2004-10-13
*/
function Getfiledir ($path)
{
$getpath = Preg_replace ('/(. *) (\/.*[^\.]. *)/I ', ' \\1 ', $path);
return $getpath;
}

/**
* Function Name: Setdoc
* Function function: Write notes to the specified directory and generate pages
* Input parameters: $filepath---------------directory path
What is written $content----------------
* Function return Value: Return value description
* Other Description: Description
*/
function Setdoc ($filepath, $content)
{
$fp = fopen ($filepath, "w+");
Flock ($FP, LOCK_EX);
Fwrite ($fp, $content);
Flock ($FP, lock_un);
}

/**
* Function Name: mkdirs ($path)
* Function function: Create a table of contents
* Input parameters: $path-------------------Path
* Function return value: None
* Other Notes: 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);
}
}
}
}

/**
* Function Name: Filter ($item, $arr =null)
* Function Function: Filter
* Input parameters: $item--------------Content
$arr---------------Filter Items
* Function return value: BOOL
* Other Notes: 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;
}
}

/**
* Function Name: createframe ($root)
* Function function: Build frames page
* Input Parameters: $root---------------The first page of the storage directory
* Function return value: str
* Other Notes: 2004-10-13
*/
function Createframe ($root)
{
$str = '
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Untitled Document </title>

<frameset cols= "150,*" frameborder= "YES" border= "Ten" 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>
$this->docdir = $root. " Index.htm ";
$this->setdoc ($this->docdir, $STR);
}

/**
* Function Name: CreateMenu ($root, $data)
* Function function: Generate menu
* Input parameters: $root-------------------page into the directory
$data-------------------Content
* Function return Value: string
* Other Notes: 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);
}

/**
* Function Name: GetStyle ()
* Function function: Style
* Input Parameters: None
* Function return Value: string
* Other Notes: 2004-10-13
*/
function GetStyle ()
{
$str = '
<style>
Table {
font-family: "Courier New", "Song Body";
Border-collapse:collapse;
Word-break:break-all;
}
TD {
font-family: "Courier New", "Song Body";
font-size:12px;
line-height:22px;
}
</style> ';
return $str;
}

/**
* Function Name: gettable ($content)
* Function function: Put the content into the table
* Input parameters: $content------------Content
* Function return Value: string
* Other Notes: 2004-10-13
*/
function GetTable ($content)
{
$str = "<table width=\" 100%\ "border=\" 1\ "bordercolor=\" #dbdbdb \ "cellpadding=\" 5\ "cellspacing=\" 0\ ">
<tr>
&LT;TD bgcolor=\ "#f5f5f5 >". $content. " </td>
</tr>
</table> ";
return $str;
}
}

Use
$d = new Doc;
$filter = "Adodb,smarty,cvs,templates,templates_c";
$d->createdoc ("e:/www/kpub20/class/", "e:/www/test/aaa/", 1, $filter);
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.