Skip to content

[FrameworkBundle] Display aliases in debug:container command #47427

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextContains(string $selector, string $text)`
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextSame(string $selector, string $text)`
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextNotContains(string $selector, string $text)`
* Display aliases in debug:container command
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Display aliases in debug:container command
* Display aliases in `debug:container` command

* Deprecate `EnableLoggerDebugModePass`, use argument `$debug` of HttpKernel's `Logger` instead
* Deprecate `AddDebugLogProcessorPass::configureLogger()`, use HttpKernel's `DebugLoggerConfigurator` instead
* Deprecate not setting the `framework.handle_all_throwables` config option; it will default to `true` in 7.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected function configure(): void
->setDefinition([
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
new InputOption('show-arguments', null, InputOption::VALUE_NONE, 'Show arguments in services'),
new InputOption('with-aliases', null, InputOption::VALUE_NONE, 'Display aliases for a single service'),
new InputOption('show-hidden', null, InputOption::VALUE_NONE, 'Show hidden (internal) services'),
new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Show all services with a specific tag'),
new InputOption('tags', null, InputOption::VALUE_NONE, 'Display tagged services for an application'),
Expand Down Expand Up @@ -153,6 +154,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$helper = new DescriptorHelper();
$options['format'] = $input->getOption('format');
$options['show_arguments'] = $input->getOption('show-arguments');
$options['with_aliases'] = $input->getOption('with-aliases');
$options['show_hidden'] = $input->getOption('show-hidden');
$options['raw_text'] = $input->getOption('raw');
$options['output'] = $io;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function describeContainerService(object $service, array $options = []
if ($service instanceof Alias) {
$this->describeContainerAlias($service, $options, $container);
} elseif ($service instanceof Definition) {
$this->writeData($this->getContainerDefinitionData($service, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $container, $options['id']), $options);
$this->writeData($this->getContainerDefinitionData($service, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $container, $options['id'], isset($options['with_aliases']) && $options['with_aliases']), $options);
} else {
$this->writeData($service::class, $options);
}
Expand All @@ -90,6 +90,7 @@ protected function describeContainerServices(ContainerBuilder $container, array
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
$omitTags = isset($options['omit_tags']) && $options['omit_tags'];
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
$withAliases = isset($options['with_aliases']) && $options['with_aliases'];
$data = ['definitions' => [], 'aliases' => [], 'services' => []];

if (isset($options['filter'])) {
Expand All @@ -109,7 +110,7 @@ protected function describeContainerServices(ContainerBuilder $container, array
if ($service->hasTag('container.excluded')) {
continue;
}
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, $omitTags, $showArguments, $container, $serviceId);
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, $omitTags, $showArguments, $container, $serviceId, $withAliases);
} else {
$data['services'][$serviceId] = $service::class;
}
Expand All @@ -120,7 +121,7 @@ protected function describeContainerServices(ContainerBuilder $container, array

protected function describeContainerDefinition(Definition $definition, array $options = [], ContainerBuilder $container = null): void
{
$this->writeData($this->getContainerDefinitionData($definition, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $container, $options['id'] ?? null), $options);
$this->writeData($this->getContainerDefinitionData($definition, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $container, $options['id'] ?? null, isset($options['with_aliases']) && $options['with_aliases']), $options);
}

protected function describeContainerAlias(Alias $alias, array $options = [], ContainerBuilder $container = null): void
Expand All @@ -132,7 +133,7 @@ protected function describeContainerAlias(Alias $alias, array $options = [], Con
}

$this->writeData(
[$this->getContainerAliasData($alias), $this->getContainerDefinitionData($container->getDefinition((string) $alias), isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $container, (string) $alias)],
[$this->getContainerAliasData($alias), $this->getContainerDefinitionData($container->getDefinition((string) $alias), isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $container, (string) $alias, isset($options['with_aliases']) && $options['with_aliases'])],
array_merge($options, ['id' => (string) $alias])
);
}
Expand Down Expand Up @@ -220,7 +221,7 @@ protected function getRouteData(Route $route): array
return $data;
}

private function getContainerDefinitionData(Definition $definition, bool $omitTags = false, bool $showArguments = false, ContainerBuilder $container = null, string $id = null): array
private function getContainerDefinitionData(Definition $definition, bool $omitTags = false, bool $showArguments = false, ContainerBuilder $container = null, string $id = null, bool $withAliases = false): array
{
$data = [
'class' => (string) $definition->getClass(),
Expand All @@ -245,7 +246,7 @@ private function getContainerDefinitionData(Definition $definition, bool $omitTa
}

if ($showArguments) {
$data['arguments'] = $this->describeValue($definition->getArguments(), $omitTags, $showArguments, $container, $id);
$data['arguments'] = $this->describeValue($definition->getArguments(), $omitTags, $showArguments, $container, $id, $withAliases);
}

$data['file'] = $definition->getFile();
Expand Down Expand Up @@ -393,12 +394,12 @@ private function getCallableData(mixed $callable): array
throw new \InvalidArgumentException('Callable is not describable.');
}

private function describeValue($value, bool $omitTags, bool $showArguments, ContainerBuilder $container = null, string $id = null): mixed
private function describeValue($value, bool $omitTags, bool $showArguments, ContainerBuilder $container = null, string $id = null, bool $withAliases = false): mixed
{
if (\is_array($value)) {
$data = [];
foreach ($value as $k => $v) {
$data[$k] = $this->describeValue($v, $omitTags, $showArguments, $container, $id);
$data[$k] = $this->describeValue($v, $omitTags, $showArguments, $container, $id, $withAliases);
}

return $data;
Expand All @@ -409,10 +410,27 @@ private function describeValue($value, bool $omitTags, bool $showArguments, Cont
}

if ($value instanceof Reference) {
return [
'type' => 'service',
'id' => (string) $value,
];
try {
$node = $container?->getCompiler()?->getServiceReferenceGraph()?->getNode((string) $value);

if ($withAliases && $node?->getValue()?->getClass()) {
return [
'alias' => $node->getValue()->getClass(),
'type' => 'service',
'id' => (string) $value,
];
} else {
return [
'type' => 'service',
'id' => (string) $value,
];
}
} catch (\Throwable $throwable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be more specific? what kind of exceptions can we expect here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlslew Any feedback?

return [
'type' => 'service',
'id' => (string) $value,
];
}
}

if ($value instanceof AbstractArgument) {
Expand All @@ -424,7 +442,7 @@ private function describeValue($value, bool $omitTags, bool $showArguments, Cont
}

if ($value instanceof Definition) {
return $this->getContainerDefinitionData($value, $omitTags, $showArguments, $container, $id);
return $this->getContainerDefinitionData($value, $omitTags, $showArguments, $container, $id, $withAliases);
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,20 @@ protected function describeContainerDefinition(Definition $definition, array $op
}

if (isset($options['show_arguments']) && $options['show_arguments']) {
$output .= "\n".'- Arguments: '.($definition->getArguments() ? 'yes' : 'no');
if ($definition->getArguments()) {
foreach ($definition->getArguments() as $argument) {
$output .= "\n".'- Argument: `'.$argument.'`';
if (isset($options['with_aliases']) && $options['with_aliases']) {
try {
$node = $container?->getCompiler()?->getServiceReferenceGraph()?->getNode((string) $argument);
$output .= "\n".' - Alias: '.$node?->getValue()?->getClass();
} catch (\Throwable $throwable) {
}
}
}
} else {
$output .= "\n".'- Arguments: no';
}
}

if ($definition->getFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,24 @@ protected function describeContainerDefinition(Definition $definition, array $op
}

$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
$withAliases = isset($options['with_aliases']) && $options['with_aliases'];
$argumentsInformation = [];
if ($showArguments && ($arguments = $definition->getArguments())) {
foreach ($arguments as $argument) {
if ($argument instanceof ServiceClosureArgument) {
$argument = $argument->getValues()[0];
}
if ($argument instanceof Reference) {
$argumentsInformation[] = sprintf('Service(%s)', (string) $argument);
$node = $container?->getCompiler()?->getServiceReferenceGraph()?->getNode((string) $argument);

if ($withAliases && $node?->getValue()?->getClass()) {
$argumentsInformation[] = vsprintf('Alias(%s, Service(%s))', [
$node->getValue()->getClass(),
(string) $argument,
]);
} else {
$argumentsInformation[] = sprintf('Service(%s)', (string) $argument);
}
} elseif ($argument instanceof IteratorArgument) {
if ($argument instanceof TaggedIteratorArgument) {
$argumentsInformation[] = sprintf('Tagged Iterator for "%s"%s', $argument->getTag(), $options['is_debug'] ? '' : sprintf(' (%d element(s))', \count($argument->getValues())));
Expand Down
Loading