annotate vendor/psy/psysh/src/Command/ThrowUpCommand.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents
children c2387f117808
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@13 15 use PhpParser\Node\Expr\StaticCall;
Chris@13 16 use PhpParser\Node\Expr\Variable;
Chris@13 17 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
Chris@13 18 use PhpParser\Node\Stmt\Throw_;
Chris@13 19 use PhpParser\PrettyPrinter\Standard as Printer;
Chris@13 20 use Psy\Context;
Chris@13 21 use Psy\ContextAware;
Chris@13 22 use Psy\Input\CodeArgument;
Chris@13 23 use Psy\ParserFactory;
Chris@13 24 use Symfony\Component\Console\Input\InputInterface;
Chris@13 25 use Symfony\Component\Console\Output\OutputInterface;
Chris@13 26
Chris@13 27 /**
Chris@13 28 * Throw an exception or error out of the Psy Shell.
Chris@13 29 */
Chris@13 30 class ThrowUpCommand extends Command implements ContextAware
Chris@13 31 {
Chris@13 32 const THROW_CLASS = 'Psy\Exception\ThrowUpException';
Chris@13 33
Chris@13 34 private $parser;
Chris@13 35 private $printer;
Chris@13 36
Chris@13 37 /**
Chris@13 38 * Context instance (for ContextAware interface).
Chris@13 39 *
Chris@13 40 * @var Context
Chris@13 41 */
Chris@13 42 protected $context;
Chris@13 43
Chris@13 44 /**
Chris@13 45 * {@inheritdoc}
Chris@13 46 */
Chris@13 47 public function __construct($name = null)
Chris@13 48 {
Chris@13 49 $parserFactory = new ParserFactory();
Chris@13 50
Chris@13 51 $this->parser = $parserFactory->createParser();
Chris@13 52 $this->printer = new Printer();
Chris@13 53
Chris@13 54 parent::__construct($name);
Chris@13 55 }
Chris@13 56
Chris@13 57 /**
Chris@13 58 * ContextAware interface.
Chris@13 59 *
Chris@13 60 * @param Context $context
Chris@13 61 */
Chris@13 62 public function setContext(Context $context)
Chris@13 63 {
Chris@13 64 $this->context = $context;
Chris@13 65 }
Chris@13 66
Chris@13 67 /**
Chris@13 68 * {@inheritdoc}
Chris@13 69 */
Chris@13 70 protected function configure()
Chris@13 71 {
Chris@13 72 $this
Chris@13 73 ->setName('throw-up')
Chris@13 74 ->setDefinition([
Chris@13 75 new CodeArgument('exception', CodeArgument::OPTIONAL, 'Exception or Error to throw.'),
Chris@13 76 ])
Chris@13 77 ->setDescription('Throw an exception or error out of the Psy Shell.')
Chris@13 78 ->setHelp(
Chris@13 79 <<<'HELP'
Chris@13 80 Throws an exception or error out of the current the Psy Shell instance.
Chris@13 81
Chris@13 82 By default it throws the most recent exception.
Chris@13 83
Chris@13 84 e.g.
Chris@13 85 <return>>>> throw-up</return>
Chris@13 86 <return>>>> throw-up $e</return>
Chris@13 87 <return>>>> throw-up new Exception('WHEEEEEE!')</return>
Chris@13 88 HELP
Chris@13 89 );
Chris@13 90 }
Chris@13 91
Chris@13 92 /**
Chris@13 93 * {@inheritdoc}
Chris@13 94 *
Chris@13 95 * @throws InvalidArgumentException if there is no exception to throw
Chris@13 96 */
Chris@13 97 protected function execute(InputInterface $input, OutputInterface $output)
Chris@13 98 {
Chris@13 99 $args = $this->prepareArgs($input->getArgument('exception'));
Chris@13 100 $throwStmt = new Throw_(new StaticCall(new FullyQualifiedName(self::THROW_CLASS), 'fromThrowable', $args));
Chris@13 101 $throwCode = $this->printer->prettyPrint([$throwStmt]);
Chris@13 102
Chris@13 103 $shell = $this->getApplication();
Chris@13 104 $shell->addCode($throwCode, !$shell->hasCode());
Chris@13 105 }
Chris@13 106
Chris@13 107 /**
Chris@13 108 * Parse the supplied command argument.
Chris@13 109 *
Chris@13 110 * If no argument was given, this falls back to `$_e`
Chris@13 111 *
Chris@13 112 * @throws InvalidArgumentException if there is no exception to throw
Chris@13 113 *
Chris@13 114 * @param string $code
Chris@13 115 *
Chris@13 116 * @return Arg[]
Chris@13 117 */
Chris@13 118 private function prepareArgs($code = null)
Chris@13 119 {
Chris@13 120 if (!$code) {
Chris@13 121 // Default to last exception if nothing else was supplied
Chris@13 122 return [new Arg(new Variable('_e'))];
Chris@13 123 }
Chris@13 124
Chris@13 125 if (strpos('<?', $code) === false) {
Chris@13 126 $code = '<?php ' . $code;
Chris@13 127 }
Chris@13 128
Chris@13 129 $expr = $this->parse($code);
Chris@13 130
Chris@13 131 if (count($expr) !== 1) {
Chris@13 132 throw new \InvalidArgumentException('No idea how to throw this');
Chris@13 133 }
Chris@13 134
Chris@13 135 return [new Arg($expr[0])];
Chris@13 136 }
Chris@13 137
Chris@13 138 /**
Chris@13 139 * Lex and parse a string of code into statements.
Chris@13 140 *
Chris@13 141 * @param string $code
Chris@13 142 *
Chris@13 143 * @return array Statements
Chris@13 144 */
Chris@13 145 private function parse($code)
Chris@13 146 {
Chris@13 147 try {
Chris@13 148 return $this->parser->parse($code);
Chris@13 149 } catch (\PhpParser\Error $e) {
Chris@13 150 if (strpos($e->getMessage(), 'unexpected EOF') === false) {
Chris@13 151 throw $e;
Chris@13 152 }
Chris@13 153
Chris@13 154 // If we got an unexpected EOF, let's try it again with a semicolon.
Chris@13 155 return $this->parser->parse($code . ';');
Chris@13 156 }
Chris@13 157 }
Chris@13 158 }