Skip to content

Updated the styles of the container commands #15983

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
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 Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Question\ChoiceQuestion;

/**
* A console command for retrieving information about services.
Expand Down Expand Up @@ -94,8 +94,9 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
if (false !== strpos($input->getFirstArgument(), ':d')) {
$output->writeln('<comment>The use of "container:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:container" instead.</comment>');
$output->caution('The use of "container:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:container" instead.');
}

$this->validateInput($input);
Expand Down Expand Up @@ -124,10 +125,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$helper = new DescriptorHelper();
$options['format'] = $input->getOption('format');
$options['raw_text'] = $input->getOption('raw');
$options['output'] = $output;
$helper->describe($output, $object, $options);

if (!$input->getArgument('name') && $input->isInteractive()) {
$output->writeln('To search for a service, re-run this command with a search term. <comment>debug:container log</comment>');
$output->comment('To search for a specific service, re-run this command with a search term. (e.g. <comment>debug:container log</comment>)');
}
}

Expand Down Expand Up @@ -186,7 +188,7 @@ protected function getContainerBuilder()
return $this->containerBuilder = $container;
}

private function findProperServiceName(InputInterface $input, OutputInterface $output, ContainerBuilder $builder, $name)
private function findProperServiceName(InputInterface $input, SymfonyStyle $output, ContainerBuilder $builder, $name)
{
if ($builder->has($name) || !$input->isInteractive()) {
return $name;
Expand All @@ -197,10 +199,7 @@ private function findProperServiceName(InputInterface $input, OutputInterface $o
throw new \InvalidArgumentException(sprintf('No services found that match "%s".', $name));
}

$question = new ChoiceQuestion('Choose a number for more information on the service', $matchingServices);
$question->setErrorMessage('Service %s is invalid.');

return $this->getHelper('question')->ask($input, $output, $question);
return $output->choice('Select one of the following services to display its information', $matchingServices);
}

private function findServiceIdsContaining(ContainerBuilder $builder, $name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,15 @@ protected function describeRoute(Route $route, array $options = array())
*/
protected function describeContainerParameters(ParameterBag $parameters, array $options = array())
{
$table = new Table($this->getOutput());
$table->setStyle('compact');
$table->setHeaders(array('Parameter', 'Value'));
$tableHeaders = array('Parameter', 'Value');

$tableRows = array();
foreach ($this->sortParameters($parameters) as $parameter => $value) {
$table->addRow(array($parameter, $this->formatParameter($value)));
$tableRows[] = array($parameter, $this->formatParameter($value));
}

$this->writeText($this->formatSection('container', 'List of parameters')."\n", $options);
$table->render();
$options['output']->title('Symfony Container Parameters');
$options['output']->table($tableHeaders, $tableRows);
}

