Version 1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ParseCommand.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Command;
13
14 use PhpParser\Node;
15 use PhpParser\Parser;
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;
24
25 /**
26  * Parse PHP code and show the abstract syntax tree.
27  */
28 class ParseCommand extends Command implements PresenterAware
29 {
30     private $presenter;
31     private $parserFactory;
32     private $parsers;
33
34     /**
35      * {@inheritdoc}
36      */
37     public function __construct($name = null)
38     {
39         $this->parserFactory = new ParserFactory();
40         $this->parsers = array();
41
42         parent::__construct($name);
43     }
44
45     /**
46      * PresenterAware interface.
47      *
48      * @param Presenter $presenter
49      */
50     public function setPresenter(Presenter $presenter)
51     {
52         $this->presenter = clone $presenter;
53         $this->presenter->addCasters(array(
54             'PhpParser\Node' => function (Node $node, array $a) {
55                 $a = array(
56                     Caster::PREFIX_VIRTUAL . 'type'       => $node->getType(),
57                     Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(),
58                 );
59
60                 foreach ($node->getSubNodeNames() as $name) {
61                     $a[Caster::PREFIX_VIRTUAL . $name] = $node->$name;
62                 }
63
64                 return $a;
65             },
66         ));
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function configure()
73     {
74         $definition = array(
75             new InputArgument('code', InputArgument::REQUIRED, 'PHP code to parse.'),
76             new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse', 10),
77         );
78
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();
84
85             $definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind);
86         }
87
88         $this
89             ->setName('parse')
90             ->setDefinition($definition)
91             ->setDescription('Parse PHP code and show the abstract syntax tree.')
92             ->setHelp(
93                 <<<'HELP'
94 Parse PHP code and show the abstract syntax tree.
95
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.
98
99 See https://github.com/nikic/PHP-Parser
100
101 It prolly won't be super useful for most of you, but it's here if you want to play.
102 HELP
103             );
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
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));
120     }
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
139             // If we got an unexpected EOF, let's try it again with a semicolon.
140             return $parser->parse($code . ';');
141         }
142     }
143
144     /**
145      * Get (or create) the Parser instance.
146      *
147      * @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above)
148      *
149      * @return Parser
150      */
151     private function getParser($kind = null)
152     {
153         if (!array_key_exists($kind, $this->parsers)) {
154             $this->parsers[$kind] = $this->parserFactory->createParser($kind);
155         }
156
157         return $this->parsers[$kind];
158     }
159 }