Created
March 21, 2014 11:45
-
-
Save klkvsk/9684458 to your computer and use it in GitHub Desktop.
extdoc.php - generate .php from extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if ($argc < 2) { | |
| die('Usage: php extdoc.php <extension_name>' . PHP_EOL); | |
| } | |
| $extension = $argv[1]; | |
| $keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', | |
| 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', | |
| 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', | |
| 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', | |
| 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', | |
| 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', | |
| 'var', 'while', 'xor'); | |
| $re = new ReflectionExtension($extension); | |
| $code = '<?php' . PHP_EOL; | |
| $code .= '// Header file for: ' . $re->getName() . ' ' . $re->getVersion() . PHP_EOL . PHP_EOL; | |
| $code .= '// -- INI settings:' . PHP_EOL; | |
| foreach ($re->getINIEntries() as $var => $val) { | |
| $code .= '// ' . sprintf('%-20s', $var) . ' = ' . var_export($val, 1) . PHP_EOL; | |
| } | |
| $code .= '// --' . PHP_EOL; | |
| foreach ($re->getConstants() as $var => $val) { | |
| $code .= 'const ' . $var . ' = ' . var_export($val, 1) . ';' . PHP_EOL; | |
| } | |
| $code .= PHP_EOL; | |
| foreach ($re->getFunctions() as $func) { | |
| $code .= 'function ' . $func->getName() . '(' ; | |
| $params = array(); | |
| foreach ($func->getParameters() as $param) { | |
| $paramString = ''; | |
| if ($param->getClass()) { | |
| $paramString .= $param->getClass()->getName() . ' '; | |
| } | |
| if ($param->isPassedByReference()) { | |
| $paramString .= '&'; | |
| } | |
| $paramString .= '$' . $param->getName(); | |
| if ($param->isOptional()) { | |
| $paramString .= ' = '; | |
| if ($param->isDefaultValueAvailable()) { | |
| $paramString .= var_export($param->getDefaultValue(), 1); | |
| } else { | |
| $paramString .= 'null'; | |
| } | |
| } | |
| $params[]= $paramString; | |
| } | |
| $code .= implode(', ', $params); | |
| $code .= ') {}' . PHP_EOL; | |
| } | |
| $code .= PHP_EOL; | |
| foreach ($re->getClasses() as $class) { | |
| // header | |
| if ($class->getDocComment()) { | |
| $code .= '/**' . PHP_EOL; | |
| $code .= $class->getDocComment(); | |
| $code .= PHP_EOL . '*/' . PHP_EOL; | |
| } | |
| if ($class->isAbstract()) { | |
| $code .= 'abstract '; | |
| } | |
| if ($class->isFinal()) { | |
| $code .= 'final '; | |
| } | |
| if ($class->isInterface()) { | |
| $code .= 'interface '; | |
| } else { | |
| $code .= 'class '; | |
| } | |
| $code .= $class->getName() . ' '; | |
| if ($class->getParentClass()) { | |
| $code .= 'extends ' . $class->getParentClass()->getName() . ' '; | |
| } | |
| if ($class->getInterfaceNames()) { | |
| $code .= 'implements ' . implode(', ', $class->getInterfaceNames()) . ' '; | |
| } | |
| $code .= '{' . PHP_EOL; | |
| // constants | |
| foreach ($class->getConstants() as $var => $val) { | |
| $code .= "\t" . 'const ' . $var . ' = ' . var_export($val, 1) . ';' . PHP_EOL; | |
| } | |
| if ($class->getConstants()) { | |
| $code .= PHP_EOL; | |
| } | |
| // properties | |
| $defaults = $class->getDefaultProperties(); | |
| foreach ($class->getProperties() as $prop) { | |
| $code .= "\t"; | |
| if ($prop->isPrivate()) { | |
| $code .= 'private '; | |
| } | |
| if ($prop->isProtected()) { | |
| $code .= 'protected '; | |
| } | |
| if ($prop->isPublic()) { | |
| $code .= 'public '; | |
| } | |
| $code .= '$' . $prop->getName(); | |
| if ($prop->isDefault() ) { | |
| $code .= ' = ' . var_export($defaults[$prop->getName()], 1); | |
| } | |
| $code .= ';' . PHP_EOL; | |
| } | |
| if ($class->getProperties()) { | |
| $code .= PHP_EOL; | |
| } | |
| // methods | |
| foreach ($class->getMethods() as $method) { | |
| if ($class->getParentClass() && $class->getParentClass()->hasMethod($method->getName())) { | |
| continue; | |
| } | |
| if ($method->getDocComment()) { | |
| $code .= "\t" . '/**' . PHP_EOL; | |
| $code .= "\t" . $method->getDocComment(); | |
| $code .= "\t" . PHP_EOL . '*/' . PHP_EOL; | |
| } | |
| $code .= "\t"; | |
| if ($method->isFinal()) { | |
| $code .= 'final '; | |
| } | |
| if ($method->isAbstract()) { | |
| $code .= 'abstract '; | |
| } | |
| if ($method->isStatic()) { | |
| $code .= 'static '; | |
| } | |
| if ($method->isPrivate()) { | |
| $code .= 'private '; | |
| } | |
| if ($method->isProtected()) { | |
| $code .= 'protected '; | |
| } | |
| if ($method->isPublic()) { | |
| $code .= 'public '; | |
| } | |
| $name = $method->getName(); | |
| if (in_array($name, $keywords)) { | |
| $name = $name . '_BITCH'; | |
| } | |
| $code .= 'function ' . $name . '('; | |
| $params = array(); | |
| foreach ($method->getParameters() as $param) { | |
| $paramString = ''; | |
| if ($param->getClass()) { | |
| $paramString .= $param->getClass()->getName() . ' '; | |
| } | |
| if ($param->isPassedByReference()) { | |
| $paramString .= '&'; | |
| } | |
| $paramString .= '$' . $param->getName(); | |
| if ($param->isOptional()) { | |
| $paramString .= ' = '; | |
| if ($param->isDefaultValueAvailable()) { | |
| $paramString .= var_export($param->getDefaultValue(), 1); | |
| } else { | |
| $paramString .= 'null'; | |
| } | |
| } | |
| $params[]= $paramString; | |
| } | |
| $code .= implode(', ', $params); | |
| $body = ''; | |
| if ($name == '__toString') { | |
| $body = ' return "string"; '; | |
| } | |
| $code .= ') {' . $body . '}' . PHP_EOL; | |
| } | |
| // footer | |
| $code .= '}' . PHP_EOL . PHP_EOL; | |
| } | |
| if (php_sapi_name() == 'cli') { | |
| echo $code; | |
| } else { | |
| highlight_string($code); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment