remove superfluous buffer macros
[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 printf("<?php\n\n");
35
36 $ext = new ReflectionExtension($ext);
37 foreach ($ext->getConstants() as $constant => $value) {
38 printf("const %s = %s;\n", $constant, $value);
39 }
40 printf("\n");
41
42 foreach ($ext->getFunctions() as $f) {
43 printf("function %s(", $f->getName());
44 $ps = array();
45 foreach ($f->getParameters() as $p) {
46 $p1 = sprintf("%s%s\$%s", t($p), $p->isPassedByReference()?"&":"", $p->getName());
47 if ($p->isOptional()) {
48 if ($p->isDefaultValueAvailable()) {
49 $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
50 } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
51 $p1 .= " = NULL";
52 } elseif ($p->isArray()) {
53 $p1 .= " = array()";
54 }
55 }
56 $ps[] = $p1;
57 }
58 printf("%s) {\n}\n", implode(", ", $ps));
59 }
60 printf("\n");
61
62 $classes = $ext->getClasses();
63 usort($classes, function($a,$b) {
64 $cmp = strcmp($a->getNamespaceName(), $b->getNamespaceName());
65 if (!$cmp) {
66 $cmp = strcmp($a->getShortName(), $b->getShortName());
67 }
68 return $cmp;
69 }
70 );
71
72 foreach ($classes as $class) {
73
74 if ($class->inNamespace()) {
75 printf("namespace %s\n{\n", $class->getNamespaceName());
76 }
77 printf("\t%s%s %s ", m($class->getModifiers()), $class->isInterface() ? "interface":"class" ,$class->getShortName());
78 if ($p = $class->getParentClass()) {
79 printf("extends \\%s ", $p->getName());
80 }
81 if ($i = $class->getInterfaceNames()) {
82 printf("implements \\%s ", implode(", \\", array_filter($i,function($v){return$v!="Traversable";})));
83 }
84 printf("\n\t{\n");
85
86 $_=0;
87 foreach ($class->getConstants() as $n => $v) {
88 c($n, $class) and $_+=printf("\t\tconst %s = %s;\n", $n, var_export($v, true));
89 }
90 $_ and printf("\n");
91 $_=0;
92 foreach ($class->getProperties() as $p) {
93 if ($p->getDeclaringClass()->getName() == $class->getName()) {
94 $_+=printf("\t\t%s\$%s;\n", m($p->getModifiers()), $p->getName());
95 }
96 }
97 $_ and printf("\n");
98
99 foreach ($class->getMethods() as $m) {
100 if ($m->getDeclaringClass()->getName() == $class->getName()) {
101 printf("\t\t%sfunction %s(", m($m->getModifiers()), $m->getName());
102 $ps = array();
103 foreach ($m->getParameters() as $p) {
104 $p1 = sprintf("%s%s\$%s", t($p), $p->isPassedByReference()?"&":"", $p->getName());
105 if ($p->isOptional()) {
106 if ($p->isDefaultValueAvailable()) {
107 $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
108 } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
109 $p1 .= sprintf(" = NULL");
110 } elseif ($p->isArray()) {
111 $p1 .= " = array()";
112 }
113 }
114 $ps[] = $p1;
115 }
116 printf("%s)", implode(", ", $ps));
117 if ($m->isAbstract()) {
118 printf(";\n\n");
119 } else {
120 printf(" {\n\t\t}\n\n");
121 }
122 }
123 }
124
125 printf("\t}\n");
126 if ($class->inNamespace()) {
127 printf("}\n");
128 }
129 printf("\n");
130 }
131