Chris@0: parser = $parserFactory->createParser(); Chris@0: $this->printer = new Printer(); Chris@0: Chris@0: parent::__construct($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * ContextAware interface. Chris@0: * Chris@0: * @param Context $context Chris@0: */ Chris@0: public function setContext(Context $context) Chris@0: { Chris@0: $this->context = $context; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function configure() Chris@0: { Chris@0: $this Chris@0: ->setName('throw-up') Chris@0: ->setDefinition([ Chris@0: new CodeArgument('exception', CodeArgument::OPTIONAL, 'Exception or Error to throw.'), Chris@0: ]) Chris@0: ->setDescription('Throw an exception or error out of the Psy Shell.') Chris@0: ->setHelp( Chris@0: <<<'HELP' Chris@0: Throws an exception or error out of the current the Psy Shell instance. Chris@0: Chris@0: By default it throws the most recent exception. Chris@0: Chris@0: e.g. Chris@0: >>> throw-up Chris@0: >>> throw-up $e Chris@0: >>> throw-up new Exception('WHEEEEEE!') Chris@0: >>> throw-up "bye!" Chris@0: HELP Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @throws InvalidArgumentException if there is no exception to throw Chris@0: */ Chris@0: protected function execute(InputInterface $input, OutputInterface $output) Chris@0: { Chris@0: $args = $this->prepareArgs($input->getArgument('exception')); Chris@0: $throwStmt = new Throw_(new StaticCall(new FullyQualifiedName(self::THROW_CLASS), 'fromThrowable', $args)); Chris@0: $throwCode = $this->printer->prettyPrint([$throwStmt]); Chris@0: Chris@0: $shell = $this->getApplication(); Chris@0: $shell->addCode($throwCode, !$shell->hasCode()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parse the supplied command argument. Chris@0: * Chris@0: * If no argument was given, this falls back to `$_e` Chris@0: * Chris@0: * @throws InvalidArgumentException if there is no exception to throw Chris@0: * Chris@0: * @param string $code Chris@0: * Chris@0: * @return Arg[] Chris@0: */ Chris@0: private function prepareArgs($code = null) Chris@0: { Chris@0: if (!$code) { Chris@0: // Default to last exception if nothing else was supplied Chris@0: return [new Arg(new Variable('_e'))]; Chris@0: } Chris@0: Chris@4: if (\strpos('parse($code); Chris@4: if (\count($nodes) !== 1) { Chris@0: throw new \InvalidArgumentException('No idea how to throw this'); Chris@0: } Chris@0: Chris@0: $node = $nodes[0]; Chris@0: Chris@0: // Make this work for PHP Parser v3.x Chris@0: $expr = isset($node->expr) ? $node->expr : $node; Chris@0: Chris@0: $args = [new Arg($expr, false, false, $node->getAttributes())]; Chris@0: Chris@0: // Allow throwing via a string, e.g. `throw-up "SUP"` Chris@0: if ($expr instanceof String_) { Chris@0: return [new New_(new FullyQualifiedName('Exception'), $args)]; Chris@0: } Chris@0: Chris@0: return $args; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Lex and parse a string of code into statements. Chris@0: * Chris@0: * @param string $code Chris@0: * Chris@0: * @return array Statements Chris@0: */ Chris@0: private function parse($code) Chris@0: { Chris@0: try { Chris@0: return $this->parser->parse($code); Chris@0: } catch (\PhpParser\Error $e) { Chris@4: if (\strpos($e->getMessage(), 'unexpected EOF') === false) { Chris@0: throw $e; Chris@0: } Chris@0: Chris@0: // If we got an unexpected EOF, let's try it again with a semicolon. Chris@0: return $this->parser->parse($code . ';'); Chris@0: } Chris@0: } Chris@0: }