4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
16 use Psy\Exception\ThrowUpException;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
22 * Throw an exception out of the Psy Shell.
24 class ThrowUpCommand extends Command implements ContextAware
27 * Context instance (for ContextAware interface).
34 * ContextAware interface.
36 * @param Context $context
38 public function setContext(Context $context)
40 $this->context = $context;
46 protected function configure()
50 ->setDefinition(array(
51 new InputArgument('exception', InputArgument::OPTIONAL, 'Exception to throw'),
53 ->setDescription('Throw an exception out of the Psy Shell.')
56 Throws an exception out of the current the Psy Shell instance.
58 By default it throws the most recent exception.
61 <return>>>> throw-up</return>
62 <return>>>> throw-up $e</return>
70 * @throws InvalidArgumentException if there is no exception to throw
71 * @throws ThrowUpException because what else do you expect it to do?
73 protected function execute(InputInterface $input, OutputInterface $output)
75 if ($name = $input->getArgument('exception')) {
76 $orig = $this->context->get(preg_replace('/^\$/', '', $name));
78 $orig = $this->context->getLastException();
81 if (!$orig instanceof \Exception) {
82 throw new \InvalidArgumentException('throw-up can only throw Exceptions');
85 throw new ThrowUpException($orig);