rename info tests
[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 ob_start(function($s) {
32 // redirect any output to stderr
33 fwrite(STDERR, $s);
34 return true;
35 });
36
37 $out = STDOUT;
38 switch ($argc) {
39 default:
40 case 3:
41 $out = fopen($argv[2], "w") or die;
42 case 2:
43 $ext = $argv[1];
44 break;
45
46 case 1:
47 die(sprintf($out, "Usage: %s <ext>\n", $argv[0]));
48 }
49
50 fprintf($out, "<?php\n\n");
51
52 $ext = new ReflectionExtension($ext);
53
54 $constants = array();
55 $functions = array();
56 $structures = array();
57
58 // split up by namespace first
59 foreach ($ext->getConstants() as $constant => $value) {
60 $ns = ($nsend = strrpos($constant, "\\")) ? substr($constant, 0, $nsend++) : "";
61 $cn = substr($constant, $nsend);
62 $constants[$ns][$cn] = $value;
63 }
64 foreach ($ext->getFunctions() as $f) {
65 /* @var $f ReflectionFunction */
66 $ns = $f->inNamespace() ? $f->getNamespaceName() : "";
67 $functions[$ns][$f->getShortName()] = $f;
68 }
69 foreach ($ext->getClasses() as $c) {
70 /* @var $c ReflectionClass */
71 $ns = $c->inNamespace() ? $c->getNamespaceName() : "";
72 $structures[$ns][$c->getShortName()] = $c;
73 }
74
75 $namespaces = array_unique(array_merge(
76 array_keys($constants),
77 array_keys($functions),
78 array_keys($structures)
79 ));
80
81 // simple sort
82 natsort($namespaces);
83
84 foreach ($namespaces as $ns) {
85 fprintf($out, "namespace %s%s\n{\n", $ns, strlen($ns) ? " " : "");
86 //
87 if (isset($constants[$ns])) {
88 ksort($constants[$ns], SORT_NATURAL);
89 foreach ($constants[$ns] as $cn => $value) {
90 fprintf($out, "\tconst %s = %s;\n", $cn, var_export($value, true));
91 }
92 }
93 //
94 if (isset($functions[$ns])) {
95 ksort($functions[$ns], SORT_NATURAL);
96 foreach ($functions[$ns] as $fn => $f) {
97 /* @var $f ReflectionFunction */
98 fprintf($out, "\n\tfunction %s(", $fn);
99 $ps = array();
100 foreach ($f->getParameters() as $p) {
101 $p1 = sfprintf($out, "%s%s\$%s", t($p),
102 $p->isPassedByReference()?"&":"", $p->getName());
103 if ($p->isOptional()) {
104 if ($p->isDefaultValueAvailable()) {
105 $p1 .= sfprintf($out, " = %s",
106 var_export($p->getDefaultValue(), true));
107 } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
108 $p1 .= " = NULL";
109 } elseif ($p->isArray()) {
110 $p1 .= " = array()";
111 }
112 }
113 $ps[] = $p1;
114 }
115 fprintf($out, "%s) {\n\t}\n", implode(", ", $ps));
116 }
117 }
118 //
119 if (isset($structures[$ns])) {
120 uasort($structures[$ns], function ($a, $b) {
121 /* @var $a ReflectionClass */
122 /* @var $b ReflectionClass */
123 $score = array_sum([
124 -!$a->isInterface()+
125 -!$a->isAbstract()+
126 -!$a->isTrait()+
127 -!substr_compare($a->getShortName(), "Exception", -strlen("Exception")),
128 +!$b->isInterface()+
129 +!$b->isAbstract()+
130 +!$b->isTrait()+
131 -!substr_compare($b->getShortName(), "Exception", -strlen("Exception")),
132 ]);
133
134 if ($score) {
135 return -$score;
136 }
137 return strnatcmp($a->getShortName(), $b->getShortName());
138 });
139 foreach ($structures[$ns] as $cn => $c) {
140 fprintf($out, "\n\t%s%s %s ", m($c->getModifiers()),
141 $c->isInterface() ? "interface":"class", $c->getShortName());
142 if ($p = $c->getParentClass()) {
143 fprintf($out, "extends \\%s ", $p->getName());
144 }
145 if ($i = $c->getInterfaceNames()) {
146 fprintf($out, "implements \\%s ",
147 implode(", \\", array_filter($i, function($v) {
148 return $v != "Traversable";
149
150 }))
151 );
152 }
153 fprintf($out, "\n\t{\n");
154
155 $_=0;
156 foreach ($c->getConstants() as $n => $v) {
157 c($n, $c) and $_+=fprintf($out, "\t\tconst %s = %s;\n", $n,
158 var_export($v, true));
159 }
160 $_ and fprintf($out, "\n");
161 $_=0;
162 foreach ($c->getProperties() as $p) {
163 if ($p->getDeclaringClass()->getName() == $c->getName()) {
164 $_+=fprintf($out, "\t\t%s\$%s;\n", m($p->getModifiers()),
165 $p->getName());
166 }
167 }
168 $_ and fprintf($out, "\n");
169
170 foreach ($c->getMethods() as $m) {
171 if ($m->getDeclaringClass()->getName() == $c->getName()) {
172 fprintf($out, "\t\t%sfunction %s(", m($m->getModifiers()),
173 $m->getName());
174 $ps = array();
175 foreach ($m->getParameters() as $p) {
176 $p1 = sprintf("%s%s\$%s", t($p),
177 $p->isPassedByReference()?"&":"", $p->getName());
178 if ($p->isOptional()) {
179 if ($p->isDefaultValueAvailable()) {
180 $p1 .= sprintf(" = %s",
181 var_export($p->getDefaultValue(), true));
182 } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
183 $p1 .= sprintf(" = NULL");
184 } elseif ($p->isArray()) {
185 $p1 .= " = array()";
186 }
187 }
188 $ps[] = $p1;
189 }
190 fprintf($out, "%s)", implode(", ", $ps));
191 if ($m->isAbstract()) {
192 fprintf($out, ";\n\n");
193 } else {
194 fprintf($out, " {\n\t\t}\n\n");
195 }
196 }
197 }
198
199 fprintf($out, "\t}\n");
200
201 }
202 }
203 //
204 fprintf($out, "}\n\n");
205 }