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