c55eb65290bb0c2a19656ca967ff9abf8ba7d31b
[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 if ($class->inNamespace()) {
63 printf("namespace %s {\n", $class->getNamespaceName());
64 }
65 printf("%s%s %s ", m($class->getModifiers()), $class->isInterface() ? "interface":"class" ,$class->getShortName());
66 if ($p = $class->getParentClass()) {
67 printf("extends \\%s ", $p->getName());
68 }
69 if ($i = $class->getInterfaceNames()) {
70 printf("implements \\%s ", implode(", \\", array_filter($i,function($v){return$v!="Traversable";})));
71 }
72 printf("\n{\n");
73
74 $_=0;
75 foreach ($class->getConstants() as $n => $v) {
76 c($n, $class) and $_+=printf("\tconst %s = %s;\n", $n, var_export($v, true));
77 }
78 $_ and printf("\n");
79 $_=0;
80 foreach ($class->getProperties() as $p) {
81 if ($p->getDeclaringClass()->getName() == $class->getName()) {
82 $_+=printf("\t%s\$%s;\n", m($p->getModifiers()), $p->getName());
83 }
84 }
85 $_ and printf("\n");
86
87 foreach ($class->getMethods() as $m) {
88 if ($m->getDeclaringClass()->getName() == $class->getName()) {
89 printf("\t%sfunction %s(", m($m->getModifiers()), $m->getName());
90 $ps = array();
91 foreach ($m->getParameters() as $p) {
92 $p1 = sprintf("%s%s\$%s", t($p), $p->isPassedByReference()?"&":"", $p->getName());
93 if ($p->isOptional()) {
94 if ($p->isDefaultValueAvailable()) {
95 $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
96 } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
97 $p1 .= sprintf(" = NULL");
98 } elseif ($p->isArray()) {
99 $p1 .= " = array()";
100 }
101 }
102 $ps[] = $p1;
103 }
104 printf("%s) {\n\t}\n", implode(", ", $ps));
105 }
106 }
107
108 printf("}\n");
109 if ($class->inNamespace()) {
110 printf("}\n");
111 }
112 printf("\n");
113 }
114