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;
14 use PhpParser\Node\Arg;
15 use PhpParser\Node\Expr\StaticCall;
16 use PhpParser\Node\Expr\Variable;
17 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
18 use PhpParser\Node\Stmt\Throw_;
19 use PhpParser\PrettyPrinter\Standard as Printer;
22 use Psy\Input\CodeArgument;
23 use Psy\ParserFactory;
24 use Symfony\Component\Console\Input\InputInterface;
25 use Symfony\Component\Console\Output\OutputInterface;
28 * Throw an exception or error out of the Psy Shell.
30 class ThrowUpCommand extends Command implements ContextAware
32 const THROW_CLASS = 'Psy\Exception\ThrowUpException';
38 * Context instance (for ContextAware interface).
47 public function __construct($name = null)
49 $parserFactory = new ParserFactory();
51 $this->parser = $parserFactory->createParser();
52 $this->printer = new Printer();
54 parent::__construct($name);
58 * ContextAware interface.
60 * @param Context $context
62 public function setContext(Context $context)
64 $this->context = $context;
70 protected function configure()
75 new CodeArgument('exception', CodeArgument::OPTIONAL, 'Exception or Error to throw.'),
77 ->setDescription('Throw an exception or error out of the Psy Shell.')
80 Throws an exception or error out of the current the Psy Shell instance.
82 By default it throws the most recent exception.
85 <return>>>> throw-up</return>
86 <return>>>> throw-up $e</return>
87 <return>>>> throw-up new Exception('WHEEEEEE!')</return>
95 * @throws InvalidArgumentException if there is no exception to throw
97 protected function execute(InputInterface $input, OutputInterface $output)
99 $args = $this->prepareArgs($input->getArgument('exception'));
100 $throwStmt = new Throw_(new StaticCall(new FullyQualifiedName(self::THROW_CLASS), 'fromThrowable', $args));
101 $throwCode = $this->printer->prettyPrint([$throwStmt]);
103 $shell = $this->getApplication();
104 $shell->addCode($throwCode, !$shell->hasCode());
108 * Parse the supplied command argument.
110 * If no argument was given, this falls back to `$_e`
112 * @throws InvalidArgumentException if there is no exception to throw
114 * @param string $code
118 private function prepareArgs($code = null)
121 // Default to last exception if nothing else was supplied
122 return [new Arg(new Variable('_e'))];
125 if (strpos('<?', $code) === false) {
126 $code = '<?php ' . $code;
129 $expr = $this->parse($code);
131 if (count($expr) !== 1) {
132 throw new \InvalidArgumentException('No idea how to throw this');
135 return [new Arg($expr[0])];
139 * Lex and parse a string of code into statements.
141 * @param string $code
143 * @return array Statements
145 private function parse($code)
148 return $this->parser->parse($code);
149 } catch (\PhpParser\Error $e) {
150 if (strpos($e->getMessage(), 'unexpected EOF') === false) {
154 // If we got an unexpected EOF, let's try it again with a semicolon.
155 return $this->parser->parse($code . ';');