get_php_methods.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. $project_root = dirname(__DIR__, 3);
  3. require_once $project_root . '/resources/classes/auto_loader.php';
  4. global $autoload;
  5. $autoload = new auto_loader();
  6. $class_methods = [];
  7. $classes_to_scan = $autoload->get_class_list();
  8. $interfaces = array_keys($autoload->get_interfaces());
  9. foreach ($classes_to_scan as $class => $path) {
  10. // Skip interfaces
  11. if (in_array($class, $interfaces)) {
  12. continue;
  13. }
  14. // Guard against removed classes
  15. if (!class_exists($class)) {
  16. continue;
  17. }
  18. // Create the RefectionClass for inspecting class
  19. $ref = new ReflectionClass($class);
  20. // Skip internal classes
  21. if ($ref->isInternal()) {
  22. continue;
  23. }
  24. $methods = [];
  25. foreach ($ref->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  26. // Skip __construct
  27. if ($method->getName() === '__construct') {
  28. continue;
  29. }
  30. // Get method parameters
  31. $params = [];
  32. foreach ($method->getParameters() as $param) {
  33. $type = $param->hasType() ? $param->getType() . " " : "";
  34. $default = "";
  35. if ($param->isOptional() && $param->isDefaultValueAvailable()) {
  36. $default = " = " . var_export($param->getDefaultValue(), true);
  37. }
  38. $params[] = $type . "$" . $param->getName() . $default;
  39. }
  40. // Get the doc comment and clean it up
  41. $doc = $method->getDocComment();
  42. if ($doc !== false) {
  43. $doc = trim(preg_replace('/(^\/\*\*|\*\/$)/', '', $doc));
  44. $doc = preg_replace('/^\s*\*\s?/m', '', $doc);
  45. } else {
  46. $doc = "";
  47. }
  48. // Get the return type, if any
  49. $return_type = "";
  50. if ($method->hasReturnType()) {
  51. $rt = $method->getReturnType();
  52. $return_type = $rt->getName();
  53. if ($rt->allowsNull()) {
  54. $return_type = "?" . $return_type;
  55. }
  56. }
  57. $methods[] = [
  58. "name" => $method->getName(),
  59. "params" => "(" . implode(", ", $params) . ")",
  60. "doc" => $doc,
  61. "static" => $method->isStatic(),
  62. "meta" => $return_type
  63. ];
  64. }
  65. $class_methods[$class] = $methods;
  66. }
  67. header('Content-Type: application/json');
  68. echo json_encode($class_methods);