4 function m($m, $c = null) {
6 if ($c && $c->isInterface()) {
7 $m ^
= ReflectionMethod
::IS_ABSTRACT
;
9 foreach (Reflection
::getModifierNames($m) as $mn) {
15 if ($c = $p->getClass()) return "\\" . $c->getName() . " ";
16 if ($p->isArray()) return "array ";
20 while ($c = $c->getParentClass()) {
21 if (array_key_exists($n, $c->getConstants())) {
26 foreach ((array) $c->getInterfaces() as $i) {
27 if (array_key_exists($n, $i->getConstants()) ||
!c($n, $i)) {
34 ob_start(function($s) {
35 // redirect any output to stderr
44 $out = fopen($argv[2], "w") or die;
50 die(sprintf($out, "Usage: %s <ext>\n", $argv[0]));
53 fprintf($out, "<?php\n\n");
55 $ext = new ReflectionExtension($ext);
59 $structures = array();
61 // split up by namespace first
62 foreach ($ext->getConstants() as $constant => $value) {
63 $ns = ($nsend = strrpos($constant, "\\")) ?
substr($constant, 0, $nsend++
) : "";
64 $cn = substr($constant, $nsend);
65 $constants[$ns][$cn] = $value;
67 foreach ($ext->getFunctions() as $f) {
68 /* @var $f ReflectionFunction */
69 $ns = $f->inNamespace() ?
$f->getNamespaceName() : "";
70 $functions[$ns][$f->getShortName()] = $f;
72 foreach ($ext->getClasses() as $c) {
73 /* @var $c ReflectionClass */
74 $ns = $c->inNamespace() ?
$c->getNamespaceName() : "";
75 $structures[$ns][$c->getShortName()] = $c;
78 $namespaces = array_unique(array_merge(
79 array_keys($constants),
80 array_keys($functions),
81 array_keys($structures)
87 foreach ($namespaces as $ns) {
88 fprintf($out, "namespace %s%s\n{\n", $ns, strlen($ns) ?
" " : "");
90 if (isset($constants[$ns])) {
91 ksort($constants[$ns], SORT_NATURAL
);
92 foreach ($constants[$ns] as $cn => $value) {
93 fprintf($out, "\tconst %s = %s;\n", $cn, var_export($value, true));
97 if (isset($functions[$ns])) {
98 ksort($functions[$ns], SORT_NATURAL
);
99 foreach ($functions[$ns] as $fn => $f) {
100 /* @var $f ReflectionFunction */
101 fprintf($out, "\n\tfunction %s(", $fn);
103 foreach ($f->getParameters() as $p) {
104 $p1 = sprintf("%s%s\$%s", t($p),
105 $p->isPassedByReference()?
"&":"", trim($p->getName(), "\""));
106 if ($p->isOptional()) {
107 if ($p->isDefaultValueAvailable()) {
108 $p1 .= sprintf(" = %s",
109 var_export($p->getDefaultValue(), true));
110 } elseif (!($p->isArray() ||
$p->getClass()) ||
$p->allowsNull()) {
112 } elseif ($p->isArray()) {
118 fprintf($out, "%s) {\n\t}\n", implode(", ", $ps));
122 if (isset($structures[$ns])) {
123 uasort($structures[$ns], function ($a, $b) {
124 /* @var $a ReflectionClass */
125 /* @var $b ReflectionClass */
130 -!substr_compare($a->getShortName(), "Exception", -strlen("Exception")),
134 -!substr_compare($b->getShortName(), "Exception", -strlen("Exception")),
140 return strnatcmp($a->getShortName(), $b->getShortName());
142 foreach ($structures[$ns] as $cn => $c) {
143 fprintf($out, "\n\t%s%s %s ", m($c->getModifiers()),
144 $c->isInterface() ?
"interface":"class", $c->getShortName());
145 if ($p = $c->getParentClass()) {
146 fprintf($out, "extends \\%s ", $p->getName());
148 if ($i = $c->getInterfaceNames()) {
149 fprintf($out, "implements \\%s ",
150 implode(", \\", array_filter($i, function($v) {
151 return $v != "Traversable";
156 fprintf($out, "\n\t{\n");
159 foreach ($c->getConstants() as $n => $v) {
160 c($n, $c) and $_+
=fprintf($out, "\t\tconst %s = %s;\n", $n,
161 var_export($v, true));
163 $_ and fprintf($out, "\n");
165 foreach ($c->getProperties() as $p) {
166 if ($p->getDeclaringClass()->getName() == $c->getName()) {
167 $_+
=fprintf($out, "\t\t%s\$%s;\n", m($p->getModifiers()),
171 $_ and fprintf($out, "\n");
173 foreach ($c->getMethods() as $m) {
174 if ($m->getDeclaringClass()->getName() == $c->getName()) {
175 fprintf($out, "\t\t%sfunction %s(", m($m->getModifiers(), $c),
178 foreach ($m->getParameters() as $p) {
179 $p1 = sprintf("%s%s\$%s", t($p),
180 $p->isPassedByReference()?
"&":"", $p->getName());
181 if ($p->isOptional()) {
182 if ($p->isDefaultValueAvailable()) {
183 $p1 .= sprintf(" = %s",
184 var_export($p->getDefaultValue(), true));
185 } elseif (!($p->isArray() ||
$p->getClass()) ||
$p->allowsNull()) {
186 $p1 .= sprintf(" = NULL");
187 } elseif ($p->isArray()) {
193 fprintf($out, "%s)", implode(", ", $ps));
194 if ($m->isAbstract()) {
195 fprintf($out, ";\n\n");
197 fprintf($out, " {\n\t\t}\n\n");
202 fprintf($out, "\t}\n");
207 fprintf($out, "}\n\n");