comparison vendor/psy/psysh/src/Psy/Command/ThrowUpCommand.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of Psy Shell.
5 *
6 * (c) 2012-2017 Justin Hileman
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Psy\Command;
13
14 use Psy\Context;
15 use Psy\ContextAware;
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;
20
21 /**
22 * Throw an exception out of the Psy Shell.
23 */
24 class ThrowUpCommand extends Command implements ContextAware
25 {
26 /**
27 * Context instance (for ContextAware interface).
28 *
29 * @var Context
30 */
31 protected $context;
32
33 /**
34 * ContextAware interface.
35 *
36 * @param Context $context
37 */
38 public function setContext(Context $context)
39 {
40 $this->context = $context;
41 }
42
43 /**
44 * {@inheritdoc}
45 */
46 protected function configure()
47 {
48 $this
49 ->setName('throw-up')
50 ->setDefinition(array(
51 new InputArgument('exception', InputArgument::OPTIONAL, 'Exception to throw'),
52 ))
53 ->setDescription('Throw an exception out of the Psy Shell.')
54 ->setHelp(
55 <<<'HELP'
56 Throws an exception out of the current the Psy Shell instance.
57
58 By default it throws the most recent exception.
59
60 e.g.
61 <return>>>> throw-up</return>
62 <return>>>> throw-up $e</return>
63 HELP
64 );
65 }
66
67 /**
68 * {@inheritdoc}
69 *
70 * @throws InvalidArgumentException if there is no exception to throw
71 * @throws ThrowUpException because what else do you expect it to do?
72 */
73 protected function execute(InputInterface $input, OutputInterface $output)
74 {
75 if ($name = $input->getArgument('exception')) {
76 $orig = $this->context->get(preg_replace('/^\$/', '', $name));
77 } else {
78 $orig = $this->context->getLastException();
79 }
80
81 if (!$orig instanceof \Exception) {
82 throw new \InvalidArgumentException('throw-up can only throw Exceptions');
83 }
84
85 throw new ThrowUpException($orig);
86 }
87 }