6 foreach (Reflection
::getModifierNames($m) as $mn) {
12 if ($c = $p->getClass()) return "\\" . $c->getName() . " ";
13 if ($p->isArray()) return "array ";
17 while ($c = $c->getParentClass()) {
18 if (array_key_exists($n, $c->getConstants())) {
23 foreach ((array) $c->getInterfaces() as $i) {
24 if (array_key_exists($n, $i->getConstants()) ||
!c($n, $i)) {
31 ob_start(function($s) {
32 // redirect any output to stderr
41 $out = fopen($argv[2], "w") or die;
47 die(sprintf($out, "Usage: %s <ext>\n", $argv[0]));
50 fprintf($out, "<?php\n\n");
52 $ext = new ReflectionExtension($ext);
56 $structures = array();
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;
64 foreach ($ext->getFunctions() as $f) {
65 /* @var $f ReflectionFunction */
66 $ns = $f->inNamespace() ?
$f->getNamespaceName() : "";
67 $functions[$ns][$f->getShortName()] = $f;
69 foreach ($ext->getClasses() as $c) {
70 /* @var $c ReflectionClass */
71 $ns = $c->inNamespace() ?
$c->getNamespaceName() : "";
72 $structures[$ns][$c->getShortName()] = $c;
75 $namespaces = array_unique(array_merge(
76 array_keys($constants),
77 array_keys($functions),
78 array_keys($structures)
84 foreach ($namespaces as $ns) {
85 fprintf($out, "namespace %s%s\n{\n", $ns, strlen($ns) ?
" " : "");
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));
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);
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()) {
109 } elseif ($p->isArray()) {
115 fprintf($out, "%s) {\n\t}\n", implode(", ", $ps));
119 if (isset($structures[$ns])) {
120 uasort($structures[$ns], function ($a, $b) {
121 /* @var $a ReflectionClass */
122 /* @var $b ReflectionClass */
127 -!substr_compare($a->getShortName(), "Exception", -strlen("Exception")),
131 -!substr_compare($b->getShortName(), "Exception", -strlen("Exception")),
137 return strnatcmp($a->getShortName(), $b->getShortName());
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());
145 if ($i = $c->getInterfaceNames()) {
146 fprintf($out, "implements \\%s ",
147 implode(", \\", array_filter($i, function($v) {
148 return $v != "Traversable";
153 fprintf($out, "\n\t{\n");
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));
160 $_ and fprintf($out, "\n");
162 foreach ($c->getProperties() as $p) {
163 if ($p->getDeclaringClass()->getName() == $c->getName()) {
164 $_+
=fprintf($out, "\t\t%s\$%s;\n", m($p->getModifiers()),
168 $_ and fprintf($out, "\n");
170 foreach ($c->getMethods() as $m) {
171 if ($m->getDeclaringClass()->getName() == $c->getName()) {
172 fprintf($out, "\t\t%sfunction %s(", m($m->getModifiers()),
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()) {
190 fprintf($out, "%s)", implode(", ", $ps));
191 if ($m->isAbstract()) {
192 fprintf($out, ";\n\n");
194 fprintf($out, " {\n\t\t}\n\n");
199 fprintf($out, "\t}\n");
204 fprintf($out, "}\n\n");