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