Ei kuvausta

ParseCommand.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Command;
  11. use PhpParser\Node;
  12. use PhpParser\Parser;
  13. use Psy\Context;
  14. use Psy\ContextAware;
  15. use Psy\Input\CodeArgument;
  16. use Psy\ParserFactory;
  17. use Psy\VarDumper\Presenter;
  18. use Psy\VarDumper\PresenterAware;
  19. use Symfony\Component\Console\Input\InputInterface;
  20. use Symfony\Component\Console\Input\InputOption;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Symfony\Component\VarDumper\Caster\Caster;
  23. /**
  24. * Parse PHP code and show the abstract syntax tree.
  25. */
  26. class ParseCommand extends Command implements ContextAware, PresenterAware
  27. {
  28. /**
  29. * Context instance (for ContextAware interface).
  30. *
  31. * @var Context
  32. */
  33. protected $context;
  34. private $presenter;
  35. private $parserFactory;
  36. private $parsers;
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function __construct($name = null)
  41. {
  42. $this->parserFactory = new ParserFactory();
  43. $this->parsers = [];
  44. parent::__construct($name);
  45. }
  46. /**
  47. * ContextAware interface.
  48. *
  49. * @param Context $context
  50. */
  51. public function setContext(Context $context)
  52. {
  53. $this->context = $context;
  54. }
  55. /**
  56. * PresenterAware interface.
  57. *
  58. * @param Presenter $presenter
  59. */
  60. public function setPresenter(Presenter $presenter)
  61. {
  62. $this->presenter = clone $presenter;
  63. $this->presenter->addCasters([
  64. 'PhpParser\Node' => function (Node $node, array $a) {
  65. $a = [
  66. Caster::PREFIX_VIRTUAL . 'type' => $node->getType(),
  67. Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(),
  68. ];
  69. foreach ($node->getSubNodeNames() as $name) {
  70. $a[Caster::PREFIX_VIRTUAL . $name] = $node->$name;
  71. }
  72. return $a;
  73. },
  74. ]);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function configure()
  80. {
  81. $definition = [
  82. new CodeArgument('code', CodeArgument::REQUIRED, 'PHP code to parse.'),
  83. new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse.', 10),
  84. ];
  85. if ($this->parserFactory->hasKindsSupport()) {
  86. $msg = 'One of PhpParser\\ParserFactory constants: '
  87. . \implode(', ', ParserFactory::getPossibleKinds())
  88. . " (default is based on current interpreter's version).";
  89. $defaultKind = $this->parserFactory->getDefaultKind();
  90. $definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind);
  91. }
  92. $this
  93. ->setName('parse')
  94. ->setDefinition($definition)
  95. ->setDescription('Parse PHP code and show the abstract syntax tree.')
  96. ->setHelp(
  97. <<<'HELP'
  98. Parse PHP code and show the abstract syntax tree.
  99. This command is used in the development of PsySH. Given a string of PHP code,
  100. it pretty-prints the PHP Parser parse tree.
  101. See https://github.com/nikic/PHP-Parser
  102. It prolly won't be super useful for most of you, but it's here if you want to play.
  103. HELP
  104. );
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. protected function execute(InputInterface $input, OutputInterface $output)
  110. {
  111. $code = $input->getArgument('code');
  112. if (\strpos('<?', $code) === false) {
  113. $code = '<?php ' . $code;
  114. }
  115. $parserKind = $this->parserFactory->hasKindsSupport() ? $input->getOption('kind') : null;
  116. $depth = $input->getOption('depth');
  117. $nodes = $this->parse($this->getParser($parserKind), $code);
  118. $output->page($this->presenter->present($nodes, $depth));
  119. $this->context->setReturnValue($nodes);
  120. return 0;
  121. }
  122. /**
  123. * Lex and parse a string of code into statements.
  124. *
  125. * @param Parser $parser
  126. * @param string $code
  127. *
  128. * @return array Statements
  129. */
  130. private function parse(Parser $parser, $code)
  131. {
  132. try {
  133. return $parser->parse($code);
  134. } catch (\PhpParser\Error $e) {
  135. if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
  136. throw $e;
  137. }
  138. // If we got an unexpected EOF, let's try it again with a semicolon.
  139. return $parser->parse($code . ';');
  140. }
  141. }
  142. /**
  143. * Get (or create) the Parser instance.
  144. *
  145. * @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above)
  146. *
  147. * @return Parser
  148. */
  149. private function getParser($kind = null)
  150. {
  151. if (!\array_key_exists($kind, $this->parsers)) {
  152. $this->parsers[$kind] = $this->parserFactory->createParser($kind);
  153. }
  154. return $this->parsers[$kind];
  155. }
  156. }