|
14 | 14 | use Symfony\Component\Console\Helper\Dumper;
|
15 | 15 | use Symfony\Component\Console\Helper\TableSeparator;
|
16 | 16 | use Symfony\Component\Form\ResolvedFormTypeInterface;
|
| 17 | +use Symfony\Component\HttpKernel\Debug\FileLinkFormatter; |
17 | 18 | use Symfony\Component\OptionsResolver\OptionsResolver;
|
18 | 19 |
|
19 | 20 | /**
|
|
23 | 24 | */
|
24 | 25 | class TextDescriptor extends Descriptor
|
25 | 26 | {
|
| 27 | + private $fileLinkFormatter; |
| 28 | + |
| 29 | + public function __construct(FileLinkFormatter $fileLinkFormatter = null) |
| 30 | + { |
| 31 | + $this->fileLinkFormatter = $fileLinkFormatter; |
| 32 | + } |
| 33 | + |
26 | 34 | protected function describeDefaults(array $options)
|
27 | 35 | {
|
28 | 36 | if ($options['core_types']) {
|
29 | 37 | $this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
|
30 |
| - $shortClassNames = array_map(function ($fqcn) { return \array_slice(explode('\\', $fqcn), -1)[0]; }, $options['core_types']); |
| 38 | + $shortClassNames = array_map(function ($fqcn) { |
| 39 | + return $this->formatClassLink($fqcn, \array_slice(explode('\\', $fqcn), -1)[0]); |
| 40 | + }, $options['core_types']); |
31 | 41 | for ($i = 0, $loopsMax = \count($shortClassNames); $i * 5 < $loopsMax; ++$i) {
|
32 | 42 | $this->output->writeln(' '.implode(', ', \array_slice($shortClassNames, $i * 5, 5)));
|
33 | 43 | }
|
34 | 44 | }
|
35 | 45 |
|
36 | 46 | if ($options['service_types']) {
|
37 | 47 | $this->output->section('Service form types');
|
38 |
| - $this->output->listing($options['service_types']); |
| 48 | + $this->output->listing(array_map([$this, 'formatClassLink'], $options['service_types'])); |
39 | 49 | }
|
40 | 50 |
|
41 | 51 | if (!$options['show_deprecated']) {
|
42 | 52 | if ($options['extensions']) {
|
43 | 53 | $this->output->section('Type extensions');
|
44 |
| - $this->output->listing($options['extensions']); |
| 54 | + $this->output->listing(array_map([$this, 'formatClassLink'], $options['extensions'])); |
45 | 55 | }
|
46 | 56 |
|
47 | 57 | if ($options['guessers']) {
|
48 | 58 | $this->output->section('Type guessers');
|
49 |
| - $this->output->listing($options['guessers']); |
| 59 | + $this->output->listing(array_map([$this, 'formatClassLink'], $options['guessers'])); |
50 | 60 | }
|
51 | 61 | }
|
52 | 62 | }
|
@@ -82,12 +92,12 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
|
82 | 92 |
|
83 | 93 | if ($this->parents) {
|
84 | 94 | $this->output->section('Parent types');
|
85 |
| - $this->output->listing($this->parents); |
| 95 | + $this->output->listing(array_map([$this, 'formatClassLink'], $this->parents)); |
86 | 96 | }
|
87 | 97 |
|
88 | 98 | if ($this->extensions) {
|
89 | 99 | $this->output->section('Type extensions');
|
90 |
| - $this->output->listing($this->extensions); |
| 100 | + $this->output->listing(array_map([$this, 'formatClassLink'], $this->extensions)); |
91 | 101 | }
|
92 | 102 | }
|
93 | 103 |
|
@@ -178,4 +188,32 @@ private function normalizeAndSortOptionsColumns(array $options)
|
178 | 188 |
|
179 | 189 | return $options;
|
180 | 190 | }
|
| 191 | + |
| 192 | + private function formatClassLink(string $class, string $text = null): string |
| 193 | + { |
| 194 | + if (null === $text) { |
| 195 | + $text = $class; |
| 196 | + } |
| 197 | + |
| 198 | + if ('' === $fileLink = $this->getFileLink($class)) { |
| 199 | + return $text; |
| 200 | + } |
| 201 | + |
| 202 | + return sprintf('<href=%s>%s</>', $fileLink, $text); |
| 203 | + } |
| 204 | + |
| 205 | + private function getFileLink(string $class): string |
| 206 | + { |
| 207 | + if (null === $this->fileLinkFormatter) { |
| 208 | + return ''; |
| 209 | + } |
| 210 | + |
| 211 | + try { |
| 212 | + $r = new \ReflectionClass($class); |
| 213 | + } catch (\ReflectionException $e) { |
| 214 | + return ''; |
| 215 | + } |
| 216 | + |
| 217 | + return (string) $this->fileLinkFormatter->format($r->getFileName(), $r->getStartLine()); |
| 218 | + } |
181 | 219 | }
|
0 commit comments