X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCommand%2FThrowUpCommand.php;fp=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCommand%2FThrowUpCommand.php;h=4b7003d1042d1f10a4900abbfc01666fed6d5a55;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/psy/psysh/src/Command/ThrowUpCommand.php b/vendor/psy/psysh/src/Command/ThrowUpCommand.php new file mode 100644 index 000000000..4b7003d10 --- /dev/null +++ b/vendor/psy/psysh/src/Command/ThrowUpCommand.php @@ -0,0 +1,158 @@ +parser = $parserFactory->createParser(); + $this->printer = new Printer(); + + parent::__construct($name); + } + + /** + * ContextAware interface. + * + * @param Context $context + */ + public function setContext(Context $context) + { + $this->context = $context; + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('throw-up') + ->setDefinition([ + new CodeArgument('exception', CodeArgument::OPTIONAL, 'Exception or Error to throw.'), + ]) + ->setDescription('Throw an exception or error out of the Psy Shell.') + ->setHelp( + <<<'HELP' +Throws an exception or error out of the current the Psy Shell instance. + +By default it throws the most recent exception. + +e.g. +>>> throw-up +>>> throw-up $e +>>> throw-up new Exception('WHEEEEEE!') +HELP + ); + } + + /** + * {@inheritdoc} + * + * @throws InvalidArgumentException if there is no exception to throw + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $args = $this->prepareArgs($input->getArgument('exception')); + $throwStmt = new Throw_(new StaticCall(new FullyQualifiedName(self::THROW_CLASS), 'fromThrowable', $args)); + $throwCode = $this->printer->prettyPrint([$throwStmt]); + + $shell = $this->getApplication(); + $shell->addCode($throwCode, !$shell->hasCode()); + } + + /** + * Parse the supplied command argument. + * + * If no argument was given, this falls back to `$_e` + * + * @throws InvalidArgumentException if there is no exception to throw + * + * @param string $code + * + * @return Arg[] + */ + private function prepareArgs($code = null) + { + if (!$code) { + // Default to last exception if nothing else was supplied + return [new Arg(new Variable('_e'))]; + } + + if (strpos('parse($code); + + if (count($expr) !== 1) { + throw new \InvalidArgumentException('No idea how to throw this'); + } + + return [new Arg($expr[0])]; + } + + /** + * Lex and parse a string of code into statements. + * + * @param string $code + * + * @return array Statements + */ + private function parse($code) + { + try { + return $this->parser->parse($code); + } catch (\PhpParser\Error $e) { + if (strpos($e->getMessage(), 'unexpected EOF') === false) { + throw $e; + } + + // If we got an unexpected EOF, let's try it again with a semicolon. + return $this->parser->parse($code . ';'); + } + } +}