| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of Psy Shell.
|
|
|
5 |
*
|
|
|
6 |
* (c) 2012-2022 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\Input\CodeArgument;
|
|
|
15 |
use Psy\VarDumper\Presenter;
|
|
|
16 |
use Psy\VarDumper\PresenterAware;
|
|
|
17 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
18 |
use Symfony\Component\Console\Input\InputOption;
|
|
|
19 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Dump an object or primitive.
|
|
|
23 |
*
|
|
|
24 |
* This is like var_dump but *way* awesomer.
|
|
|
25 |
*/
|
|
|
26 |
class DumpCommand extends ReflectingCommand implements PresenterAware
|
|
|
27 |
{
|
|
|
28 |
private $presenter;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* PresenterAware interface.
|
|
|
32 |
*
|
|
|
33 |
* @param Presenter $presenter
|
|
|
34 |
*/
|
|
|
35 |
public function setPresenter(Presenter $presenter)
|
|
|
36 |
{
|
|
|
37 |
$this->presenter = $presenter;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* {@inheritdoc}
|
|
|
42 |
*/
|
|
|
43 |
protected function configure()
|
|
|
44 |
{
|
|
|
45 |
$this
|
|
|
46 |
->setName('dump')
|
|
|
47 |
->setDefinition([
|
|
|
48 |
new CodeArgument('target', CodeArgument::REQUIRED, 'A target object or primitive to dump.'),
|
|
|
49 |
new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse.', 10),
|
|
|
50 |
new InputOption('all', 'a', InputOption::VALUE_NONE, 'Include private and protected methods and properties.'),
|
|
|
51 |
])
|
|
|
52 |
->setDescription('Dump an object or primitive.')
|
|
|
53 |
->setHelp(
|
|
|
54 |
<<<'HELP'
|
|
|
55 |
Dump an object or primitive.
|
|
|
56 |
|
|
|
57 |
This is like var_dump but <strong>way</strong> awesomer.
|
|
|
58 |
|
|
|
59 |
e.g.
|
|
|
60 |
<return>>>> dump $_</return>
|
|
|
61 |
<return>>>> dump $someVar</return>
|
|
|
62 |
<return>>>> dump $stuff->getAll()</return>
|
|
|
63 |
HELP
|
|
|
64 |
);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* {@inheritdoc}
|
|
|
69 |
*
|
|
|
70 |
* @return int 0 if everything went fine, or an exit code
|
|
|
71 |
*/
|
|
|
72 |
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
73 |
{
|
|
|
74 |
$depth = $input->getOption('depth');
|
|
|
75 |
$target = $this->resolveCode($input->getArgument('target'));
|
|
|
76 |
$output->page($this->presenter->present($target, $depth, $input->getOption('all') ? Presenter::VERBOSE : 0));
|
|
|
77 |
|
|
|
78 |
if (\is_object($target)) {
|
|
|
79 |
$this->setCommandScopeVariables(new \ReflectionObject($target));
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return 0;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* @deprecated Use `resolveCode` instead
|
|
|
87 |
*
|
|
|
88 |
* @param string $name
|
|
|
89 |
*
|
|
|
90 |
* @return mixed
|
|
|
91 |
*/
|
|
|
92 |
protected function resolveTarget(string $name)
|
|
|
93 |
{
|
|
|
94 |
@\trigger_error('`resolveTarget` is deprecated; use `resolveCode` instead.', \E_USER_DEPRECATED);
|
|
|
95 |
|
|
|
96 |
return $this->resolveCode($name);
|
|
|
97 |
}
|
|
|
98 |
}
|