4 function m($m, $c = null) {
6 if ($c && $c->isInterface()) {
7 $m ^
= ReflectionMethod
::IS_ABSTRACT
;
9 foreach (Reflection
::getModifierNames($m) as $mn) {
14 function t(ReflectionParameter
$p) {
15 if ($c = $p->getClass()) return ($p->allowsNull()?
"?":"") . "\\" . $c->getName() . " ";
16 if ($p->isArray()) return ($p->allowsNull() ?
"?":"") . "array ";
18 return ($p->allowsNull() ?
"?" : "") . ($p->getType()->isBuiltin() ?
"" : "\\") . $p->getType()->getName() . " ";
23 while ($c = $c->getParentClass()) {
24 if (array_key_exists($n, $c->getConstants())) {
29 foreach ((array) $c->getInterfaces() as $i) {
30 if (array_key_exists($n, $i->getConstants()) ||
!c($n, $i)) {
37 ob_start(function($s) {
38 // redirect any output to stderr
47 $out = fopen($argv[2], "w") or die;
53 die(sprintf($out, "Usage: %s <ext>\n", $argv[0]));
56 fprintf($out, "<?php\n\n");
58 $ext = new ReflectionExtension($ext);
62 $structures = array();
64 // split up by namespace first
65 foreach ($ext->getConstants() as $constant => $value) {
66 $ns = ($nsend = strrpos($constant, "\\")) ?
substr($constant, 0, $nsend++
) : "";
67 $cn = substr($constant, $nsend);
68 $constants[$ns][$cn] = $value;
70 foreach ($ext->getFunctions() as $f) {
71 /* @var $f ReflectionFunction */
72 $ns = $f->inNamespace() ?
$f->getNamespaceName() : "";
73 $functions[$ns][$f->getShortName()] = $f;
75 foreach ($ext->getClasses() as $c) {
76 /* @var $c ReflectionClass */
77 $ns = $c->inNamespace() ?
$c->getNamespaceName() : "";
78 $structures[$ns][$c->getShortName()] = $c;
81 $namespaces = array_unique(array_merge(
82 array_keys($constants),
83 array_keys($functions),
84 array_keys($structures)
90 foreach ($namespaces as $ns) {
91 fprintf($out, "namespace %s%s\n{\n", $ns, strlen($ns) ?
" " : "");
93 if (isset($constants[$ns])) {
94 ksort($constants[$ns], SORT_NATURAL
);
95 foreach ($constants[$ns] as $cn => $value) {
96 fprintf($out, "\tconst %s = %s;\n", $cn, var_export($value, true));
100 if (isset($functions[$ns])) {
101 ksort($functions[$ns], SORT_NATURAL
);
102 foreach ($functions[$ns] as $fn => $f) {
103 /* @var $f ReflectionFunction */
104 fprintf($out, "\n\tfunction %s(", $fn);
106 foreach ($f->getParameters() as $p) {
107 $p1 = sprintf("%s%s\$%s", t($p),
108 $p->isPassedByReference()?
"&":"", trim($p->getName(), "\""));
109 if ($p->isOptional()) {
110 if ($p->isDefaultValueAvailable()) {
111 $p1 .= sprintf(" = %s",
112 var_export($p->getDefaultValue(), true));
113 } elseif (!($p->isArray() ||
$p->getClass()) ||
$p->allowsNull()) {
115 } elseif ($p->isArray()) {
121 fprintf($out, "%s)", implode(", ", $ps));
122 if ($f->hasReturnType()) {
123 fprintf($out, " : %s%s", $f->getReturnType()->allowsNull() ?
"?":"", $f->getReturnType()->getName());
125 fprintf($out, " {\n\t}\n");
129 if (isset($structures[$ns])) {
130 uasort($structures[$ns], function ($a, $b) {
131 /* @var $a ReflectionClass */
132 /* @var $b ReflectionClass */
137 -!substr_compare($a->getShortName(), "Exception", -strlen("Exception")),
141 -!substr_compare($b->getShortName(), "Exception", -strlen("Exception")),
147 return strnatcmp($a->getShortName(), $b->getShortName());
149 foreach ($structures[$ns] as $cn => $c) {
150 fprintf($out, "\n\t%s%s %s ", m($c->getModifiers()),
151 $c->isInterface() ?
"interface":"class", $c->getShortName());
152 if ($p = $c->getParentClass()) {
153 fprintf($out, "extends \\%s ", $p->getName());
155 if ($i = $c->getInterfaceNames()) {
156 fprintf($out, "%s \\%s ",
157 $c->isInterface() ?
"extends" : "implements",
158 implode(", \\", array_filter($i, function($v) use($c, $i) {
159 foreach ($i as $ii) {
160 if ($v != $ii && (new ReflectionClass($ii))->implementsInterface("\\".$v)) {
164 return $v != "Traversable";
169 fprintf($out, "\n\t{\n");
172 foreach ($c->getConstants() as $n => $v) {
173 c($n, $c) and $_+
=fprintf($out, "\t\tconst %s = %s;\n", $n,
174 var_export($v, true));
176 $_ and fprintf($out, "\n");
178 foreach ($c->getProperties() as $p) {
179 if ($p->getDeclaringClass()->getName() == $c->getName()) {
180 $_+
=fprintf($out, "\t\t%s\$%s;\n", m($p->getModifiers()),
184 $_ and fprintf($out, "\n");
186 foreach ($c->getMethods() as $m) {
187 if ($m->getDeclaringClass()->getName() == $c->getName()) {
188 fprintf($out, "\t\t%sfunction %s(", m($m->getModifiers(), $c),
191 foreach ($m->getParameters() as $p) {
192 $p1 = sprintf("%s%s\$%s", t($p),
193 $p->isPassedByReference()?
"&":"", $p->getName());
194 if ($p->isOptional()) {
195 if ($p->isDefaultValueAvailable()) {
196 $p1 .= sprintf(" = %s",
197 var_export($p->getDefaultValue(), true));
198 } elseif (!($p->isArray() ||
$p->getClass()) ||
$p->allowsNull()) {
199 $p1 .= sprintf(" = NULL");
200 } elseif ($p->isArray()) {
206 fprintf($out, "%s)", implode(", ", $ps));
207 if ($m->hasReturnType()) {
208 fprintf($out, " : %s%s%s", $m->getReturnType()->allowsNull() ?
"?":"",
209 $m->getReturnType()->isBuiltin() ?
"" : "\\", //0 === strpos($m->getReturnType()->getName(), $c->getNamespaceName()) ? "\\":"",
210 $m->getReturnType()->getName());
212 if ($m->isAbstract()) {
213 fprintf($out, ";\n\n");
215 fprintf($out, " {\n\t\t}\n\n");
220 fprintf($out, "\t}\n");
225 fprintf($out, "}\n\n");