6791aba35fb28500227ca108026f89a0f75b8c29
[m6w6/ext-http] / reflection2php.php
1 #!/usr/bin/env php
2 <?php
3
4 function m($m) {
5 $n = "";
6 foreach (Reflection::getModifierNames($m) as $mn) {
7 $n .= $mn . " ";
8 }
9 return $n;
10 }
11 function t($p) {
12 if ($c = $p->getClass()) return $c->getName() . " ";
13 if ($p->isArray()) return "array ";
14 }
15
16 if (!strlen($ext = $argv[1]))
17 die(sprintf("Usage: %s <ext>\n", $argv[0]));
18
19 $ext = new ReflectionExtension($ext);
20 foreach ($ext->getClasses() as $class) {
21
22 printf("%s%s %s ", m($class->getModifiers()), $class->isInterface() ? "interface":"class" ,$class->getName());
23 if ($p = $class->getParentClass()) {
24 printf("extends %s ", $p->getName());
25 }
26 if ($i = $class->getInterfaceNames()) {
27 printf("implements %s ", implode(", ", array_filter($i,function($v){return$v!="Traversable";})));
28 }
29 printf("\n{\n");
30
31 $_=0;
32 foreach ($class->getConstants() as $n => $v) {
33 $_+=printf("\tconst %s = %s;\n", $n, var_export($v, true));
34 }
35 $_ and printf("\n");
36 $_=0;
37 foreach ($class->getProperties() as $p) {
38 if ($p->getDeclaringClass()->getName() == $class->getName()) {
39 $_+=printf("\t%s\$%s;\n", m($p->getModifiers()), $p->getName());
40 }
41 }
42 $_ and printf("\n");
43
44 foreach ($class->getMethods() as $m) {
45 if ($m->getDeclaringClass()->getName() == $class->getName()) {
46 printf("\t%sfunction %s(", m($m->getModifiers()), $m->getName());
47 $ps = array();
48 foreach ($m->getParameters() as $p) {
49 $p1 = sprintf("%s%s", t($p), $p->isPassedByReference()?"&":"");
50 if ($p->isOptional()) {
51 $p1 .= sprintf("[\$%s", $p->getName());
52 } else {
53 $p1 .= sprintf("\$%s", $p->getName());
54 }
55 if ($p->isDefaultValueAvailable()) {
56 $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
57 } elseif ($p->allowsNull()) {
58 $p1 .= sprintf(" = NULL");
59 }
60 if ($p->isOptional()) {
61 $p1 .= sprintf("]");
62 }
63 $ps[] = $p1;
64 }
65 printf("%s) {\n\t}\n", implode(", ", $ps));
66 }
67 }
68
69 printf("}\n\n");
70 }
71