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\Exception\RuntimeException;
|
Chris@0
|
15 use Psy\VarDumper\Presenter;
|
Chris@0
|
16 use Psy\VarDumper\PresenterAware;
|
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\Input\InputOption;
|
Chris@0
|
20 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Dump an object or primitive.
|
Chris@0
|
24 *
|
Chris@0
|
25 * This is like var_dump but *way* awesomer.
|
Chris@0
|
26 */
|
Chris@0
|
27 class DumpCommand extends ReflectingCommand implements PresenterAware
|
Chris@0
|
28 {
|
Chris@0
|
29 private $presenter;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * PresenterAware interface.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param Presenter $presenter
|
Chris@0
|
35 */
|
Chris@0
|
36 public function setPresenter(Presenter $presenter)
|
Chris@0
|
37 {
|
Chris@0
|
38 $this->presenter = $presenter;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * {@inheritdoc}
|
Chris@0
|
43 */
|
Chris@0
|
44 protected function configure()
|
Chris@0
|
45 {
|
Chris@0
|
46 $this
|
Chris@0
|
47 ->setName('dump')
|
Chris@0
|
48 ->setDefinition(array(
|
Chris@0
|
49 new InputArgument('target', InputArgument::REQUIRED, 'A target object or primitive to dump.', null),
|
Chris@0
|
50 new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse', 10),
|
Chris@0
|
51 new InputOption('all', 'a', InputOption::VALUE_NONE, 'Include private and protected methods and properties.'),
|
Chris@0
|
52 ))
|
Chris@0
|
53 ->setDescription('Dump an object or primitive.')
|
Chris@0
|
54 ->setHelp(
|
Chris@0
|
55 <<<'HELP'
|
Chris@0
|
56 Dump an object or primitive.
|
Chris@0
|
57
|
Chris@0
|
58 This is like var_dump but <strong>way</strong> awesomer.
|
Chris@0
|
59
|
Chris@0
|
60 e.g.
|
Chris@0
|
61 <return>>>> dump $_</return>
|
Chris@0
|
62 <return>>>> dump $someVar</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 protected function execute(InputInterface $input, OutputInterface $output)
|
Chris@0
|
71 {
|
Chris@0
|
72 $depth = $input->getOption('depth');
|
Chris@0
|
73 $target = $this->resolveTarget($input->getArgument('target'));
|
Chris@0
|
74 $output->page($this->presenter->present($target, $depth, $input->getOption('all') ? Presenter::VERBOSE : 0));
|
Chris@0
|
75
|
Chris@0
|
76 if (is_object($target)) {
|
Chris@0
|
77 $this->setCommandScopeVariables(new \ReflectionObject($target));
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Resolve dump target name.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @throws RuntimeException if target name does not exist in the current scope
|
Chris@0
|
85 *
|
Chris@0
|
86 * @param string $target
|
Chris@0
|
87 *
|
Chris@0
|
88 * @return mixed
|
Chris@0
|
89 */
|
Chris@0
|
90 protected function resolveTarget($target)
|
Chris@0
|
91 {
|
Chris@0
|
92 $matches = array();
|
Chris@0
|
93 if (preg_match(self::SUPERGLOBAL, $target, $matches)) {
|
Chris@0
|
94 if (!array_key_exists($matches[1], $GLOBALS)) {
|
Chris@0
|
95 throw new RuntimeException('Unknown target: ' . $target);
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 return $GLOBALS[$matches[1]];
|
Chris@0
|
99 } elseif (preg_match(self::INSTANCE, $target, $matches)) {
|
Chris@0
|
100 return $this->getScopeVariable($matches[1]);
|
Chris@0
|
101 } else {
|
Chris@0
|
102 throw new RuntimeException('Unknown target: ' . $target);
|
Chris@0
|
103 }
|
Chris@0
|
104 }
|
Chris@0
|
105 }
|