annotate vendor/psy/psysh/src/Command/ThrowUpCommand.php @ 4:a9cd425dd02b

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