/
DebugCommand.php
238 lines (192 loc) · 7.36 KB
/
DebugCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Twig\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Twig\Environment;
/**
* Lists twig functions, filters, globals and tests present in the current project.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class DebugCommand extends Command
{
protected static $defaultName = 'debug:twig';
private $twig;
/**
* @param Environment $twig
*/
public function __construct($twig = null)
{
if (!$twig instanceof Environment) {
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
parent::__construct($twig);
return;
}
parent::__construct();
$this->twig = $twig;
}
public function setTwigEnvironment(Environment $twig)
{
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
$this->twig = $twig;
}
/**
* @return Environment $twig
*/
protected function getTwigEnvironment()
{
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
return $this->twig;
}
protected function configure()
{
$this
->setDefinition(array(
new InputArgument('filter', InputArgument::OPTIONAL, 'Show details for all entries matching this filter'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (text or json)', 'text'),
))
->setDescription('Shows a list of twig functions, filters, globals and tests')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command outputs a list of twig functions,
filters, globals and tests. Output can be filtered with an optional argument.
<info>php %command.full_name%</info>
The command lists all functions, filters, etc.
<info>php %command.full_name% date</info>
The command lists everything that contains the word date.
<info>php %command.full_name% --format=json</info>
The command lists everything in a machine readable json format.
EOF
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
// BC to be removed in 4.0
if (__CLASS__ !== get_class($this)) {
$r = new \ReflectionMethod($this, 'getTwigEnvironment');
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0. Construct the command with its required arguments instead.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED);
$this->twig = $this->getTwigEnvironment();
}
}
if (null === $this->twig) {
throw new \RuntimeException('The Twig environment needs to be set.');
}
$types = array('functions', 'filters', 'tests', 'globals');
if ($input->getOption('format') === 'json') {
$data = array();
foreach ($types as $type) {
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
$data[$type][$name] = $this->getMetadata($type, $entity);
}
}
$data['tests'] = array_keys($data['tests']);
$io->writeln(json_encode($data));
return 0;
}
$filter = $input->getArgument('filter');
foreach ($types as $index => $type) {
$items = array();
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
if (!$filter || false !== strpos($name, $filter)) {
$items[$name] = $name.$this->getPrettyMetadata($type, $entity);
}
}
if (!$items) {
continue;
}
$io->section(ucfirst($type));
ksort($items);
$io->listing($items);
}
return 0;
}
private function getMetadata($type, $entity)
{
if ($type === 'globals') {
return $entity;
}
if ($type === 'tests') {
return;
}
if ($type === 'functions' || $type === 'filters') {
$cb = $entity->getCallable();
if (null === $cb) {
return;
}
if (is_array($cb)) {
if (!method_exists($cb[0], $cb[1])) {
return;
}
$refl = new \ReflectionMethod($cb[0], $cb[1]);
} elseif (is_object($cb) && method_exists($cb, '__invoke')) {
$refl = new \ReflectionMethod($cb, '__invoke');
} elseif (function_exists($cb)) {
$refl = new \ReflectionFunction($cb);
} elseif (is_string($cb) && preg_match('{^(.+)::(.+)$}', $cb, $m) && method_exists($m[1], $m[2])) {
$refl = new \ReflectionMethod($m[1], $m[2]);
} else {
throw new \UnexpectedValueException('Unsupported callback type');
}
$args = $refl->getParameters();
// filter out context/environment args
if ($entity->needsEnvironment()) {
array_shift($args);
}
if ($entity->needsContext()) {
array_shift($args);
}
if ($type === 'filters') {
// remove the value the filter is applied on
array_shift($args);
}
// format args
$args = array_map(function ($param) {
if ($param->isDefaultValueAvailable()) {
return $param->getName().' = '.json_encode($param->getDefaultValue());
}
return $param->getName();
}, $args);
return $args;
}
}
private function getPrettyMetadata($type, $entity)
{
if ($type === 'tests') {
return '';
}
try {
$meta = $this->getMetadata($type, $entity);
if ($meta === null) {
return '(unknown?)';
}
} catch (\UnexpectedValueException $e) {
return ' <error>'.$e->getMessage().'</error>';
}
if ($type === 'globals') {
if (is_object($meta)) {
return ' = object('.get_class($meta).')';
}
return ' = '.substr(@json_encode($meta), 0, 50);
}
if ($type === 'functions') {
return '('.implode(', ', $meta).')';
}
if ($type === 'filters') {
return $meta ? '('.implode(', ', $meta).')' : '';
}
}
}