Mercurial > hg > cmmr2012-drupal-site
comparison vendor/psy/psysh/src/Command/ParseCommand.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of Psy Shell. | |
5 * | |
6 * (c) 2012-2018 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\Context; | |
17 use Psy\ContextAware; | |
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; | |
26 | |
27 /** | |
28 * Parse PHP code and show the abstract syntax tree. | |
29 */ | |
30 class ParseCommand extends Command implements ContextAware, PresenterAware | |
31 { | |
32 /** | |
33 * Context instance (for ContextAware interface). | |
34 * | |
35 * @var Context | |
36 */ | |
37 protected $context; | |
38 | |
39 private $presenter; | |
40 private $parserFactory; | |
41 private $parsers; | |
42 | |
43 /** | |
44 * {@inheritdoc} | |
45 */ | |
46 public function __construct($name = null) | |
47 { | |
48 $this->parserFactory = new ParserFactory(); | |
49 $this->parsers = []; | |
50 | |
51 parent::__construct($name); | |
52 } | |
53 | |
54 /** | |
55 * ContextAware interface. | |
56 * | |
57 * @param Context $context | |
58 */ | |
59 public function setContext(Context $context) | |
60 { | |
61 $this->context = $context; | |
62 } | |
63 | |
64 /** | |
65 * PresenterAware interface. | |
66 * | |
67 * @param Presenter $presenter | |
68 */ | |
69 public function setPresenter(Presenter $presenter) | |
70 { | |
71 $this->presenter = clone $presenter; | |
72 $this->presenter->addCasters([ | |
73 'PhpParser\Node' => function (Node $node, array $a) { | |
74 $a = [ | |
75 Caster::PREFIX_VIRTUAL . 'type' => $node->getType(), | |
76 Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(), | |
77 ]; | |
78 | |
79 foreach ($node->getSubNodeNames() as $name) { | |
80 $a[Caster::PREFIX_VIRTUAL . $name] = $node->$name; | |
81 } | |
82 | |
83 return $a; | |
84 }, | |
85 ]); | |
86 } | |
87 | |
88 /** | |
89 * {@inheritdoc} | |
90 */ | |
91 protected function configure() | |
92 { | |
93 $definition = [ | |
94 new CodeArgument('code', CodeArgument::REQUIRED, 'PHP code to parse.'), | |
95 new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse.', 10), | |
96 ]; | |
97 | |
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(); | |
103 | |
104 $definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind); | |
105 } | |
106 | |
107 $this | |
108 ->setName('parse') | |
109 ->setDefinition($definition) | |
110 ->setDescription('Parse PHP code and show the abstract syntax tree.') | |
111 ->setHelp( | |
112 <<<'HELP' | |
113 Parse PHP code and show the abstract syntax tree. | |
114 | |
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. | |
117 | |
118 See https://github.com/nikic/PHP-Parser | |
119 | |
120 It prolly won't be super useful for most of you, but it's here if you want to play. | |
121 HELP | |
122 ); | |
123 } | |
124 | |
125 /** | |
126 * {@inheritdoc} | |
127 */ | |
128 protected function execute(InputInterface $input, OutputInterface $output) | |
129 { | |
130 $code = $input->getArgument('code'); | |
131 if (strpos('<?', $code) === false) { | |
132 $code = '<?php ' . $code; | |
133 } | |
134 | |
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)); | |
139 | |
140 $this->context->setReturnValue($nodes); | |
141 } | |
142 | |
143 /** | |
144 * Lex and parse a string of code into statements. | |
145 * | |
146 * @param Parser $parser | |
147 * @param string $code | |
148 * | |
149 * @return array Statements | |
150 */ | |
151 private function parse(Parser $parser, $code) | |
152 { | |
153 try { | |
154 return $parser->parse($code); | |
155 } catch (\PhpParser\Error $e) { | |
156 if (strpos($e->getMessage(), 'unexpected EOF') === false) { | |
157 throw $e; | |
158 } | |
159 | |
160 // If we got an unexpected EOF, let's try it again with a semicolon. | |
161 return $parser->parse($code . ';'); | |
162 } | |
163 } | |
164 | |
165 /** | |
166 * Get (or create) the Parser instance. | |
167 * | |
168 * @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above) | |
169 * | |
170 * @return Parser | |
171 */ | |
172 private function getParser($kind = null) | |
173 { | |
174 if (!array_key_exists($kind, $this->parsers)) { | |
175 $this->parsers[$kind] = $this->parserFactory->createParser($kind); | |
176 } | |
177 | |
178 return $this->parsers[$kind]; | |
179 } | |
180 } |