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