[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/scripts/symbols/ -> generate_php_symbols.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  
   4  $root = dirname(dirname(dirname(__FILE__)));
   5  require_once $root.'/scripts/__init_script__.php';
   6  
   7  if ($argc !== 1 || posix_isatty(STDIN)) {
   8    echo phutil_console_format(
   9      "usage: find . -type f -name '*.php' | ./generate_php_symbols.php\n");
  10    exit(1);
  11  }
  12  
  13  $input = file_get_contents('php://stdin');
  14  $input = trim($input);
  15  $input = explode("\n", $input);
  16  
  17  $data = array();
  18  $futures = array();
  19  
  20  foreach ($input as $file) {
  21    $file = Filesystem::readablePath($file);
  22    $data[$file] = Filesystem::readFile($file);
  23    $futures[$file] = xhpast_get_parser_future($data[$file]);
  24  }
  25  
  26  foreach (Futures($futures)->limit(8) as $file => $future) {
  27    $tree = XHPASTTree::newFromDataAndResolvedExecFuture(
  28      $data[$file],
  29      $future->resolve());
  30  
  31    $root = $tree->getRootNode();
  32    $scopes = array();
  33  
  34    $functions = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
  35    foreach ($functions as $function) {
  36      $name = $function->getChildByIndex(2);
  37      // Skip anonymous functions
  38      if (!$name->getConcreteString()) {
  39        continue;
  40      }
  41      print_symbol($file, 'function', $name);
  42    }
  43  
  44    $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
  45    foreach ($classes as $class) {
  46      $class_name = $class->getChildByIndex(1);
  47      print_symbol($file, 'class', $class_name);
  48      $scopes[] = array($class, $class_name);
  49    }
  50  
  51    $interfaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION');
  52    foreach ($interfaces as $interface) {
  53      $interface_name = $interface->getChildByIndex(1);
  54      // We don't differentiate classes and interfaces in highlighters.
  55      print_symbol($file, 'class', $interface_name);
  56      $scopes[] = array($interface, $interface_name);
  57    }
  58  
  59    $constants = $root->selectDescendantsOfType('n_CONSTANT_DECLARATION_LIST');
  60    foreach ($constants as $constant_list) {
  61      foreach ($constant_list->getChildren() as $constant) {
  62        $constant_name = $constant->getChildByIndex(0);
  63        print_symbol($file, 'constant', $constant_name);
  64      }
  65    }
  66  
  67    foreach ($scopes as $scope) {
  68      // this prints duplicate symbols in the case of nested classes
  69      // luckily, PHP doesn't allow those
  70      list($class, $class_name) = $scope;
  71  
  72      $consts = $class->selectDescendantsOfType(
  73        'n_CLASS_CONSTANT_DECLARATION_LIST');
  74      foreach ($consts as $const_list) {
  75        foreach ($const_list->getChildren() as $const) {
  76          $const_name = $const->getChildByIndex(0);
  77          print_symbol($file, 'class_const', $const_name, $class_name);
  78        }
  79      }
  80  
  81      $members = $class->selectDescendantsOfType(
  82        'n_CLASS_MEMBER_DECLARATION_LIST');
  83      foreach ($members as $member_list) {
  84        foreach ($member_list->getChildren() as $member) {
  85          if ($member->getTypeName() == 'n_CLASS_MEMBER_MODIFIER_LIST') {
  86            continue;
  87          }
  88          $member_name = $member->getChildByIndex(0);
  89          print_symbol($file, 'member', $member_name, $class_name);
  90        }
  91      }
  92  
  93      $methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
  94      foreach ($methods as $method) {
  95        $method_name = $method->getChildByIndex(2);
  96        print_symbol($file, 'method', $method_name, $class_name);
  97      }
  98    }
  99  }
 100  
 101  function print_symbol($file, $type, $token, $context = null) {
 102    $parts = array(
 103      $context ? $context->getConcreteString() : '',
 104      // variable tokens are `$name`, not just `name`, so strip the $ off of
 105      // class field names
 106      ltrim($token->getConcreteString(), '$'),
 107      $type,
 108      'php',
 109      $token->getLineNumber(),
 110      '/'.ltrim($file, './'),
 111    );
 112    echo implode(' ', $parts)."\n";
 113  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1