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;
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;
23 * Dump an object or primitive.
25 * This is like var_dump but *way* awesomer.
27 class DumpCommand extends ReflectingCommand implements PresenterAware
32 * PresenterAware interface.
34 * @param Presenter $presenter
36 public function setPresenter(Presenter $presenter)
38 $this->presenter = $presenter;
44 protected function configure()
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.'),
53 ->setDescription('Dump an object or primitive.')
56 Dump an object or primitive.
58 This is like var_dump but <strong>way</strong> awesomer.
61 <return>>>> dump $_</return>
62 <return>>>> dump $someVar</return>
70 protected function execute(InputInterface $input, OutputInterface $output)
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));
76 if (is_object($target)) {
77 $this->setCommandScopeVariables(new \ReflectionObject($target));
82 * Resolve dump target name.
84 * @throws RuntimeException if target name does not exist in the current scope
86 * @param string $target
90 protected function resolveTarget($target)
93 if (preg_match(self::SUPERGLOBAL, $target, $matches)) {
94 if (!array_key_exists($matches[1], $GLOBALS)) {
95 throw new RuntimeException('Unknown target: ' . $target);
98 return $GLOBALS[$matches[1]];
99 } elseif (preg_match(self::INSTANCE, $target, $matches)) {
100 return $this->getScopeVariable($matches[1]);
102 throw new RuntimeException('Unknown target: ' . $target);