4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
16 use Psy\ParserFactory;
17 use Psy\VarDumper\Presenter;
18 use Psy\VarDumper\PresenterAware;
19 use Symfony\Component\Console\Input\InputArgument;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Input\InputOption;
22 use Symfony\Component\Console\Output\OutputInterface;
23 use Symfony\Component\VarDumper\Caster\Caster;
26 * Parse PHP code and show the abstract syntax tree.
28 class ParseCommand extends Command implements PresenterAware
31 private $parserFactory;
37 public function __construct($name = null)
39 $this->parserFactory = new ParserFactory();
40 $this->parsers = array();
42 parent::__construct($name);
46 * PresenterAware interface.
48 * @param Presenter $presenter
50 public function setPresenter(Presenter $presenter)
52 $this->presenter = clone $presenter;
53 $this->presenter->addCasters(array(
54 'PhpParser\Node' => function (Node $node, array $a) {
56 Caster::PREFIX_VIRTUAL . 'type' => $node->getType(),
57 Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(),
60 foreach ($node->getSubNodeNames() as $name) {
61 $a[Caster::PREFIX_VIRTUAL . $name] = $node->$name;
72 protected function configure()
75 new InputArgument('code', InputArgument::REQUIRED, 'PHP code to parse.'),
76 new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse', 10),
79 if ($this->parserFactory->hasKindsSupport()) {
80 $msg = 'One of PhpParser\\ParserFactory constants: '
81 . implode(', ', ParserFactory::getPossibleKinds())
82 . " (default is based on current interpreter's version)";
83 $defaultKind = $this->parserFactory->getDefaultKind();
85 $definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind);
90 ->setDefinition($definition)
91 ->setDescription('Parse PHP code and show the abstract syntax tree.')
94 Parse PHP code and show the abstract syntax tree.
96 This command is used in the development of PsySH. Given a string of PHP code,
97 it pretty-prints the PHP Parser parse tree.
99 See https://github.com/nikic/PHP-Parser
101 It prolly won't be super useful for most of you, but it's here if you want to play.
109 protected function execute(InputInterface $input, OutputInterface $output)
111 $code = $input->getArgument('code');
112 if (strpos('<?', $code) === false) {
113 $code = '<?php ' . $code;
116 $parserKind = $this->parserFactory->hasKindsSupport() ? $input->getOption('kind') : null;
117 $depth = $input->getOption('depth');
118 $nodes = $this->parse($this->getParser($parserKind), $code);
119 $output->page($this->presenter->present($nodes, $depth));
123 * Lex and parse a string of code into statements.
125 * @param Parser $parser
126 * @param string $code
128 * @return array Statements
130 private function parse(Parser $parser, $code)
133 return $parser->parse($code);
134 } catch (\PhpParser\Error $e) {
135 if (strpos($e->getMessage(), 'unexpected EOF') === false) {
139 // If we got an unexpected EOF, let's try it again with a semicolon.
140 return $parser->parse($code . ';');
145 * Get (or create) the Parser instance.
147 * @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above)
151 private function getParser($kind = null)
153 if (!array_key_exists($kind, $this->parsers)) {
154 $this->parsers[$kind] = $this->parserFactory->createParser($kind);
157 return $this->parsers[$kind];