Skip to content

[Form] Add file links for described classes in debug:form command #30826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
<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 guessers are stored here by FormPass -->
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
<tag name="console.command" command="debug:form" />
</service>
</services>
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Form/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\FormRegistryInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;

/**
* A console command for retrieving information about form types.
Expand All @@ -37,8 +38,9 @@ class DebugCommand extends Command
private $types;
private $extensions;
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();

Expand All @@ -47,6 +49,7 @@ public function __construct(FormRegistryInterface $formRegistry, array $namespac
$this->types = $types;
$this->extensions = $extensions;
$this->guessers = $guessers;
$this->fileLinkFormatter = $fileLinkFormatter;
}

/**
Expand Down Expand Up @@ -145,7 +148,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$helper = new DescriptorHelper();
$helper = new DescriptorHelper($this->fileLinkFormatter);
$options['format'] = $input->getOption('format');
$options['show_deprecated'] = $input->getOption('show-deprecated');
$helper->describe($io, $object, $options);
Expand Down
50 changes: 44 additions & 6 deletions src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Helper\Dumper;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -23,30 +24,39 @@
*/
class TextDescriptor extends Descriptor
{
private $fileLinkFormatter;

public function __construct(FileLinkFormatter $fileLinkFormatter = null)
{
$this->fileLinkFormatter = $fileLinkFormatter;
}

protected function describeDefaults(array $options)
{
if ($options['core_types']) {
$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) {
$this->output->writeln(' '.implode(', ', \array_slice($shortClassNames, $i * 5, 5)));
}
}

if ($options['service_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['extensions']) {
$this->output->section('Type extensions');
$this->output->listing($options['extensions']);
$this->output->listing(array_map([$this, 'formatClassLink'], $options['extensions']));
}

if ($options['guessers']) {
$this->output->section('Type guessers');
$this->output->listing($options['guessers']);
$this->output->listing(array_map([$this, 'formatClassLink'], $options['guessers']));
}
}
}
Expand Down Expand Up @@ -82,12 +92,12 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF

if ($this->parents) {
$this->output->section('Parent types');
$this->output->listing($this->parents);
$this->output->listing(array_map([$this, 'formatClassLink'], $this->parents));
}

if ($this->extensions) {
$this->output->section('Type extensions');
$this->output->listing($this->extensions);
$this->output->listing(array_map([$this, 'formatClassLink'], $this->extensions));
}
}

Expand Down Expand Up @@ -178,4 +188,32 @@ private function normalizeAndSortOptionsColumns(array $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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper;
use Symfony\Component\Form\Console\Descriptor\JsonDescriptor;
use Symfony\Component\Form\Console\Descriptor\TextDescriptor;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;

/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
Expand All @@ -22,10 +23,10 @@
*/
class DescriptorHelper extends BaseDescriptorHelper
{
public function __construct()
public function __construct(FileLinkFormatter $fileLinkFormatter = null)
{
$this
->register('txt', new TextDescriptor())
->register('txt', new TextDescriptor($fileLinkFormatter))
->register('json', new JsonDescriptor())
;
}
Expand Down