Debian Codegraph @

codelitephp/php-plugin/doc-gen.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This script can be used for generating PHP model for PDT.
5 * It builds PHP functions according to the loaded extensions in running PHP,
6 * using complementary information gathered from PHP.net documentation
7 *
8 * @author Michael Spector <[email protected]>
9 */
10
11 /**
12 * Usage:
13 * ===========================
14 * Checkout phpdoc from here:
15 * svn checkout https://svn.php.net/repository/phpdoc/modules/doc-en phpdoc/
16 * cd phpdoc
17 * php doc-gen.php /path/to/phpdoc/
18 */
19 if(version_compare(phpversion(), "5.0.0")< 0){
20 die("This script requires PHP 5.0.0 or higher!\n");
21 }
22 $splitFiles = true;
23 $phpdocDir = null;
24 $phpDir = "php5";
25 if(strstr(phpversion(), "5.3")){
26 $phpDir = "php5.3";
27 }
28 // Parse arguments:
29 $argv = $_SERVER["argv"];
30 $argv0 = array_shift($argv);
31 for($i = 0;
32 $i < count ($argv );
33 ++ $i ){
34 switch($argv[$i]){
35 case "-nosplit" : $splitFiles = false;
36 break;
37 case "-help" : show_help();
38 break;
39 default : $phpdocDir = $argv[$i];
40 }
41 }
42 if(!$phpdocDir){
43 show_help();
44 }
45 $functionsDoc = parse_phpdoc_functions($phpdocDir);
46 $classesDoc = parse_phpdoc_classes($phpdocDir);
47 $constantsDoc = parse_phpdoc_constants($phpdocDir);
48 $processedFunctions = array();
49 $processedClasses = array();
50 $processedConstants = array();
51 @ mkdir($phpDir);
52 if(!$splitFiles){
53 begin_file_output();
54 }
55 $extensions = get_loaded_extensions();
56
57 foreach($extensions as $extName){
58 if($splitFiles){
59 begin_file_output();
60 }
61 print_extension(new ReflectionExtension($extName));
62 if($splitFiles){
63 finish_file_output("{$phpDir}/{$extName}.php");
64 }
65 }
66 if($splitFiles){
67 begin_file_output();
68 }
69 $intFunctions = get_defined_functions();
70
71 foreach($intFunctions["internal"] as $intFunction){
72 if(!@ $processedFunctions[strtolower($intFunction)]){
73 print_function(new ReflectionFunction($intFunction), false);
74 }
75 }
76 $intClasses = array_merge(get_declared_classes(), get_declared_interfaces());
77
78 foreach($intClasses as $intClass){
79 if(!@ $processedClasses[strtolower($intClass)]){
80 print_class(new ReflectionClass($intClass));
81 }
82 }
83 print "\n";
84 $constants = get_defined_constants(true);
85 $intConstants = $constants["internal"];
86 // add magic constants:
87 $intConstants['__FILE__'] = null;
88 $intConstants['__LINE__'] = null;
89 $intConstants['__CLASS__'] = null;
90 $intConstants['__FUNCTION__'] = null;
91 $intConstants['__METHOD__'] = null;
92 if(version_compare(phpversion(), "5.3.0")>= 0){
93 $intConstants['__DIR__'] = null;
94 $intConstants['__NAMESPACE__'] = null;
95 }
96
97 foreach($intConstants as $name => $value){
98 if(!@ $processedConstants[$name]){
99 print_constant($name, $value);
100 }
101 }
102 finish_file_output("{$phpDir}/basic.php");
103 // Create .list file
104 $fp = fopen("{$phpDir}/.list", "w");
105
106 foreach(glob("{$phpDir}/*.php")as $f){
107 fwrite($fp, basename($f));
108 fwrite($fp, "\n");
109 }
110 fclose($fp);
111 echo 'Finished...';
112 // === Functions ===
113
114 /**
115 * Makes generic key from given function name
116 * @param name string Function name
117 * @return string generic key
118 */
119 function make_funckey_from_str($name){
120 $name = str_replace("->", "::", $name);
121 $name = str_replace("()", "", $name);
122 $name = strtolower($name);
123 return $name;
124 }
125
126 /**
127 * Replaces all invalid charaters with '_' in PHP identifier
128 * @param name PHP identifier
129 * @return string PHP identifier with stripped invalid characters
130 */
131 function clean_php_identifier($name){
132 $name = preg_replace('/[^\$\w\_]+/', '_', $name);
133 return $name;
134 }
135
136 /**
137 * Makes generic key from given function reference
138 * @param name ReflectionMethod function reference
139 * @return string generic key
140 */
141 function make_funckey_from_ref($ref){
142 if($ref instanceof ReflectionMethod){
143 $funckey = strtolower($ref->getDeclaringClass()->getName()). "::" . strtolower($ref->getName());
144 } else {
145 $funckey = strtolower($ref->getName());
146 }
147 return $funckey;
148 }
149
150 /**
151 * Parses PHP documentation
152 * @param phpdocDir string PHP.net documentation directory
153 * @return array Function information gathered from the PHP.net documentation by parsing XML files
154 */
155 function parse_phpdoc_functions($phpdocDir){
156 $xml_files = array_merge(glob("{$phpdocDir}/en/reference/*/*/*.xml"));
157
158 foreach($xml_files as $xml_file){
159 $xml = file_get_contents($xml_file);
160 if(preg_match('@<refentry.*?xml:id=["\'](.*?)["\'].*?>.*?<refname>(.*?)</refname>.*?<refpurpose>(.*?)</refpurpose>@s', $xml, $match)){
161 $refname = make_funckey_from_str($match[2]);
162 $functionsDoc[$refname]['id'] = $match[1];
163 $functionsDoc[$refname]['quickref'] = trim($match[3]);
164 if(preg_match('@<refsect1\s+role=["\']description["\']>(.*?)</refsect1>@s', $xml, $match)){
165 $description = $match[1];
166 $function_alias = null;
167 $parameters = null;
168 $has_object_style = false;
169 if(preg_match('@^(.*?)<classsynopsis>.*?<classname>(.*)</classname>.*?<methodsynopsis>.*?<type>(.*?)</type>.*?<methodname>(.*?)</methodname>(.*?)</methodsynopsis>.*?</classsynopsis>(.*)$@s', $description, $match)){
170 $functionsDoc[$refname]['classname'] = trim($match[2]);
171 $functionsDoc[$refname]['returntype'] = trim($match[3]);
172 $functionsDoc[$refname]['methodname'] = trim($match[4]);
173 $parameters = $match[5];
174 $description = $match[1] . $match[6];
175 $has_object_style = true;
176 }
177 if(preg_match('@^(.*?)<classsynopsis>.*?<classname>(.*)</classname>.*?<constructorsynopsis>.*?<methodname>(.*?)</methodname>(.*?)</constructorsynopsis>.*?</classsynopsis>(.*)$@s', $description, $match)){
178 $functionsDoc[$refname]['classname'] = trim($match[2]);
179 $functionsDoc[$refname]['methodname'] = trim($match[3]);
180 $parameters = $match[4];
181 $description = $match[1] . $match[5];
182 $has_object_style = true;
183 }
184 if(preg_match('@<methodsynopsis>.*?<type>(.*?)</type>.*?<methodname>(.*?)</methodname>(.*?)</methodsynopsis>@s', $description, $match)){
185 if($has_object_style){
186 $function_alias = trim($match[2]);
187 } else {
188 //For return type of simplexml_load_string and simplexml_load_file.
189 if(preg_match('@<refsect1\s+role=["\']returnvalues["\']>(.*?)</refsect1>@s', $xml, $match1)){
190 $returnvalues = $match1[1];
191 if(preg_match('@<type>object</type> of class <type>(.*?)</type>@s', $returnvalues, $match1)){
192 $functionsDoc[$refname]['returntype'] = trim($match1[1]);
193 } else {
194 $functionsDoc[$refname]['returntype'] = trim($match[1]);
195 }
196 } else {
197 $functionsDoc[$refname]['returntype'] = trim($match[1]);
198 }
199 $functionsDoc[$refname]['methodname'] = trim($match[2]);
200 $parameters = $match[3];
201 }
202 }
203 if($parameters){
204 if(preg_match_all('@<methodparam\s*(.*?)>.*?<type>(.*?)</type>.*?<parameter\s*(.*?)>(.*?)</parameter>.*?</methodparam>@s', $parameters, $match)){
205 for($i = 0;
206 $i < count ($match[0] );
207 ++ $i ){
208 $parameter = array('type' => trim($match[2][$i]), 'name' => clean_php_identifier(trim($match[4][$i])),);
209 if(preg_match('@choice=[\'"]opt[\'"]@', $match[1][$i])){
210 $parameter['isoptional'] = true;
211 }
212 if(preg_match('@role=[\'"]reference[\'"]@', $match[3][$i])){
213 $parameter['isreference'] = true;
214 }
215 $functionsDoc[$refname]['parameters'][] = $parameter;
216 }
217 }
218 }
219 }
220 if(preg_match('@<refsect1\s+role=["\']parameters["\']>(.*?)</refsect1>@s', $xml, $match)){
221 $parameters = $match[1];
222 if(preg_match_all('@<varlistentry\s*.*?>.*?<parameter>(.*?)</parameter>.*?<listitem\s*.*?>(.*?)</listitem>.*?</varlistentry>@s', $parameters, $match)){
223 for($i = 0;
224 $i < count ($match[0] );
225 $i ++ ){
226 for($j = 0;
227 $j < count (@ $functionsDoc[$refname]['parameters'] );
228 $j ++ ){
229 if($match[1][$i] == $functionsDoc[$refname]['parameters'][$j]['name']){
230 $functionsDoc[$refname]['parameters'][$j]['paramdoc'] = xml_to_phpdoc($match[2][$i]);
231 break;
232 }
233 }
234 }
235 }
236 }
237 if(preg_match('@<refsect1\s+role=["\']returnvalues["\']>(.*?)</refsect1>@s', $xml, $match)){
238 $returnvalues = $match[1];
239 if(preg_match('@<para>\s*(Returns)?(.*)</para>?@s', $returnvalues, $match)){
240 $functionsDoc[$refname]['returndoc'] = xml_to_phpdoc($match[2]);
241 }
242 }
243 // Create information for function alias
244 if($function_alias){
245 $functionsDoc[$function_alias] = $functionsDoc[$refname];
246 }
247 }
248 }
249 return $functionsDoc;
250 }
251
252 /**
253 * Parses PHP documentation
254 * @param phpdocDir string PHP.net documentation directory
255 * @return array Class information gathered from the PHP.net documentation by parsing XML files
256 */
257 function parse_phpdoc_classes($phpdocDir){
258 $xml_files = array_merge(glob("{$phpdocDir}/en/reference/*/reference.xml"), glob("{$phpdocDir}/reference/*/classes.xml"), glob("{$phpdocDir}/language/*/*.xml"), glob("{$phpdocDir}/language/*.xml"));
259 global $fields_doc;
260
261 foreach($xml_files as $xml_file){
262 $xml = file_get_contents($xml_file);
263 if(preg_match('@xml:id=["\'](.*?)["\']@', $xml, $match)){
264 $id = $match[1];
265 if(preg_match_all('@<title><classname>(.*?)</classname></title>@', $xml, $match)){
266 for($i = 0;
267 $i < count ($match[0] );
268 ++ $i ){
269 $class = $match[1][$i];
270 $refname = strtolower($class);
271 $classesDoc[$refname]['id'] = $id;
272 $classesDoc[$refname]['name'] = $class;
273 if(preg_match("@<title><classname>{$class}</classname></title>\s*<para>(.*?)</para>@s", $xml, $match2)){
274 $classesDoc[$refname]['doc'] = xml_to_phpdoc($match2[1]);
275 }
276 //pass over class fields here
277 $fields_xml_file = array_merge(glob("{$phpdocDir}/reference/*/" . $refname . ".xml"));
278 if($fields_xml_file[0] != null){
279 $xml_field_data = file_get_contents($fields_xml_file[0]);
280 if($xml_field_data != null){
281 if(preg_match_all('@<fieldsynopsis>((\w|\W|\s)*?)</fieldsynopsis>@', $xml_field_data, $fieldsynopsis_list)){
282
283 foreach($fieldsynopsis_list[1] as $fieldsynopsis){
284 if(preg_match_all("@<varname\s*linkend=\"{$refname}.props.(.*?)\">(.*?)</varname>@", $fieldsynopsis, $varname)){
285 $field_name = $varname[2][0];
286 $fields_doc[$refname][$field_name]['name'] = $field_name;
287 }
288 // <varlistentry xml:id="domdocument.props.formatoutput">
289 // <term><varname>formatOutput</varname></term>
290 // <listitem>
291 // <para>Nicely formats output with indentation and extra space.</para>
292 // </listitem>
293 // </varlistentry>
294 if(preg_match("@<varlistentry.*?<term><varname>{$field_name}</varname></term>.*?<para>(.*?)</para>@s", $xml_field_data, $doc)){
295 if(preg_match("@<emphasis>Deprecated</emphasis>@s", $doc[1], $deprecated)){
296 $fields_doc[$refname][$field_name]['deprecated'] = true;
297 } else {
298 $fields_doc[$refname][$field_name]['deprecated'] = false;
299 }
300 $fields_doc[$refname][$field_name]['doc'] = xml_to_phpdoc($doc[1]);
301 }
302 if(preg_match_all("@<modifier>(.*?)</modifier>@", $fieldsynopsis, $modifier_list)){
303
304 foreach($modifier_list[1] as $current_modifier){
305 if($current_modifier == "readonly"){
306 continue;
307 } else {
308 $modifier = $current_modifier;
309 break;
310 }
311 }
312 //go to the next field if not public ???
313 if($modifier == "private"){
314 continue;
315 }
316 $fields_doc[$refname][$field_name]['modifier'] = $modifier;
317 }
318 if(preg_match_all("@<type>(.*?)</type>@", $fieldsynopsis, $type)){
319 $field_type = $type[1][0];
320 $fields_doc[$refname][$field_name]['type'] = $field_type;
321 }
322 }
323 }
324 }
325 }
326 }
327 }
328 }
329 }
330 return $classesDoc;
331 }
332
333 /**
334 * Parses PHP documentation
335 * @param phpdocDir string PHP.net documentation directory
336 * @return array Constant information gathered from the PHP.net documentation by parsing XML files
337 */
338 function parse_phpdoc_constants($phpdocDir){
339 exec("find " . addslashes($phpdocDir). " -name \"*constants.xml\"", $xml_files);
340
341 foreach($xml_files as $xml_file){
342 $xml = file_get_contents($xml_file);
343 if(preg_match('@xml:id=["\'](.*?)["\']@', $xml, $match)){
344 $id = $match[1];
345 if(preg_match_all('@<term>\s*<constant>([a-zA-Z_][a-zA-Z0-9_]*)</constant>.*?</term>.*?<listitem>(.*?)</listitem>@s', $xml, $match)){
346 for($i = 0;
347 $i < count ($match[0] );
348 ++ $i ){
349 $constant = $match[1][$i];
350 $constantsDoc[$constant]['id'] = $id;
351 $constantsDoc[$constant]['doc'] = xml_to_phpdoc($match[2][$i]);
352 }
353 }
354 if(preg_match_all('@<entry>\s*<constant>([a-zA-Z_][a-zA-Z0-9_]*)</constant>.*?</entry>\s*<entry>\d+</entry>\s*<entry>(.*?)</entry>@s', $xml, $match)|| preg_match_all('@<entry>\s*<constant>([a-zA-Z_][a-zA-Z0-9_]*)</constant>.*?</entry>\s*<entry>(.*?)</entry>@s', $xml, $match)){
355 for($i = 0;
356 $i < count ($match[0] );
357 ++ $i ){
358 $constant = $match[1][$i];
359 $constantsDoc[$constant]['id'] = $id;
360 $constantsDoc[$constant]['doc'] = xml_to_phpdoc($match[2][$i]);
361 }
362 }
363 }
364 }
365 return $constantsDoc;
366 }
367
368 /**
369 * Prints ReflectionExtension in format of PHP code
370 * @param extRef ReflectionExtension object
371 */
372 function print_extension($extRef){
373 print "\n// Start of {$extRef->getName()} v.{$extRef->getVersion()}\n";
374 // process classes:
375 $classesRef = $extRef->getClasses();
376 if(count($classesRef)> 0){
377
378 foreach($classesRef as $classRef){
379 print_class($classRef);
380 }
381 }
382 // process functions
383 $funcsRef = $extRef->getFunctions();
384 if(count($funcsRef)> 0){
385
386 foreach($funcsRef as $funcRef){
387 print_function($funcRef, false);
388 }
389 print "\n";
390 }
391 // process constants
392 $constsRef = $extRef->getConstants();
393 if(count($constsRef)> 0){
394 print_constants($constsRef);
395 print "\n";
396 }
397 print "// End of {$extRef->getName()} v.{$extRef->getVersion()}\n";
398 }
399
400 /**
401 * Prints ReflectionClass in format of PHP code
402 * @param classRef ReflectionClass object
403 * @param tabs integer[optional] number of tabs for indentation
404 */
405 function print_class($classRef, $tabs = 0){
406 global $processedClasses;
407 $processedClasses[strtolower($classRef->getName())] = true;
408 print "\n";
409 print_doccomment($classRef, $tabs);
410 print_tabs($tabs);
411 if($classRef->isFinal())print "final ";
412 print $classRef->isInterface()? "interface " : "class ";
413 print clean_php_identifier($classRef->getName()). " ";
414 // print out parent class
415 $parentClassRef = $classRef->getParentClass();
416 if($parentClassRef){
417 print "extends {$parentClassRef->getName()} ";
418 }
419 // print out interfaces
420 $interfacesRef = $classRef->getInterfaces();
421 if(count($interfacesRef)> 0){
422 print $classRef->isInterface()? "extends " : "implements ";
423 $i = 0;
424
425 foreach($interfacesRef as $interfaceRef){
426 if($i ++ > 0){
427 print ", ";
428 }
429 print "{$interfaceRef->getName()}";
430 }
431 }
432 print " {\n";
433 // process constants
434 $constsRef = $classRef->getConstants();
435 if(count($constsRef)> 0){
436 print_class_constants($constsRef, $tabs + 1);
437 print "\n";
438 }
439 global $classesDoc;
440 // process properties
441 $propertiesRef = $classRef->getProperties();
442 if(count($propertiesRef)> 0){
443
444 foreach($propertiesRef as $propertyRef){
445 print_property($propertyRef, $tabs + 1);
446 $printedFields[$propertyRef->getName()] = true;
447 }
448 print "\n";
449 }
450 $className = strtolower($classRef->getName());
451 if($className == "DomDocument"){
452 echo "DomDocument";
453 }
454 global $fields_doc;
455 if(@ $fields_doc[$className]){
456 $fields = @ $fields_doc[$className];
457
458 foreach($fields as $field){
459 if(!$printedFields[$field['name']]){
460 //print doc here
461 print("\n");
462 $doc = $field['doc'];
463 if($doc){
464 print_tabs($tabs + 1);
465 print "/**\n";
466 print_tabs($tabs + 1);
467 print " * " . newline_to_phpdoc($doc, $tabs + 1). "\n";
468 print_tabs($tabs + 1);
469 print " * @var " . $field['type'] . "\n";
470 if($field['deprecated'] == true){
471 print_tabs($tabs + 1);
472 print " * @deprecated " . "\n";
473 }
474 print_Tabs($tabs + 1);
475 // http://www.php.net/manual/en/class.domdocument.php#domdocument.props.actualencoding
476 $refname = strtolower($classRef->getName());
477 $class_url = make_url("class." . $refname);
478 $field_name = strtolower($field['name']);
479 $field_url = $class_url . '#' . $className . ".props." . $field_name;
480 print " * @link {$field_url}\n";
481 print_tabs($tabs + 1);
482 print " */\n";
483 }
484 print_tabs($tabs + 1);
485 print implode(' ', array($field['modifier']));
486 print " ";
487 print "\${$field['name']};\n";
488 }
489 }
490 }
491 // process methods
492 $methodsRef = $classRef->getMethods();
493 if(count($methodsRef)> 0){
494
495 foreach($methodsRef as $methodRef){
496 print_function($methodRef, $classRef->isInterface(), $tabs + 1);
497 }
498 print "\n";
499 }
500 print_tabs($tabs);
501 print "}\n";
502 }
503
504 /**
505 * Prints ReflectionProperty in format of PHP code
506 * @param propertyRef ReflectionProperty object
507 * @param tabs integer[optional] number of tabs for indentation
508 */
509 function print_property($propertyRef, $tabs = 0){
510 print_doccomment($propertyRef, $tabs);
511 print_tabs($tabs);
512 print_modifiers($propertyRef);
513 print "\${$propertyRef->getName()};\n";
514 }
515
516 function print_function($functionRef, $childOfInterface, $tabs = 0){
517 global $functionsDoc;
518 global $processedFunctions;
519 $funckey = make_funckey_from_ref($functionRef);
520 $processedFunctions[$funckey] = true;
521 print "\n";
522 print_doccomment($functionRef, $tabs);
523 print_tabs($tabs);
524 if(!($functionRef instanceof ReflectionFunction)){
525 print_modifiers($functionRef);
526 }
527 print "function ";
528 if($functionRef->returnsReference()){
529 print "&";
530 }
531 print "{$functionRef->getName()} (";
532 $parameters = @ $functionsDoc[$funckey]['parameters'];
533 if($parameters){
534 print_parameters($parameters);
535 } else {
536 print_parameters_ref($functionRef->getParameters());
537 }
538 print ") ";
539
540 if($childOfInterface) {
541 print ";\n";
542 } else {
543 print "{}\n";
544 }
545 }
546
547 /**
548 * Prints ReflectionParameter in format of PHP code
549 * @param parameters array information from PHP.net documentation
550 */
551 function print_parameters($parameters){
552 $i = 0;
553
554 foreach($parameters as $parameter){
555 if($parameter['name'] != "..."){
556 if($i ++ > 0){
557 print ", ";
558 }
559 $type = $parameter['type'];
560 if($type &&(class_exists($type)|| $type == "array")){
561 print "{$type} ";
562 }
563 if(@ $parameter['isreference']){
564 print "&";
565 }
566 print "\${$parameter['name']}";
567 if(@ $parameter['isoptional']){
568 if(@ $parameter['defaultvalue']){
569 $value = $parameter['defaultvalue'];
570 if(!is_numeric($value)){
571 $value = "'{$value}'";
572 }
573 print " = {$value}";
574 } else {
575 print " = null";
576 }
577 }
578 }
579 }
580 }
581
582 /**
583 * Prints ReflectionParameter in format of PHP code
584 * @param paramsRef ReflectionParameter[] array of objects
585 */
586 function print_parameters_ref($paramsRef){
587 $i = 0;
588
589 foreach($paramsRef as $paramRef){
590 if($paramRef->isArray()){
591 print "array ";
592 } else {
593 if($className = get_parameter_classname($paramRef)){
594 print "{$className} ";
595 }
596 }
597 $name = $paramRef->getName()? $paramRef->getName(): "var" .($i + 1);
598 if($name != "..."){
599 if($i ++ > 0){
600 print ", ";
601 }
602 if($paramRef->isPassedByReference()){
603 print "&";
604 }
605 print "\${$name}";
606 if($paramRef->allowsNull()){
607 print " = null";
608 } else if($paramRef->isDefaultValueAvailable()){
609 $value = $paramRef->getDefaultValue();
610 if(!is_numeric($value)){
611 $value = "'{$value}'";
612 }
613 print " = {$value}";
614 }
615 }
616 }
617 }
618
619 /**
620 * Prints constants in format of PHP code
621 * @param constants array containing constants, where key is a name of constant
622 * @param tabs integer[optional] number of tabs for indentation
623 */
624 function print_constants($constants, $tabs = 0){
625
626 foreach($constants as $name => $value){
627 print_constant($name, $value, $tabs);
628 }
629 }
630
631 function print_constant($name, $value = null, $tabs = 0){
632 global $constantsDoc;
633 global $processedConstants;
634 $processedConstants[$name] = true;
635 if($value === null){
636 $value = @ constant($name);
637 }
638 $value = escape_const_value($value);
639 $doc = @ $constantsDoc[$name]['doc'];
640 if($doc){
641 print "\n";
642 print_tabs($tabs);
643 print "/**\n";
644 print_tabs($tabs);
645 print " * " . newline_to_phpdoc($doc, $tabs). "\n";
646 print_tabs($tabs);
647 print " * @link " . make_url($constantsDoc[$name]['id']). "\n";
648 print_tabs($tabs);
649 print " */\n";
650 }
651 print_tabs($tabs);
652 print "define ('{$name}', {$value});\n";
653 }
654
655 function escape_const_value($value){
656 if(is_resource($value)){
657 $value = "\"${value}\"";
658 } else if(!is_numeric($value)&& !is_bool($value)&& $value !== null){
659 if($value === '\\'){
660 $value = '"' . addcslashes($value, "\\\"\r\n\t"). '"';
661 } else {
662 $value = '"' . addcslashes($value, "\"\r\n\t"). '"';
663 }
664 } else if($value === null){
665 $value = "null";
666 } else if($value === false){
667 $value = "false";
668 } else if($value === true){
669 $value = "true";
670 }
671 return $value;
672 }
673
674 /**
675 * Prints class constants in format of PHP code
676 * @param constants array containing constants, where key is a name of constant
677 * @param tabs integer[optional] number of tabs for indentation
678 */
679 function print_class_constants($constants, $tabs = 0){
680
681 foreach($constants as $name => $value){
682 $value = escape_const_value($value);
683 print_tabs($tabs);
684 print "const {$name} = {$value};\n";
685 }
686 }
687
688 /**
689 * Prints modifiers of reflection object in format of PHP code
690 * @param ref Reflection some reflection object
691 */
692 function print_modifiers($ref){
693 $modifiers = Reflection::getModifierNames($ref->getModifiers());
694 if(count($modifiers)> 0){
695 print implode(' ', $modifiers);
696 print " ";
697 }
698 }
699
700 /**
701 * Makes PHP Manual URL from the given ID
702 * @param id PHP Element ID
703 * @return URL
704 */
705 function make_url($id){
706 return "http://www.php.net/manual/en/{$id}.php";
707 }
708
709 /**
710 * Prints PHPDOC comment before specified reflection object
711 * @param ref Reflection some reflection object
712 * @param tabs integer[optional] number of tabs for indentation
713 */
714 function print_doccomment($ref, $tabs = 0){
715 global $functionsDoc;
716 global $classesDoc;
717 $docComment = $ref->getDocComment();
718 if($docComment){
719 print_tabs($tabs);
720 print "{$docComment}\n";
721 } else if($ref instanceof ReflectionClass){
722 $refname = strtolower($ref->getName());
723 if(@ $classesDoc[$refname]){
724 print_tabs($tabs);
725 print "/**\n";
726 $doc = @ $classesDoc[$refname]['doc'];
727 if($doc){
728 $doc = newline_to_phpdoc($doc, $tabs);
729 print_tabs($tabs);
730 print " * {$doc}\n";
731 }
732 if(@ $classesDoc[$refname]['id']){
733 print_Tabs($tabs);
734 $url = make_url("class." . $refname);
735 print " * @link {$url}\n";
736 }
737 print_tabs($tabs);
738 print " */\n";
739 }
740 } else if($ref instanceof ReflectionFunctionAbstract){
741 $funckey = make_funckey_from_ref($ref);
742 $returntype = @ $functionsDoc[$funckey]['returntype'];
743 $desc = @ $functionsDoc[$funckey]['quickref'];
744 $returndoc = newline_to_phpdoc(@ $functionsDoc[$funckey]['returndoc'], $tabs);
745 $paramsRef = $ref->getParameters();
746 $parameters = @ $functionsDoc[$funckey]['parameters'];
747 if($desc || count($paramsRef)> 0 || $parameters || $returntype){
748 print_tabs($tabs);
749 print "/**\n";
750 if($desc){
751 print_tabs($tabs);
752 print " * {$desc}\n";
753 }
754 if(@ $functionsDoc[$funckey]['id']){
755 print_tabs($tabs);
756 $url = make_url($functionsDoc[$funckey]['id']);
757 print " * @link {$url}\n";
758 }
759 if($parameters){
760
761 foreach($parameters as $parameter){
762 print_tabs($tabs);
763 print " * @param {$parameter['name']} {$parameter['type']}";
764 if(@ $parameter['isoptional']){
765 print "[optional]";
766 }
767 $paramdoc = newline_to_phpdoc(@ $parameter['paramdoc'], $tabs);
768 print " {$paramdoc}";
769 print "\n";
770 }
771 } else {
772 $i = 0;
773
774 foreach($paramsRef as $paramRef){
775 print_tabs($tabs);
776 $name = $paramRef->getName()? $paramRef->getName(): "var" . ++ $i;
777 print " * @param {$name}";
778 if($className = get_parameter_classname($paramRef)){
779 print " {$className}";
780 if($paramRef->isArray()){
781 print "[]";
782 }
783 }
784 if($paramRef->isOptional()){
785 print "[optional]";
786 }
787 print "\n";
788 }
789 }
790 if($returntype){
791 print_tabs($tabs);
792 print " * @return {$returntype} {$returndoc}\n";
793 }
794 print_tabs($tabs);
795 print " */\n";
796 }
797 } else if($ref instanceof ReflectionProperty){
798 //TODO complete phpdoc for fields detected by reflection
799 }
800 }
801
802 /**
803 * Converts XML entities to human readable string for PHPDOC
804 * @param str string
805 * @return string
806 */
807 function xml_to_phpdoc($str){
808 $str = str_replace("&return.success;", "Returns true on success, false on failure.", $str);
809 $str = str_replace("&return.void;", "", $str);
810 $str = str_replace("&true;", "true", $str);
811 $str = str_replace("&false;", "false", $str);
812 $str = str_replace("&lt;", "<", $str);
813 $str = str_replace("&gt;", ">", $str);
814 $str = str_replace("&amp;", "&", $str);
815 $str = strip_tags_special($str);
816 //$str = preg_replace ("/ */", " ", $str);
817 $str = preg_replace("/[\r\n][\t ]/", "\n", $str);
818 $str = trim($str);
819 return $str;
820 }
821
822 /**
823 * Converts newlines to PHPDOC prefixes in the given string
824 * @param str string
825 * @param tabs integer[optional] number of tabs for indentation
826 * @return string PHPDOC string
827 */
828 function newline_to_phpdoc($str, $tabs = 0){
829 $str = preg_replace("@[\r\n]+@", "\n" . str_repeat("\t", $tabs). " * ", $str);
830 return $str;
831 }
832
833 /**
834 * Prints specified number of tabs
835 * @param tabs integer number of tabs to print
836 */
837 function print_tabs($tabs){
838 print str_repeat("\t", $tabs);
839 }
840
841 /**
842 * Returns class name from given parameter reference, this method is a workaround
843 * for the case when exception is thrown from getClass() when such classname does not exist.
844 */
845 function get_parameter_classname(ReflectionParameter $paramRef){
846 try {
847 if($classRef = $paramRef->getClass()){
848 return $classRef->getName();
849 }
850 }
851 catch(Exception $e){
852 if(preg_match('/Class (\w+) does not exist/', $e->getMessage(), $matches)){
853 return $matches[1];
854 }
855 }
856 return null;
857 }
858
859 /**
860 * Starts outputing to the new file
861 */
862 function begin_file_output(){
863 ob_start();
864 print "<?php\n";
865 }
866
867 /**
868 * Ends outputing, and dumps the output to the specified file
869 * @param filename File to dump the output
870 */
871 function finish_file_output($filename){
872 //if (file_exists ($filename)) {
873 // rename ($filename, "{$filename}.bak");
874 //}
875 print "?>\n";
876 file_put_contents($filename, ob_get_contents());
877 ob_end_clean();
878 }
879
880 /**
881 * Strips xml tags from the string like the standard strip_tags() function
882 * would do, but also translates some of the docbook tags (such as tables
883 * an paragraphs) to proper html tags
884 * @param str string
885 * @return string
886 */
887 function strip_tags_special($str){
888 // first mask and translate the tags to preseve
889 $str = preg_replace("/<(\/?)table>/", "###($1table)###", $str);
890 $str = str_replace("<row>", "###(tr valign=\"top\")###", $str);
891 $str = str_replace("</row>", "###(/tr)###", $str);
892 $str = preg_replace("/<(\/?)entry>/", "###($1td)###", $str);
893 $str = preg_replace("/<(\/?)para>/", "###($1p)###", $str);
894 // now strip the remaining tags
895 $str = strip_tags($str);
896 // and restore the translated ones
897 $str = str_replace("###(", "<", $str);
898 $str = str_replace(")###", ">", $str);
899 return $str;
900 }
901
902 /**
903 * Prints usage help to the screen, and exits from program
904 */
905 function show_help(){
906 global $argv0;
907 die(<<<EOF
908 USAGE: {
909 $argv0
910 } [options] <PHP.net documentation directory>
911
912 Where options are:
913
914 -help Show this help.
915 -split Split output to different files (one file per PHP extension).
916
917 EOF
918 );
919 }
920
921 ?>