X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=reflection2php.php;h=aa238ed1980fc144a3a555bcbf28f8e96d700d36;hp=4f04c28d381670bf21b233abdebcfa0ffb7ac9e4;hb=d5de4f9eae86ae1dab0f8a906bf9543710ae124d;hpb=7fc758b4ccec7ae1c996b87244373fb3ab4f1376 diff --git a/reflection2php.php b/reflection2php.php index 4f04c28..aa238ed 100755 --- a/reflection2php.php +++ b/reflection2php.php @@ -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) { @@ -31,48 +31,101 @@ function c($n, $c) { if (!strlen($ext = $argv[1])) die(sprintf("Usage: %s \n", $argv[0])); +printf("getClasses() as $class) { +foreach ($ext->getConstants() as $constant => $value) { + printf("const %s = %s;\n", $constant, $value); +} +printf("\n"); - printf("%s%s %s ", m($class->getModifiers()), $class->isInterface() ? "interface":"class" ,$class->getName()); +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"); + +$classes = $ext->getClasses(); +usort($classes, function($a,$b) { + $cmp = strcmp($a->getNamespaceName(), $b->getNamespaceName()); + if (!$cmp) { + $cmp = strcmp($a->getShortName(), $b->getShortName()); + } + return $cmp; + } +); + +foreach ($classes as $class) { + + if ($class->inNamespace()) { + printf("namespace %s\n{\n", $class->getNamespaceName()); + } + printf("\t%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"); + printf("\n\t{\n"); $_=0; foreach ($class->getConstants() as $n => $v) { - c($n, $class) and $_+=printf("\tconst %s = %s;\n", $n, var_export($v, true)); + c($n, $class) and $_+=printf("\t\tconst %s = %s;\n", $n, var_export($v, true)); } $_ and printf("\n"); $_=0; foreach ($class->getProperties() as $p) { if ($p->getDeclaringClass()->getName() == $class->getName()) { - $_+=printf("\t%s\$%s;\n", m($p->getModifiers()), $p->getName()); + $_+=printf("\t\t%s\$%s;\n", m($p->getModifiers()), $p->getName()); } } $_ and printf("\n"); foreach ($class->getMethods() as $m) { if ($m->getDeclaringClass()->getName() == $class->getName()) { - printf("\t%sfunction %s(", m($m->getModifiers()), $m->getName()); + printf("\t\t%sfunction %s(", m($m->getModifiers()), $m->getName()); $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("%s)", implode(", ", $ps)); + if ($m->isAbstract()) { + printf(";\n\n"); + } else { + printf(" {\n\t\t}\n\n"); + } } } - printf("}\n\n"); + printf("\t}\n"); + if ($class->inNamespace()) { + printf("}\n"); + } + printf("\n"); }