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