annotate vendor/psy/psysh/src/Command/ThrowUpCommand.php @ 19:fa3358dc1485 tip

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