4 * This file is part of Psy Shell.
6 * (c) 2012-2018 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;
18 use Psy\Input\CodeArgument;
19 use Psy\ParserFactory;
20 use Psy\VarDumper\Presenter;
21 use Psy\VarDumper\PresenterAware;
22 use Symfony\Component\Console\Input\InputInterface;
23 use Symfony\Component\Console\Input\InputOption;
24 use Symfony\Component\Console\Output\OutputInterface;
25 use Symfony\Component\VarDumper\Caster\Caster;
28 * Parse PHP code and show the abstract syntax tree.
30 class ParseCommand extends Command implements ContextAware, PresenterAware
33 * Context instance (for ContextAware interface).
40 private $parserFactory;
46 public function __construct($name = null)
48 $this->parserFactory = new ParserFactory();
51 parent::__construct($name);
55 * ContextAware interface.
57 * @param Context $context
59 public function setContext(Context $context)
61 $this->context = $context;
65 * PresenterAware interface.
67 * @param Presenter $presenter
69 public function setPresenter(Presenter $presenter)
71 $this->presenter = clone $presenter;
72 $this->presenter->addCasters([
73 'PhpParser\Node' => function (Node $node, array $a) {
75 Caster::PREFIX_VIRTUAL . 'type' => $node->getType(),
76 Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(),
79 foreach ($node->getSubNodeNames() as $name) {
80 $a[Caster::PREFIX_VIRTUAL . $name] = $node->$name;
91 protected function configure()
94 new CodeArgument('code', CodeArgument::REQUIRED, 'PHP code to parse.'),
95 new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse.', 10),
98 if ($this->parserFactory->hasKindsSupport()) {
99 $msg = 'One of PhpParser\\ParserFactory constants: '
100 . \implode(', ', ParserFactory::getPossibleKinds())
101 . " (default is based on current interpreter's version).";
102 $defaultKind = $this->parserFactory->getDefaultKind();
104 $definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind);
109 ->setDefinition($definition)
110 ->setDescription('Parse PHP code and show the abstract syntax tree.')
113 Parse PHP code and show the abstract syntax tree.
115 This command is used in the development of PsySH. Given a string of PHP code,
116 it pretty-prints the PHP Parser parse tree.
118 See https://github.com/nikic/PHP-Parser
120 It prolly won't be super useful for most of you, but it's here if you want to play.
128 protected function execute(InputInterface $input, OutputInterface $output)
130 $code = $input->getArgument('code');
131 if (\strpos('<?', $code) === false) {
132 $code = '<?php ' . $code;
135 $parserKind = $this->parserFactory->hasKindsSupport() ? $input->getOption('kind') : null;
136 $depth = $input->getOption('depth');
137 $nodes = $this->parse($this->getParser($parserKind), $code);
138 $output->page($this->presenter->present($nodes, $depth));
140 $this->context->setReturnValue($nodes);
144 * Lex and parse a string of code into statements.
146 * @param Parser $parser
147 * @param string $code
149 * @return array Statements
151 private function parse(Parser $parser, $code)
154 return $parser->parse($code);
155 } catch (\PhpParser\Error $e) {
156 if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
160 // If we got an unexpected EOF, let's try it again with a semicolon.
161 return $parser->parse($code . ';');
166 * Get (or create) the Parser instance.
168 * @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above)
172 private function getParser($kind = null)
174 if (!\array_key_exists($kind, $this->parsers)) {
175 $this->parsers[$kind] = $this->parserFactory->createParser($kind);
178 return $this->parsers[$kind];