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\Input\CodeArgument;
17 use Psy\ParserFactory;
18 use Psy\VarDumper\Presenter;
19 use Psy\VarDumper\PresenterAware;
20 use Symfony\Component\Console\Input\InputArgument;
21 use Symfony\Component\Console\Input\InputInterface;
22 use Symfony\Component\Console\Input\InputOption;
23 use Symfony\Component\Console\Output\OutputInterface;
24 use Symfony\Component\VarDumper\Caster\Caster;
27 * Parse PHP code and show the abstract syntax tree.
29 class ParseCommand extends Command implements PresenterAware
32 private $parserFactory;
38 public function __construct($name = null)
40 $this->parserFactory = new ParserFactory();
41 $this->parsers = array();
43 parent::__construct($name);
47 * PresenterAware interface.
49 * @param Presenter $presenter
51 public function setPresenter(Presenter $presenter)
53 $this->presenter = clone $presenter;
54 $this->presenter->addCasters(array(
55 'PhpParser\Node' => function (Node $node, array $a) {
57 Caster::PREFIX_VIRTUAL . 'type' => $node->getType(),
58 Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(),
61 foreach ($node->getSubNodeNames() as $name) {
62 $a[Caster::PREFIX_VIRTUAL . $name] = $node->$name;
73 protected function configure()
76 new CodeArgument('code', InputArgument::REQUIRED, 'PHP code to parse.'),
77 new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse', 10),
80 if ($this->parserFactory->hasKindsSupport()) {
81 $msg = 'One of PhpParser\\ParserFactory constants: '
82 . implode(', ', ParserFactory::getPossibleKinds())
83 . " (default is based on current interpreter's version)";
84 $defaultKind = $this->parserFactory->getDefaultKind();
86 $definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind);
91 ->setDefinition($definition)
92 ->setDescription('Parse PHP code and show the abstract syntax tree.')
95 Parse PHP code and show the abstract syntax tree.
97 This command is used in the development of PsySH. Given a string of PHP code,
98 it pretty-prints the PHP Parser parse tree.
100 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.
110 protected function execute(InputInterface $input, OutputInterface $output)
112 $code = $input->getArgument('code');
113 if (strpos('<?', $code) === false) {
114 $code = '<?php ' . $code;
117 $parserKind = $this->parserFactory->hasKindsSupport() ? $input->getOption('kind') : null;
118 $depth = $input->getOption('depth');
119 $nodes = $this->parse($this->getParser($parserKind), $code);
120 $output->page($this->presenter->present($nodes, $depth));
124 * Lex and parse a string of code into statements.
126 * @param Parser $parser
127 * @param string $code
129 * @return array Statements
131 private function parse(Parser $parser, $code)
134 return $parser->parse($code);
135 } catch (\PhpParser\Error $e) {
136 if (strpos($e->getMessage(), 'unexpected EOF') === false) {
140 // If we got an unexpected EOF, let's try it again with a semicolon.
141 return $parser->parse($code . ';');
146 * Get (or create) the Parser instance.
148 * @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above)
152 private function getParser($kind = null)
154 if (!array_key_exists($kind, $this->parsers)) {
155 $this->parsers[$kind] = $this->parserFactory->createParser($kind);
158 return $this->parsers[$kind];