header cleanups and fix some warnings
[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 function c($n, $c) {
16 $_=$c;
17 while ($c = $c->getParentClass()) {
18 if (array_key_exists($n, $c->getConstants())) {
19 return false;
20 }
21 }
22 $c=$_;
23 foreach ((array) $c->getInterfaces() as $i) {
24 if (array_key_exists($n, $i->getConstants()) || !c($n, $i)) {
25 return false;
26 }
27 }
28 return true;
29 }
30
31 if (!strlen($ext = $argv[1]))
32 die(sprintf("Usage: %s <ext>\n", $argv[0]));
33
34 $ext = new ReflectionExtension($ext);
35 foreach ($ext->getConstants() as $constant => $value) {
36 printf("const %s = %s;\n", $constant, $value);
37 }
38 printf("\n");
39
40 foreach ($ext->getFunctions() as $f) {
41 printf("function %s(", $f->getName());
42 $ps = array();
43 foreach ($f->getParameters() as $p) {
44 $p1 = sprintf("%s%s\$%s", t($p), $p->isPassedByReference()?"&":"", $p->getName());
45 if ($p->isOptional()) {
46 if ($p->isDefaultValueAvailable()) {
47 $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
48 } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
49 $p1 .= " = NULL";
50 } elseif ($p->isArray()) {
51 $p1 .= " = array()";
52 }
53 }
54 $ps[] = $p1;
55 }
56 printf("%s) {\n}\n", implode(", ", $ps));
57 }
58 printf("\n");
59
60 foreach ($ext->getClasses() as $class) {
61
62 printf("%s%s %s ", m($class->getModifiers()), $class->isInterface() ? "interface":"class" ,$class->getName());
63 if ($p = $class->getParentClass()) {
64 printf("extends %s ", $p->getName());
65 }
66 if ($i = $class->getInterfaceNames()) {
67 printf("implements %s ", implode(", ", array_filter($i,function($v){return$v!="Traversable";})));
68 }
69 printf("\n{\n");
70
71 $_=0;
72 foreach ($class->getConstants() as $n => $v) {
73 c($n, $class) and $_+=printf("\tconst %s = %s;\n", $n, var_export($v, true));
74 }
75 $_ and printf("\n");
76 $_=0;
77 foreach ($class->getProperties() as $p) {
78 if ($p->getDeclaringClass()->getName() == $class->getName()) {
79 $_+=printf("\t%s\$%s;\n", m($p->getModifiers()), $p->getName());
80 }
81 }
82 $_ and printf("\n");
83
84 foreach ($class->getMethods() as $m) {
85 if ($m->getDeclaringClass()->getName() == $class->getName()) {
86 printf("\t%sfunction %s(", m($m->getModifiers()), $m->getName());
87 $ps = array();
88 foreach ($m->getParameters() as $p) {
89 $p1 = sprintf("%s%s\$%s", t($p), $p->isPassedByReference()?"&":"", $p->getName());
90 if ($p->isOptional()) {
91 if ($p->isDefaultValueAvailable()) {
92 $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
93 } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
94 $p1 .= sprintf(" = NULL");
95 } elseif ($p->isArray()) {
96 $p1 .= " = array()";
97 }
98 }
99 $ps[] = $p1;
100 }
101 printf("%s) {\n\t}\n", implode(", ", $ps));
102 }
103 }
104
105 printf("}\n\n");
106 }
107