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