Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

some improvements

  • Loading branch information...
commit 231926fcd53f9a64ae277c6de716b8f065419662 1 parent 4971939
@juzna authored
Showing with 90 additions and 50 deletions.
  1. +90 −50 phpstubgen.php
View
140 phpstubgen.php
@@ -1,67 +1,107 @@
<?php
$outDir = getcwd() . "/out";
if (!file_exists($outDir)) {
- $result = mkdir($outDir, 0755, true);
+ $result = mkdir($outDir, 0755, true);
}
$loaded_extensions = get_loaded_extensions();
foreach ($loaded_extensions as $extensionName) {
- handleExtension($extensionName);
+ handleExtension($extensionName);
}
function handleExtension($extensionName)
{
+ global $outDir;
- $out = fopen("out\\$extensionName.php", 'w');
- fwrite($out, "<?php\n");
- $reflectionExtension = new ReflectionExtension($extensionName);
+ $methodModifiers = array(
+ ReflectionMethod::IS_ABSTRACT => 'abstract',
+ ReflectionMethod::IS_FINAL => 'final',
+ ReflectionMethod::IS_PRIVATE => 'private',
+ ReflectionMethod::IS_PROTECTED => 'protected',
+ ReflectionMethod::IS_PUBLIC => 'public',
+ ReflectionMethod::IS_STATIC => 'static',
+ );
- //constants
- $constants = $reflectionExtension->getConstants();
- foreach ($constants as $constantName => $constantValue) {
- if (is_string($constantValue)) {
- $constantValue = "'$constantValue'";
- }
- fwrite($out, "const $constantName = $constantValue;\n");
- }
- //functions
- $defined_functions = $reflectionExtension->getFunctions();
- foreach ($defined_functions as $definedFunction) {
- /** @var $definedFunction ReflectionFunction */
- $definedFunctionName = $definedFunction->getName();
- fwrite($out, "function $definedFunctionName(");
- $parameters = $definedFunction->getParameters();
- $isFirstParameter = true;
- foreach ($parameters as $parameter) {
- if ($isFirstParameter) {
- $isFirstParameter = false;
- } else {
- fwrite($out, ", ");
- }
- /** @var $parameter ReflectionParameter */
- if ($parameter->isPassedByReference()) {
- fwrite($out, "&");
- }
- $parameterName = $parameter->getName();
- fwrite($out, "$$parameterName");
- if ($parameter->isDefaultValueAvailable()) {
- fwrite($out, " = ");
- print_r($parameter->getDefaultValue());
- }
- }
- fwrite($out, "){ }\n");
- }
+ $out = fopen("$outDir/$extensionName.php", 'w');
+ fwrite($out, "<?php\n");
+ $reflectionExtension = new ReflectionExtension($extensionName);
- //classes
- $classes = $reflectionExtension->getClasses();
- foreach ($classes as $class) {
- /** @var $class ReflectionClass */
- $className = $class->getName();
+ //constants
+ $constants = $reflectionExtension->getConstants();
+ foreach ($constants as $constantName => $constantValue) {
+ if (is_string($constantValue)) {
+ $constantValue = "'$constantValue'";
+ }
+ fwrite($out, "const $constantName = $constantValue;\n");
+ }
- fwrite($out, "class $className {\n");
- fwrite($out, "}\n");
- }
- fclose($out);
-}
+ //functions
+ $defined_functions = $reflectionExtension->getFunctions();
+ foreach ($defined_functions as $definedFunction) {
+ /** @var $definedFunction ReflectionFunction */
+ $definedFunctionName = $definedFunction->getName();
+ fwrite($out, "function $definedFunctionName(");
+ $parameters = $definedFunction->getParameters();
+ $isFirstParameter = true;
+ foreach ($parameters as $parameter) {
+ if ($isFirstParameter) {
+ $isFirstParameter = false;
+ } else {
+ fwrite($out, ", ");
+ }
+ /** @var $parameter ReflectionParameter */
+ if ($parameter->isPassedByReference()) {
+ fwrite($out, "&");
+ }
+ $parameterName = $parameter->getName();
+ fwrite($out, "$$parameterName");
+ if ($parameter->isDefaultValueAvailable()) {
+ fwrite($out, " = ");
+ print_r($parameter->getDefaultValue());
+ }
+ }
+ fwrite($out, "){ }\n");
+ }
+
+ //classes
+ $classes = $reflectionExtension->getClasses();
+ foreach ($classes as $class) {
+ /** @var $class ReflectionClass */
+ $className = $class->getName();
+
+ if ($class->getNamespaceName()) fwrite($out, "namespace {$class->getNamespaceName()};\n");
+
+ $modifiers = $class->isAbstract() ? 'abstract ' : '';
+ fwrite($out, "{$modifiers}class {$class->getShortName()} {\n");
+
+ // methods
+ foreach ($class->getMethods() as /** @var ReflectionMethod $method */ $method) {
+ $modifierFlags = $method->getModifiers();
+ $modifiers = array();
+ foreach ($methodModifiers as $flag => $code) {
+ if ($modifierFlags & $flag) $modifiers[] = $code;
+ }
+
+ // args
+ $args = array();
+ foreach ($method->getParameters() as $parameter) {
+ try {
+ $args[] =
+ ($parameter->getClass() ? '\'' . $parameter->getClass()->getName() . ' ' : '') .
+ '$' . $parameter->getName() .
+ ($parameter->isDefaultValueAvailable() ? ' = ' . var_export($parameter->getDefaultValue(), true) : '');
+ } catch(Exception $e) {
+ $args[] = '$' . $parameter->getName();
+ }
+ }
+ $args = implode(', ', $args);
+
+ fwrite($out, "\t" . implode(' ', $modifiers) . " function {$method->getName()}($args) {}\n");
+ }
+
+ fwrite($out, "}\n");
+ }
+ fclose($out);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.