let the ctor honor the options array too
[m6w6/ext-http] / reflection2php.php
index 4f04c28d381670bf21b233abdebcfa0ffb7ac9e4..c55eb65290bb0c2a19656ca967ff9abf8ba7d31b 100755 (executable)
@@ -9,7 +9,7 @@ function m($m) {
        return $n;
 }
 function t($p) {
-       if ($c = $p->getClass()) return $c->getName() . " ";
+       if ($c = $p->getClass()) return "\\" . $c->getName() . " ";
        if ($p->isArray()) return "array ";
 }
 function c($n, $c) {
@@ -32,14 +32,42 @@ if (!strlen($ext = $argv[1]))
        die(sprintf("Usage: %s <ext>\n", $argv[0]));
 
 $ext = new ReflectionExtension($ext);
+foreach ($ext->getConstants() as $constant => $value) {
+    printf("const %s = %s;\n", $constant, $value);
+}
+printf("\n");
+
+foreach ($ext->getFunctions() as $f) {
+    printf("function %s(", $f->getName());
+    $ps = array();
+    foreach ($f->getParameters() as $p) {
+        $p1 = sprintf("%s%s\$%s", t($p), $p->isPassedByReference()?"&":"", $p->getName());
+        if ($p->isOptional()) {
+            if ($p->isDefaultValueAvailable()) {
+                $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
+            } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
+                $p1 .= " = NULL";
+            } elseif ($p->isArray()) {
+                $p1 .= " = array()";
+            }
+        }
+        $ps[] = $p1;
+    }
+    printf("%s) {\n}\n", implode(", ", $ps));
+}
+printf("\n");
+
 foreach ($ext->getClasses() as $class) {
 
-       printf("%s%s %s ", m($class->getModifiers()), $class->isInterface() ? "interface":"class" ,$class->getName());
+    if ($class->inNamespace()) {
+        printf("namespace %s {\n", $class->getNamespaceName());
+    }
+       printf("%s%s %s ", m($class->getModifiers()), $class->isInterface() ? "interface":"class" ,$class->getShortName());
        if ($p = $class->getParentClass()) {
-               printf("extends %s ", $p->getName());
+               printf("extends \\%s ", $p->getName());
        }
        if ($i = $class->getInterfaceNames()) {
-               printf("implements %s ", implode(", ", array_filter($i,function($v){return$v!="Traversable";})));
+               printf("implements \\%s ", implode(", \\", array_filter($i,function($v){return$v!="Traversable";})));
        }
        printf("\n{\n");
 
@@ -62,17 +90,25 @@ foreach ($ext->getClasses() as $class) {
                        $ps = array();
                        foreach ($m->getParameters() as $p) {
                                $p1 = sprintf("%s%s\$%s", t($p), $p->isPassedByReference()?"&":"", $p->getName());
-                               if ($p->isDefaultValueAvailable()) {
-                                       $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
-                               } elseif ($p->allowsNull() || $p->isOptional()) {
-                                       $p1 .= sprintf(" = NULL");
-                               }
+                if ($p->isOptional()) {
+                    if ($p->isDefaultValueAvailable()) {
+                        $p1 .= sprintf(" = %s", var_export($p->getDefaultValue(), true));
+                    } elseif (!($p->isArray() || $p->getClass()) || $p->allowsNull()) {
+                        $p1 .= sprintf(" = NULL");
+                    } elseif ($p->isArray()) {
+                        $p1 .= " = array()";
+                    }
+                }
                                $ps[] = $p1;
                        }
                        printf("%s) {\n\t}\n", implode(", ", $ps));
                }
        }
 
-       printf("}\n\n");
+    printf("}\n");
+    if ($class->inNamespace()) {
+        printf("}\n");
+    }
+    printf("\n");
 }