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;
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;
25 * Evaluate PHP code, bypassing visibility restrictions.
27 class SudoCommand extends Command
37 public function __construct($name = null)
39 $parserFactory = new ParserFactory();
40 $this->parser = $parserFactory->createParser();
42 $this->traverser = new NodeTraverser();
43 $this->traverser->addVisitor(new SudoVisitor());
45 $this->printer = new Printer();
47 parent::__construct($name);
51 * Set the Shell's Readline service.
53 * @param Readline $readline
55 public function setReadline(Readline $readline)
57 $this->readline = $readline;
63 protected function configure()
67 ->setDefinition(array(
68 new CodeArgument('code', InputArgument::REQUIRED, 'Code to execute.'),
70 ->setDescription('Evaluate PHP code, bypassing visibility restrictions.')
73 Evaluate PHP code, bypassing visibility restrictions.
76 <return>>>> $sekret->whisper("hi")</return>
77 <return>PHP error: Call to private method Sekret::whisper() from context '' on line 1</return>
79 <return>>>> sudo $sekret->whisper("hi")</return>
80 <return>=> "hi"</return>
82 <return>>>> $sekret->word</return>
83 <return>PHP error: Cannot access private property Sekret::$word on line 1</return>
85 <return>>>> sudo $sekret->word</return>
86 <return>=> "hi"</return>
88 <return>>>> $sekret->word = "please"</return>
89 <return>PHP error: Cannot access private property Sekret::$word on line 1</return>
91 <return>>>> sudo $sekret->word = "please"</return>
92 <return>=> "please"</return>
100 protected function execute(InputInterface $input, OutputInterface $output)
102 $code = $input->getArgument('code');
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');
110 $code = $history[count($history) - 2];
113 if (strpos('<?', $code) === false) {
114 $code = '<?php ' . $code;
117 $nodes = $this->traverser->traverse($this->parse($code));
119 $sudoCode = $this->printer->prettyPrint($nodes);
120 $this->getApplication()->addInput($sudoCode, true);
124 * Lex and parse a string of code into statements.
126 * @param string $code
128 * @return array Statements
130 private function parse($code)
133 return $this->parser->parse($code);
134 } catch (\PhpParser\Error $e) {
135 if (strpos($e->getMessage(), 'unexpected EOF') === false) {
139 // If we got an unexpected EOF, let's try it again with a semicolon.
140 return $this->parser->parse($code . ';');