/**
Expand All @@ -123,15 +122,17 @@ protected function describeContainerParameters(ParameterBag $parameters, array $
protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
{
$showPrivate = isset($options['show_private']) && $options['show_private'];
$description = array($this->formatSection('container', 'Tagged services'));

foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
$description[] = $this->formatSection('tag', $tag);
$description = array_merge($description, array_keys($definitions));
$description[] = '';
if ($showPrivate) {
$options['output']->title('Symfony Container Public and Private Tags');
} else {
$options['output']->title('Symfony Container Public Tags');
}

$this->writeText(implode("\n", $description), $options);
foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
$options['output']->section(sprintf('"%s" tag', $tag));
$options['output']->listing(array_keys($definitions));
}
}

/**
Expand All @@ -148,11 +149,13 @@ protected function describeContainerService($service, array $options = array())
} elseif ($service instanceof Definition) {
$this->describeContainerDefinition($service, $options);
} else {
$description = $this->formatSection('container', sprintf('Information for service <info>%s</info>', $options['id']))
."\n".sprintf('<comment>Service Id</comment> %s', isset($options['id']) ? $options['id'] : '-')
."\n".sprintf('<comment>Class</comment> %s', get_class($service));

$this->writeText($description, $options);
$options['output']->title(sprintf('Information for Service "<info>%s</info>"', $options['id']));
$options['output']->table(
array('Service ID', 'Class'),
array(
array(isset($options['id']) ? $options['id'] : '-', get_class($service)),
)
);
}
}

Expand All @@ -165,16 +168,16 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$showTag = isset($options['tag']) ? $options['tag'] : null;

if ($showPrivate) {
$label = '<comment>Public</comment> and <comment>private</comment> services';
$title = 'Symfony Container Public and Private Services';
} else {
$label = '<comment>Public</comment> services';
$title = 'Symfony Container Public Services';
}

if ($showTag) {
$label .= ' with tag <info>'.$options['tag'].'</info>';
$title .= sprintf(' Tagged with "%s" Tag', $options['tag']);
}

$this->writeText($this->formatSection('container', $label)."\n", $options);
$options['output']->title($title);

$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$maxTags = array();
Expand Down Expand Up @@ -206,10 +209,8 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$tagsCount = count($maxTags);
$tagsNames = array_keys($maxTags);

$table = new Table($this->getOutput());
$table->setStyle('compact');
$table->setHeaders(array_merge(array('Service ID'), $tagsNames, array('Class name')));

$tableHeaders = array_merge(array('Service ID'), $tagsNames, array('Class name'));
$tableRows = array();
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
$definition = $this->resolveServiceDefinition($builder, $serviceId);
if ($definition instanceof Definition) {
Expand All @@ -220,112 +221,125 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$tagValues[] = isset($tag[$tagName]) ? $tag[$tagName] : '';
}
if (0 === $key) {
$table->addRow(array_merge(array($serviceId), $tagValues, array($definition->getClass())));
$tableRows[] = array_merge(array($serviceId), $tagValues, array($definition->getClass()));
} else {
$table->addRow(array_merge(array(' "'), $tagValues, array('')));
$tableRows[] = array_merge(array(' "'), $tagValues, array(''));
}
}
} else {
$table->addRow(array($serviceId, $definition->getClass()));
$tableRows[] = array($serviceId, $definition->getClass());
}
} elseif ($definition instanceof Alias) {
$alias = $definition;
$table->addRow(array_merge(array($serviceId, sprintf('alias for "%s"', $alias)), $tagsCount ? array_fill(0, $tagsCount, '') : array()));
$tableRows[] = array_merge(array($serviceId, sprintf('alias for "%s"', $alias)), $tagsCount ? array_fill(0, $tagsCount, '') : array());
} else {
// we have no information (happens with "service_container")
$table->addRow(array_merge(array($serviceId, get_class($definition)), $tagsCount ? array_fill(0, $tagsCount, '') : array()));
$tableRows[] = array_merge(array($serviceId, get_class($definition)), $tagsCount ? array_fill(0, $tagsCount, '') : array());
}
}

$table->render();
$options['output']->table($tableHeaders, $tableRows);
}

/**
* {@inheritdoc}
*/
protected function describeContainerDefinition(Definition $definition, array $options = array())
{
$description = isset($options['id'])
? array($this->formatSection('container', sprintf('Information for service <info>%s</info>', $options['id'])))
: array();
if (isset($options['id'])) {
$options['output']->title(sprintf('Information for Service "<info>%s</info>"', $options['id']));
}

$description[] = sprintf('<comment>Service Id</comment> %s', isset($options['id']) ? $options['id'] : '-');
$description[] = sprintf('<comment>Class</comment> %s', $definition->getClass() ?: '-');
$tableHeaders = array('Option', 'Value');

$tableRows[] = array('Service ID', isset($options['id']) ? $options['id'] : '-');
$tableRows[] = array('Class', $definition->getClass() ?: '-');

$tags = $definition->getTags();
if (count($tags)) {
$description[] = '<comment>Tags</comment>';
$tagInformation = '';
foreach ($tags as $tagName => $tagData) {
foreach ($tagData as $parameters) {
$description[] = sprintf(' - %-30s (%s)', $tagName, implode(', ', array_map(function ($key, $value) {
foreach ($tagData as $tagParameters) {
$parameters = array_map(function ($key, $value) {
return sprintf('<info>%s</info>: %s', $key, $value);
}, array_keys($parameters), array_values($parameters))));
}, array_keys($tagParameters), array_values($tagParameters));
$parameters = implode(', ', $parameters);

if ('' === $parameters) {
$tagInformation .= sprintf('%s', $tagName);
} else {
$tagInformation .= sprintf('%s (%s)', $tagName, $parameters);
}
}
}
} else {
$description[] = '<comment>Tags</comment> -';
$tagInformation = '-';
}
$tableRows[] = array('Tags', $tagInformation);

$tableRows[] = array('Scope', $definition->getScope(false));
$tableRows[] = array('Public', $definition->isPublic() ? 'yes' : 'no');
$tableRows[] = array('Synthetic', $definition->isSynthetic() ? 'yes' : 'no');
$tableRows[] = array('Lazy', $definition->isLazy() ? 'yes' : 'no');

$description[] = sprintf('<comment>Scope</comment> %s', $definition->getScope(false));
$description[] = sprintf('<comment>Public</comment> %s', $definition->isPublic() ? 'yes' : 'no');
$description[] = sprintf('<comment>Synthetic</comment> %s', $definition->isSynthetic() ? 'yes' : 'no');
$description[] = sprintf('<comment>Lazy</comment> %s', $definition->isLazy() ? 'yes' : 'no');
if (method_exists($definition, 'isShared')) {
$description[] = sprintf('<comment>Shared</comment> %s', $definition->isShared() ? 'yes' : 'no');
}
if (method_exists($definition, 'isSynchronized')) {
$description[] = sprintf('<comment>Synchronized</comment> %s', $definition->isSynchronized(false) ? 'yes' : 'no');
$tableRows[] = array('Synchronized', $definition->isSynchronized(false) ? 'yes' : 'no');
}
$description[] = sprintf('<comment>Abstract</comment> %s', $definition->isAbstract() ? 'yes' : 'no');
$tableRows[] = array('Abstract', $definition->isAbstract() ? 'yes' : 'no');

if ($definition->getFile()) {
$description[] = sprintf('<comment>Required File</comment> %s', $definition->getFile() ?: '-');
$tableRows[] = array('Required File', $definition->getFile() ? $definition->getFile() : '-');
}

if ($definition->getFactoryClass(false)) {
$description[] = sprintf('<comment>Factory Class</comment> %s', $definition->getFactoryClass(false));
$tableRows[] = array('Factory Class', $definition->getFactoryClass(false));
}

if ($definition->getFactoryService(false)) {
$description[] = sprintf('<comment>Factory Service</comment> %s', $definition->getFactoryService(false));
$tableRows[] = array('Factory Service', $definition->getFactoryService(false));
}

if ($definition->getFactoryMethod(false)) {
$description[] = sprintf('<comment>Factory Method</comment> %s', $definition->getFactoryMethod(false));
$tableRows[] = array('Factory Method', $definition->getFactoryMethod(false));
}

if ($factory = $definition->getFactory()) {
if (is_array($factory)) {
if ($factory[0] instanceof Reference) {
$description[] = sprintf('<comment>Factory Service</comment> %s', $factory[0]);
$tableRows[] = array('Factory Service', $factory[0]);
} elseif ($factory[0] instanceof Definition) {
throw new \InvalidArgumentException('Factory is not describable.');
} else {
$description[] = sprintf('<comment>Factory Class</comment> %s', $factory[0]);
$tableRows[] = array('Factory Class', $factory[0]);
}
$description[] = sprintf('<comment>Factory Method</comment> %s', $factory[1]);
$tableRows[] = array('Factory Method', $factory[1]);
} else {
$description[] = sprintf('<comment>Factory Function</comment> %s', $factory);
$tableRows[] = array('Factory Function', $factory);
}
}

$this->writeText(implode("\n", $description)."\n", $options);
$options['output']->table($tableHeaders, $tableRows);
}

/**
* {@inheritdoc}
*/
protected function describeContainerAlias(Alias $alias, array $options = array())
{
$this->writeText(sprintf("This service is an alias for the service <info>%s</info>\n", (string) $alias), $options);
$options['output']->comment(sprintf("This service is an alias for the service <info>%s</info>\n", (string) $alias));
}

/**
* {@inheritdoc}
*/
protected function describeContainerParameter($parameter, array $options = array())
{
$this->writeText($this->formatParameter($parameter), $options);
$options['output']->table(
array('Parameter', 'Value'),
array(
array($options['parameter'], $this->formatParameter($parameter),
),
));
}

/**
Expand All @@ -344,16 +358,33 @@ protected function describeEventDispatcherListeners(EventDispatcherInterface $ev

$this->writeText($this->formatSection('event_dispatcher', $label)."\n", $options);

$registeredListeners = $eventDispatcher->getListeners($event, true);
$registeredListeners = $eventDispatcher->getListeners($event);

if (null !== $event) {
$this->writeText("\n");
$this->renderEventListenerTable($registeredListeners);
$table = new Table($this->getOutput());
$table->getStyle()->setCellHeaderFormat('%s');
$table->setHeaders(array('Order', 'Callable'));

foreach ($registeredListeners as $order => $listener) {
$table->addRow(array(sprintf('#%d', $order + 1), $this->formatCallable($listener)));
}

$table->render();
} else {
ksort($registeredListeners);
foreach ($registeredListeners as $eventListened => $eventListeners) {
$this->writeText(sprintf("\n<info>[Event]</info> %s\n", $eventListened), $options);
$this->renderEventListenerTable($eventListeners);

$table = new Table($this->getOutput());
$table->getStyle()->setCellHeaderFormat('%s');
$table->setHeaders(array('Order', 'Callable'));

foreach ($eventListeners as $order => $eventListener) {
$table->addRow(array(sprintf('#%d', $order + 1), $this->formatCallable($eventListener)));
}

$table->render();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -149,6 +151,11 @@ private function assertDescription($expectedDescription, $describedObject, array
{
$options['raw_output'] = true;
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);

if ('txt' === $this->getFormat()) {
$options['output'] = new SymfonyStyle(new ArrayInput(array()), $output);
}

$this->getDescriptor()->describe($output, $describedObject, $options);

if ('json' === $this->getFormat()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This service is an alias for the service <info>service_1</info>
// This service is an alias for the service service_1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This service is an alias for the service <info>service_2</info>
// This service is an alias for the service service_2
Loading