comparison vendor/psy/psysh/src/Psy/Command/SudoCommand.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\NodeTraverser;
15 use PhpParser\PrettyPrinter\Standard as Printer;
16 use Psy\Input\CodeArgument;
17 use Psy\ParserFactory;
18 use Psy\Readline\Readline;
19 use Psy\Sudo\SudoVisitor;
20 use Symfony\Component\Console\Input\InputArgument;
21 use Symfony\Component\Console\Input\InputInterface;
22 use Symfony\Component\Console\Output\OutputInterface;
23
24 /**
25 * Evaluate PHP code, bypassing visibility restrictions.
26 */
27 class SudoCommand extends Command
28 {
29 private $readline;
30 private $parser;
31 private $traverser;
32 private $printer;
33
34 /**
35 * {@inheritdoc}
36 */
37 public function __construct($name = null)
38 {
39 $parserFactory = new ParserFactory();
40 $this->parser = $parserFactory->createParser();
41
42 $this->traverser = new NodeTraverser();
43 $this->traverser->addVisitor(new SudoVisitor());
44
45 $this->printer = new Printer();
46
47 parent::__construct($name);
48 }
49
50 /**
51 * Set the Shell's Readline service.
52 *
53 * @param Readline $readline
54 */
55 public function setReadline(Readline $readline)
56 {
57 $this->readline = $readline;
58 }
59
60 /**
61 * {@inheritdoc}
62 */
63 protected function configure()
64 {
65 $this
66 ->setName('sudo')
67 ->setDefinition(array(
68 new CodeArgument('code', InputArgument::REQUIRED, 'Code to execute.'),
69 ))
70 ->setDescription('Evaluate PHP code, bypassing visibility restrictions.')
71 ->setHelp(
72 <<<'HELP'
73 Evaluate PHP code, bypassing visibility restrictions.
74
75 e.g.
76 <return>>>> $sekret->whisper("hi")</return>
77 <return>PHP error: Call to private method Sekret::whisper() from context '' on line 1</return>
78
79 <return>>>> sudo $sekret->whisper("hi")</return>
80 <return>=> "hi"</return>
81
82 <return>>>> $sekret->word</return>
83 <return>PHP error: Cannot access private property Sekret::$word on line 1</return>
84
85 <return>>>> sudo $sekret->word</return>
86 <return>=> "hi"</return>
87
88 <return>>>> $sekret->word = "please"</return>
89 <return>PHP error: Cannot access private property Sekret::$word on line 1</return>
90
91 <return>>>> sudo $sekret->word = "please"</return>
92 <return>=> "please"</return>
93 HELP
94 );
95 }
96
97 /**
98 * {@inheritdoc}
99 */
100 protected function execute(InputInterface $input, OutputInterface $output)
101 {
102 $code = $input->getArgument('code');
103
104 // special case for !!
105 if ($code === '!!') {
106 $history = $this->readline->listHistory();
107 if (count($history) < 2) {
108 throw new \InvalidArgumentException('No previous command to replay');
109 }
110 $code = $history[count($history) - 2];
111 }
112
113 if (strpos('<?', $code) === false) {
114 $code = '<?php ' . $code;
115 }
116
117 $nodes = $this->traverser->traverse($this->parse($code));
118
119 $sudoCode = $this->printer->prettyPrint($nodes);
120 $this->getApplication()->addInput($sudoCode, true);
121 }
122
123 /**
124 * Lex and parse a string of code into statements.
125 *
126 * @param string $code
127 *
128 * @return array Statements
129 */
130 private function parse($code)
131 {
132 try {
133 return $this->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 $this->parser->parse($code . ';');
141 }
142 }
143 }