| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of Psy Shell.
|
|
|
5 |
*
|
|
|
6 |
* (c) 2012-2022 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\Arg;
|
|
|
15 |
use PhpParser\Node\Expr\New_;
|
|
|
16 |
use PhpParser\Node\Expr\Variable;
|
|
|
17 |
use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
|
|
|
18 |
use PhpParser\Node\Scalar\String_;
|
|
|
19 |
use PhpParser\Node\Stmt\Throw_;
|
|
|
20 |
use PhpParser\PrettyPrinter\Standard as Printer;
|
|
|
21 |
use Psy\Context;
|
|
|
22 |
use Psy\ContextAware;
|
|
|
23 |
use Psy\Exception\ThrowUpException;
|
|
|
24 |
use Psy\Input\CodeArgument;
|
|
|
25 |
use Psy\ParserFactory;
|
|
|
26 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
27 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Throw an exception or error out of the Psy Shell.
|
|
|
31 |
*/
|
|
|
32 |
class ThrowUpCommand extends Command implements ContextAware
|
|
|
33 |
{
|
|
|
34 |
private $parser;
|
|
|
35 |
private $printer;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* {@inheritdoc}
|
|
|
39 |
*/
|
|
|
40 |
public function __construct($name = null)
|
|
|
41 |
{
|
|
|
42 |
$parserFactory = new ParserFactory();
|
|
|
43 |
|
|
|
44 |
$this->parser = $parserFactory->createParser();
|
|
|
45 |
$this->printer = new Printer();
|
|
|
46 |
|
|
|
47 |
parent::__construct($name);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @deprecated throwUp no longer needs to be ContextAware
|
|
|
52 |
*
|
|
|
53 |
* @param Context $context
|
|
|
54 |
*/
|
|
|
55 |
public function setContext(Context $context)
|
|
|
56 |
{
|
|
|
57 |
// Do nothing
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* {@inheritdoc}
|
|
|
62 |
*/
|
|
|
63 |
protected function configure()
|
|
|
64 |
{
|
|
|
65 |
$this
|
|
|
66 |
->setName('throw-up')
|
|
|
67 |
->setDefinition([
|
|
|
68 |
new CodeArgument('exception', CodeArgument::OPTIONAL, 'Exception or Error to throw.'),
|
|
|
69 |
])
|
|
|
70 |
->setDescription('Throw an exception or error out of the Psy Shell.')
|
|
|
71 |
->setHelp(
|
|
|
72 |
<<<'HELP'
|
|
|
73 |
Throws an exception or error out of the current the Psy Shell instance.
|
|
|
74 |
|
|
|
75 |
By default it throws the most recent exception.
|
|
|
76 |
|
|
|
77 |
e.g.
|
|
|
78 |
<return>>>> throw-up</return>
|
|
|
79 |
<return>>>> throw-up $e</return>
|
|
|
80 |
<return>>>> throw-up new Exception('WHEEEEEE!')</return>
|
|
|
81 |
<return>>>> throw-up "bye!"</return>
|
|
|
82 |
HELP
|
|
|
83 |
);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* {@inheritdoc}
|
|
|
88 |
*
|
|
|
89 |
* @return int 0 if everything went fine, or an exit code
|
|
|
90 |
*
|
|
|
91 |
* @throws \InvalidArgumentException if there is no exception to throw
|
|
|
92 |
*/
|
|
|
93 |
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
94 |
{
|
|
|
95 |
$args = $this->prepareArgs($input->getArgument('exception'));
|
|
|
96 |
$throwStmt = new Throw_(new New_(new FullyQualifiedName(ThrowUpException::class), $args));
|
|
|
97 |
$throwCode = $this->printer->prettyPrint([$throwStmt]);
|
|
|
98 |
|
|
|
99 |
$shell = $this->getApplication();
|
|
|
100 |
$shell->addCode($throwCode, !$shell->hasCode());
|
|
|
101 |
|
|
|
102 |
return 0;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Parse the supplied command argument.
|
|
|
107 |
*
|
|
|
108 |
* If no argument was given, this falls back to `$_e`
|
|
|
109 |
*
|
|
|
110 |
* @throws \InvalidArgumentException if there is no exception to throw
|
|
|
111 |
*
|
|
|
112 |
* @param string $code
|
|
|
113 |
*
|
|
|
114 |
* @return Arg[]
|
|
|
115 |
*/
|
|
|
116 |
private function prepareArgs(string $code = null): array
|
|
|
117 |
{
|
|
|
118 |
if (!$code) {
|
|
|
119 |
// Default to last exception if nothing else was supplied
|
|
|
120 |
return [new Arg(new Variable('_e'))];
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
if (\strpos($code, '<?') === false) {
|
|
|
124 |
$code = '<?php '.$code;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
$nodes = $this->parse($code);
|
|
|
128 |
if (\count($nodes) !== 1) {
|
|
|
129 |
throw new \InvalidArgumentException('No idea how to throw this');
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
$node = $nodes[0];
|
|
|
133 |
|
|
|
134 |
// Make this work for PHP Parser v3.x
|
|
|
135 |
$expr = isset($node->expr) ? $node->expr : $node;
|
|
|
136 |
|
|
|
137 |
$args = [new Arg($expr, false, false, $node->getAttributes())];
|
|
|
138 |
|
|
|
139 |
// Allow throwing via a string, e.g. `throw-up "SUP"`
|
|
|
140 |
if ($expr instanceof String_) {
|
|
|
141 |
return [new New_(new FullyQualifiedName(\Exception::class), $args)];
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
return $args;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Lex and parse a string of code into statements.
|
|
|
149 |
*
|
|
|
150 |
* @param string $code
|
|
|
151 |
*
|
|
|
152 |
* @return array Statements
|
|
|
153 |
*/
|
|
|
154 |
private function parse(string $code): array
|
|
|
155 |
{
|
|
|
156 |
try {
|
|
|
157 |
return $this->parser->parse($code);
|
|
|
158 |
} catch (\PhpParser\Error $e) {
|
|
|
159 |
if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
|
|
|
160 |
throw $e;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// If we got an unexpected EOF, let's try it again with a semicolon.
|
|
|
164 |
return $this->parser->parse($code.';');
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
}
|