PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/include/inc_ext/html2fpdf/source2doc.php

http://phpwcms.googlecode.com/
PHP | 202 lines | 131 code | 21 blank | 50 comment | 22 complexity | cc42112a5ae0486973ea7cf8f24ca811 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, ISC, MIT, LGPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. Copyright (C) 2004 Renato Coelho
  4. (PHP)Source 2 Doc v0.5.0
  5. This is a simple script created in order to update the HTML2FPDF page
  6. It should make a php class documentation
  7. LICENSE: Freeware.
  8. Lacks: html_decode and the likes
  9. Plans: make an independent table for each part?
  10. Usage:
  11. require_once('source2doc.php');
  12. echo source2doc('filename.php'); //Print doc info on browser
  13. HOW TO declare var types and HOW TO use @return and @desc: (//! is a one-line comment)
  14. var $name; //! type
  15. function name()
  16. {
  17. //! @return void
  18. //! @desc Say something in one line, but dont use tags or ';' here
  19. //! @desc Fale algo em uma linha, mas nao use tags ou ';' aqui
  20. ...}
  21. */
  22. function source2doc( $filename )
  23. {
  24. define( 'endl', "\n" );
  25. $classname = '';
  26. $extends = '';
  27. $file = fopen( $filename, "r" );
  28. $tamarquivo = filesize( $filename );
  29. $buffer = fread( $file, $tamarquivo );
  30. fclose( $file );
  31. // //
  32. // Remove all PHP comments
  33. // Leave only the special comments '//!'
  34. // //
  35. // Remove /* multi-line comments */
  36. $regexp = '|/\\*.*?\\*/|s';
  37. $buffer = preg_replace( $regexp, '', $buffer );
  38. // Remove // one line comments
  39. $regexp = '|//[^!].*|m';
  40. $buffer = preg_replace( $regexp, '', $buffer );
  41. // //
  42. // Get class name and what it extends (or not)
  43. // //
  44. $regexp = '|class\\s+?(\\S+)(\\s+?\\S+\\s+?(\\S+))?|mi';
  45. preg_match( $regexp, $buffer, $aux ); //one class per source file
  46. $classname = $aux[1];
  47. if ( !empty( $aux[3] ) ) $extends = $aux[3];
  48. else $extends = '';
  49. $html = '<b>CLASSNAME:</b> ' . $classname . '<br />' . endl;
  50. if ( $extends != '' ) $html .= '<b>EXTENDS:</b> ' . $extends . '<br />' . endl;
  51. $html .= '<table border="1" width="100%">' . endl;
  52. // //
  53. // Get constants from source code
  54. // //
  55. $html .= '<tr>' . endl;
  56. $html .= '<th bgcolor="#6191ff" colspan="2">' . endl;
  57. $html .= 'CONSTANTS' . endl;
  58. $html .= '</th>' . endl;
  59. $html .= '</tr>' . endl;
  60. $regexp = '/define[(](.*?);/si';
  61. preg_match_all( $regexp, $buffer, $const );
  62. $const = $const[0];
  63. for( $i = 0; $i < count( $const ) ; $i++ ) {
  64. $html .= '<tr>' . endl;
  65. $html .= '<td colspan="2">' . endl;
  66. $html .= '<font size=2>' . $const[$i] . '</font>' . endl;
  67. $html .= '</td>' . endl;
  68. $html .= '</tr>' . endl;
  69. }
  70. // //
  71. // Get imports from source code
  72. // //
  73. $html .= '<tr>' . endl;
  74. $html .= '<th bgcolor="#6191ff" colspan="2">' . endl;
  75. $html .= 'IMPORTS' . endl;
  76. $html .= '</th>' . endl;
  77. $html .= '</tr>' . endl;
  78. $regexp = '/((require|include)[(_].*?);/si';
  79. preg_match_all( $regexp, $buffer, $imports );
  80. $imports = $imports[0];
  81. for( $i = 0; $i < count( $imports ) ; $i++ ) {
  82. $html .= '<tr>' . endl;
  83. $html .= '<td colspan="2">' . endl;
  84. $html .= '<font size=2>' . $imports[$i] . '</font>' . endl;
  85. $html .= '</td>' . endl;
  86. $html .= '</tr>' . endl;
  87. }
  88. // //
  89. // Get attributes from class
  90. // //
  91. $html .= '<tr>' . endl;
  92. $html .= '<th bgcolor="#6191ff" colspan="2">' . endl;
  93. $html .= 'ATTRIBUTES' . endl;
  94. $html .= '</th>' . endl;
  95. $html .= '</tr>' . endl;
  96. $regexp = '|var\\s(.+);\\s*(//!\\s*?(\\S+))?|mi';
  97. preg_match_all( $regexp, $buffer, $atr );
  98. $vname = $atr[1];
  99. $vtype = $atr[3];
  100. if ( !empty( $vname ) ) {
  101. $html .= '<tr>' . endl;
  102. $html .= '<td align="center" width="10%" bgcolor="#bbbbbb">' . endl;
  103. $html .= 'TYPE' . endl;
  104. $html .= '</td>' . endl;
  105. $html .= '<td align="center" width="90%" bgcolor="#bbbbbb">' . endl;
  106. $html .= 'NAME' . endl;
  107. $html .= '</td>' . endl;
  108. $html .= '</tr>' . endl;
  109. }
  110. for( $i = 0; $i < count( $vname ) ; $i++ ) {
  111. $html .= '<tr>' . endl;
  112. $html .= '<td align="center">' . endl;
  113. if ( empty( $vtype[$i] ) ) $html .= '<font size=2><i>(???)</i></font>' . endl;
  114. else $html .= '<font size=2><i>(' . $vtype[$i] . ')</i></font>' . endl;
  115. $html .= '</td>' . endl;
  116. $html .= '<td>' . endl;
  117. $html .= '<font size=2><b>var</b> ' . $vname[$i] . ';</font>' . endl;
  118. $html .= '</td>' . endl;
  119. $html .= '</tr>' . endl;
  120. }
  121. // ///
  122. // Get class' methods
  123. // ///
  124. $html .= '<tr>' . endl;
  125. $html .= '<th bgcolor="#6191ff" colspan="2">' . endl;
  126. $html .= 'METHODS' . endl;
  127. $html .= '</th>' . endl;
  128. $html .= '</tr>' . endl;
  129. $regexp = '|function\\s([^)]*)[)].*?(//!.*?)*;|si';
  130. preg_match_all( $regexp, $buffer, $func );
  131. $funcname = $func[1];
  132. $funccomment = $func[0];
  133. for( $i = 0; $i < count( $funcname ) ; $i++ ) {
  134. $html .= '<tr>' . endl;
  135. $html .= '<td bgcolor="#33ff99" colspan="2">' . endl;
  136. $html .= '<font size=2><b>function</b> ' . $funcname[$i] . ')</font>' . endl;
  137. $html .= '</td>' . endl;
  138. $html .= '</tr>' . endl;
  139. $desc = '';
  140. $ret = '';
  141. $regexp = '|//!(.*)|mi';
  142. preg_match_all( $regexp, $funccomment[$i], $temp );
  143. $temp = $temp[1];
  144. if ( empty( $temp[0] ) ) continue;
  145. foreach( $temp as $val ) {
  146. if ( strstr( $val, '@desc' ) ) {
  147. $regexp = '|.*?@desc(.*)|si';
  148. preg_match( $regexp, $val, $temp2 );
  149. $desc = $temp2[1];
  150. } elseif ( strstr( $val, '@return' ) ) {
  151. $regexp = '|.*?@return(.*)|si';
  152. preg_match( $regexp, $val, $temp3 );
  153. $ret = $temp3[1];
  154. }
  155. }
  156. if ( $ret != '' or $desc != '' ) {
  157. $html .= '<tr>' . endl;
  158. // @return column
  159. $html .= '<td width="30%">' . endl;
  160. if ( $ret == '' ) $html .= '<font size=2><b>Return:</b> <i>?void?</i></font>' . endl;
  161. else $html .= '<font size=2><b>Return:</b> <i>' . trim( $ret ) . '</i></font>' . endl;
  162. $html .= '</td>' . endl;
  163. // @desc column
  164. $html .= '<td width="70%">' . endl;
  165. if ( $desc == '' ) $html .= '<font size=2><b>OBS:</b> </font>' . endl;
  166. else $html .= '<font size=2><b>OBS:</b> ' . trim( $desc ) . '</font>' . endl;
  167. $html .= '</td>' . endl;
  168. $html .= '</tr>' . endl;
  169. }
  170. }
  171. // ///
  172. $html .= '</table>';
  173. return $html;
  174. }
  175. ?>