No Description

php-parse.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. require __DIR__ . '/../lib/bootstrap.php';
  3. ini_set('xdebug.max_nesting_level', 2000);
  4. // Disable XDebug var_dump() output truncation
  5. ini_set('xdebug.var_display_max_children', -1);
  6. ini_set('xdebug.var_display_max_data', -1);
  7. ini_set('xdebug.var_display_max_depth', -1);
  8. list($operations, $files) = parseArgs($argv);
  9. /* Dump nodes by default */
  10. if (empty($operations)) {
  11. $operations[] = 'dump';
  12. }
  13. if (empty($files)) {
  14. showHelp("Must specify at least one file.");
  15. }
  16. $parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative);
  17. $dumper = new PhpParser\NodeDumper;
  18. $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
  19. $serializer = new PhpParser\Serializer\XML;
  20. $traverser = new PhpParser\NodeTraverser();
  21. $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
  22. foreach ($files as $file) {
  23. if (strpos($file, '<?php') === 0) {
  24. $code = $file;
  25. echo "====> Code $code\n";
  26. } else {
  27. if (!file_exists($file)) {
  28. die("File $file does not exist.\n");
  29. }
  30. $code = file_get_contents($file);
  31. echo "====> File $file:\n";
  32. }
  33. try {
  34. $stmts = $parser->parse($code);
  35. } catch (PhpParser\Error $e) {
  36. die("==> Parse Error: {$e->getMessage()}\n");
  37. }
  38. foreach ($operations as $operation) {
  39. if ('dump' === $operation) {
  40. echo "==> Node dump:\n";
  41. echo $dumper->dump($stmts), "\n";
  42. } elseif ('pretty-print' === $operation) {
  43. echo "==> Pretty print:\n";
  44. echo $prettyPrinter->prettyPrintFile($stmts), "\n";
  45. } elseif ('serialize-xml' === $operation) {
  46. echo "==> Serialized XML:\n";
  47. echo $serializer->serialize($stmts), "\n";
  48. } elseif ('var-dump' === $operation) {
  49. echo "==> var_dump():\n";
  50. var_dump($stmts);
  51. } elseif ('resolve-names' === $operation) {
  52. echo "==> Resolved names.\n";
  53. $stmts = $traverser->traverse($stmts);
  54. }
  55. }
  56. }
  57. function showHelp($error) {
  58. die($error . "\n\n" .
  59. <<<OUTPUT
  60. Usage:
  61. php php-parse.php [operations] file1.php [file2.php ...]
  62. The file arguments can also be replaced with a code string:
  63. php php-parse.php [operations] "<?php code"
  64. Operations is a list of the following options (--dump by default):
  65. --dump -d Dump nodes using NodeDumper
  66. --pretty-print -p Pretty print file using PrettyPrinter\Standard
  67. --serialize-xml Serialize nodes using Serializer\XML
  68. --var-dump var_dump() nodes (for exact structure)
  69. --resolve-names -N Resolve names using NodeVisitor\NameResolver
  70. Example:
  71. php php-parse.php -d -p -N -d file.php
  72. Dumps nodes, pretty prints them, then resolves names and dumps them again.
  73. OUTPUT
  74. );
  75. }
  76. function parseArgs($args) {
  77. $operations = array();
  78. $files = array();
  79. array_shift($args);
  80. $parseOptions = true;
  81. foreach ($args as $arg) {
  82. if (!$parseOptions) {
  83. $files[] = $arg;
  84. continue;
  85. }
  86. switch ($arg) {
  87. case '--dump':
  88. case '-d':
  89. $operations[] = 'dump';
  90. break;
  91. case '--pretty-print':
  92. case '-p':
  93. $operations[] = 'pretty-print';
  94. break;
  95. case '--serialize-xml':
  96. $operations[] = 'serialize-xml';
  97. break;
  98. case '--var-dump':
  99. $operations[] = 'var-dump';
  100. break;
  101. case '--resolve-names':
  102. case '-N';
  103. $operations[] = 'resolve-names';
  104. break;
  105. case '--':
  106. $parseOptions = false;
  107. break;
  108. default:
  109. if ($arg[0] === '-') {
  110. showHelp("Invalid operation $arg.");
  111. } else {
  112. $files[] = $arg;
  113. }
  114. }
  115. }
  116. return array($operations, $files);
  117. }