Add file links for described classes

This commit is contained in:
Yonel Ceruto 2019-04-01 16:27:30 -04:00
parent 9bcea2e9f4
commit dcba01d212
4 changed files with 53 additions and 10 deletions

View File

@ -160,6 +160,7 @@
<argument type="collection" /> <!-- All services form types are stored here by FormPass --> <argument type="collection" /> <!-- All services form types are stored here by FormPass -->
<argument type="collection" /> <!-- All type extensions are stored here by FormPass --> <argument type="collection" /> <!-- All type extensions are stored here by FormPass -->
<argument type="collection" /> <!-- All type guessers are stored here by FormPass --> <argument type="collection" /> <!-- All type guessers are stored here by FormPass -->
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
<tag name="console.command" command="debug:form" /> <tag name="console.command" command="debug:form" />
</service> </service>
</services> </services>

View File

@ -22,6 +22,7 @@ use Symfony\Component\Form\Console\Helper\DescriptorHelper;
use Symfony\Component\Form\Extension\Core\CoreExtension; use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\FormRegistryInterface; use Symfony\Component\Form\FormRegistryInterface;
use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
/** /**
* A console command for retrieving information about form types. * A console command for retrieving information about form types.
@ -37,8 +38,9 @@ class DebugCommand extends Command
private $types; private $types;
private $extensions; private $extensions;
private $guessers; private $guessers;
private $fileLinkFormatter;
public function __construct(FormRegistryInterface $formRegistry, array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [], array $extensions = [], array $guessers = []) public function __construct(FormRegistryInterface $formRegistry, array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [], array $extensions = [], array $guessers = [], FileLinkFormatter $fileLinkFormatter = null)
{ {
parent::__construct(); parent::__construct();
@ -47,6 +49,7 @@ class DebugCommand extends Command
$this->types = $types; $this->types = $types;
$this->extensions = $extensions; $this->extensions = $extensions;
$this->guessers = $guessers; $this->guessers = $guessers;
$this->fileLinkFormatter = $fileLinkFormatter;
} }
/** /**
@ -145,7 +148,7 @@ EOF
} }
} }
$helper = new DescriptorHelper(); $helper = new DescriptorHelper($this->fileLinkFormatter);
$options['format'] = $input->getOption('format'); $options['format'] = $input->getOption('format');
$options['show_deprecated'] = $input->getOption('show-deprecated'); $options['show_deprecated'] = $input->getOption('show-deprecated');
$helper->describe($io, $object, $options); $helper->describe($io, $object, $options);

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Console\Descriptor;
use Symfony\Component\Console\Helper\Dumper; use Symfony\Component\Console\Helper\Dumper;
use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Form\ResolvedFormTypeInterface; use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
/** /**
@ -23,11 +24,20 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/ */
class TextDescriptor extends Descriptor class TextDescriptor extends Descriptor
{ {
private $fileLinkFormatter;
public function __construct(FileLinkFormatter $fileLinkFormatter = null)
{
$this->fileLinkFormatter = $fileLinkFormatter;
}
protected function describeDefaults(array $options) protected function describeDefaults(array $options)
{ {
if ($options['core_types']) { if ($options['core_types']) {
$this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)'); $this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
$shortClassNames = array_map(function ($fqcn) { return \array_slice(explode('\\', $fqcn), -1)[0]; }, $options['core_types']); $shortClassNames = array_map(function ($fqcn) {
return $this->formatClassLink($fqcn, \array_slice(explode('\\', $fqcn), -1)[0]);
}, $options['core_types']);
for ($i = 0, $loopsMax = \count($shortClassNames); $i * 5 < $loopsMax; ++$i) { for ($i = 0, $loopsMax = \count($shortClassNames); $i * 5 < $loopsMax; ++$i) {
$this->output->writeln(' '.implode(', ', \array_slice($shortClassNames, $i * 5, 5))); $this->output->writeln(' '.implode(', ', \array_slice($shortClassNames, $i * 5, 5)));
} }
@ -35,18 +45,18 @@ class TextDescriptor extends Descriptor
if ($options['service_types']) { if ($options['service_types']) {
$this->output->section('Service form types'); $this->output->section('Service form types');
$this->output->listing($options['service_types']); $this->output->listing(array_map([$this, 'formatClassLink'], $options['service_types']));
} }
if (!$options['show_deprecated']) { if (!$options['show_deprecated']) {
if ($options['extensions']) { if ($options['extensions']) {
$this->output->section('Type extensions'); $this->output->section('Type extensions');
$this->output->listing($options['extensions']); $this->output->listing(array_map([$this, 'formatClassLink'], $options['extensions']));
} }
if ($options['guessers']) { if ($options['guessers']) {
$this->output->section('Type guessers'); $this->output->section('Type guessers');
$this->output->listing($options['guessers']); $this->output->listing(array_map([$this, 'formatClassLink'], $options['guessers']));
} }
} }
} }
@ -82,12 +92,12 @@ class TextDescriptor extends Descriptor
if ($this->parents) { if ($this->parents) {
$this->output->section('Parent types'); $this->output->section('Parent types');
$this->output->listing($this->parents); $this->output->listing(array_map([$this, 'formatClassLink'], $this->parents));
} }
if ($this->extensions) { if ($this->extensions) {
$this->output->section('Type extensions'); $this->output->section('Type extensions');
$this->output->listing($this->extensions); $this->output->listing(array_map([$this, 'formatClassLink'], $this->extensions));
} }
} }
@ -178,4 +188,32 @@ class TextDescriptor extends Descriptor
return $options; return $options;
} }
private function formatClassLink(string $class, string $text = null): string
{
if (null === $text) {
$text = $class;
}
if ('' === $fileLink = $this->getFileLink($class)) {
return $text;
}
return sprintf('<href=%s>%s</>', $fileLink, $text);
}
private function getFileLink(string $class): string
{
if (null === $this->fileLinkFormatter) {
return '';
}
try {
$r = new \ReflectionClass($class);
} catch (\ReflectionException $e) {
return '';
}
return (string) $this->fileLinkFormatter->format($r->getFileName(), $r->getStartLine());
}
} }

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Console\Helper;
use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper; use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper;
use Symfony\Component\Form\Console\Descriptor\JsonDescriptor; use Symfony\Component\Form\Console\Descriptor\JsonDescriptor;
use Symfony\Component\Form\Console\Descriptor\TextDescriptor; use Symfony\Component\Form\Console\Descriptor\TextDescriptor;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
/** /**
* @author Yonel Ceruto <yonelceruto@gmail.com> * @author Yonel Ceruto <yonelceruto@gmail.com>
@ -22,10 +23,10 @@ use Symfony\Component\Form\Console\Descriptor\TextDescriptor;
*/ */
class DescriptorHelper extends BaseDescriptorHelper class DescriptorHelper extends BaseDescriptorHelper
{ {
public function __construct() public function __construct(FileLinkFormatter $fileLinkFormatter = null)
{ {
$this $this
->register('txt', new TextDescriptor()) ->register('txt', new TextDescriptor($fileLinkFormatter))
->register('json', new JsonDescriptor()) ->register('json', new JsonDescriptor())
; ;
} }