Skip to content

Commit 37054b8

Browse files
[DI][FrameworkBundle] Use container.hidden tag to hide services from debug:container
1 parent 9a99955 commit 37054b8

40 files changed

+225
-180
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ protected function configure()
4848
$this
4949
->setDefinition(array(
5050
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
51-
new InputOption('show-private', null, InputOption::VALUE_NONE, 'Used to show public *and* private services'),
51+
new InputOption('show-private', null, InputOption::VALUE_NONE, 'Used to show public *and* private services (deprecated)'),
5252
new InputOption('show-arguments', null, InputOption::VALUE_NONE, 'Used to show arguments in services'),
53+
new InputOption('show-hidden', null, InputOption::VALUE_NONE, 'Used to show hidden (internal) services'),
5354
new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Shows all services with a specific tag'),
5455
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application'),
5556
new InputOption('parameter', null, InputOption::VALUE_REQUIRED, 'Displays a specific parameter for an application'),
@@ -72,11 +73,6 @@ protected function configure()
7273
7374
<info>php %command.full_name% --types</info>
7475
75-
By default, private services are hidden. You can display all services by
76-
using the <info>--show-private</info> flag:
77-
78-
<info>php %command.full_name% --show-private</info>
79-
8076
Use the --tags option to display tagged <comment>public</comment> services grouped by tag:
8177
8278
<info>php %command.full_name% --tags</info>
@@ -103,14 +99,18 @@ protected function configure()
10399
*/
104100
protected function execute(InputInterface $input, OutputInterface $output)
105101
{
102+
if ($input->getOption('show-private')) {
103+
@trigger_error('The "--show-private" option no longer has any effect and is deprecated since Symfony 4.1.', E_USER_DEPRECATED);
104+
}
105+
106106
$io = new SymfonyStyle($input, $output);
107107
$errorIo = $io->getErrorStyle();
108108

109109
$this->validateInput($input);
110110
$object = $this->getContainerBuilder();
111111

112112
if ($input->getOption('types')) {
113-
$options = array('show_private' => true);
113+
$options = array();
114114
$options['filter'] = array($this, 'filterToServiceTypes');
115115
} elseif ($input->getOption('parameters')) {
116116
$parameters = array();
@@ -122,19 +122,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
122122
} elseif ($parameter = $input->getOption('parameter')) {
123123
$options = array('parameter' => $parameter);
124124
} elseif ($input->getOption('tags')) {
125-
$options = array('group_by' => 'tags', 'show_private' => $input->getOption('show-private'));
125+
$options = array('group_by' => 'tags');
126126
} elseif ($tag = $input->getOption('tag')) {
127-
$options = array('tag' => $tag, 'show_private' => $input->getOption('show-private'));
127+
$options = array('tag' => $tag);
128128
} elseif ($name = $input->getArgument('name')) {
129129
$name = $this->findProperServiceName($input, $errorIo, $object, $name);
130130
$options = array('id' => $name);
131131
} else {
132-
$options = array('show_private' => $input->getOption('show-private'));
132+
$options = array();
133133
}
134134

135135
$helper = new DescriptorHelper();
136136
$options['format'] = $input->getOption('format');
137137
$options['show_arguments'] = $input->getOption('show-arguments');
138+
$options['show_hidden'] = $input->getOption('show-hidden');
138139
$options['raw_text'] = $input->getOption('raw');
139140
$options['output'] = $io;
140141
$helper->describe($io, $object, $options);

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\DependencyInjection\Alias;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
1819
use Symfony\Component\DependencyInjection\Definition;
1920
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2021
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -230,27 +231,34 @@ protected function resolveServiceDefinition(ContainerBuilder $builder, $serviceI
230231
return $builder->getAlias($serviceId);
231232
}
232233

234+
if ('service_container' === $serviceId) {
235+
return (new Definition(ContainerInterface::class))->setPublic(true)->setSynthetic(true);
236+
}
237+
233238
// the service has been injected in some special way, just return the service
234239
return $builder->get($serviceId);
235240
}
236241

237242
/**
238243
* @param ContainerBuilder $builder
239-
* @param bool $showPrivate
244+
* @param bool $showHidden
240245
*
241246
* @return array
242247
*/
243-
protected function findDefinitionsByTag(ContainerBuilder $builder, $showPrivate)
248+
protected function findDefinitionsByTag(ContainerBuilder $builder, $showHidden)
244249
{
245250
$definitions = array();
246251
$tags = $builder->findTags();
247252
asort($tags);
248253

249254
foreach ($tags as $tag) {
255+
if ('container.hidden' === $tag) {
256+
continue;
257+
}
250258
foreach ($builder->findTaggedServiceIds($tag) as $serviceId => $attributes) {
251259
$definition = $this->resolveServiceDefinition($builder, $serviceId);
252260

253-
if (!$definition instanceof Definition || !$showPrivate && !$definition->isPublic()) {
261+
if (!$definition instanceof Definition || ($showHidden xor $definition->hasTag('container.hidden'))) {
254262
continue;
255263
}
256264

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ protected function describeContainerParameters(ParameterBag $parameters, array $
6363
*/
6464
protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
6565
{
66-
$showPrivate = isset($options['show_private']) && $options['show_private'];
66+
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
6767
$data = array();
6868

69-
foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
69+
foreach ($this->findDefinitionsByTag($builder, $showHidden) as $tag => $definitions) {
7070
$data[$tag] = array();
7171
foreach ($definitions as $definition) {
7272
$data[$tag][] = $this->getContainerDefinitionData($definition, true);
@@ -100,7 +100,7 @@ protected function describeContainerService($service, array $options = array(),
100100
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
101101
{
102102
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
103-
$showPrivate = isset($options['show_private']) && $options['show_private'];
103+
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
104104
$omitTags = isset($options['omit_tags']) && $options['omit_tags'];
105105
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
106106
$data = array('definitions' => array(), 'aliases' => array(), 'services' => array());
@@ -113,11 +113,11 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
113113
$service = $this->resolveServiceDefinition($builder, $serviceId);
114114

115115
if ($service instanceof Alias) {
116-
if ($showPrivate || ($service->isPublic() && !$service->isPrivate())) {
116+
if (!$showHidden) {
117117
$data['aliases'][$serviceId] = $this->getContainerAliasData($service);
118118
}
119119
} elseif ($service instanceof Definition) {
120-
if (($showPrivate || ($service->isPublic() && !$service->isPrivate()))) {
120+
if (!($showHidden xor $service->hasTag('container.hidden'))) {
121121
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, $omitTags, $showArguments);
122122
}
123123
} else {

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ protected function describeContainerParameters(ParameterBag $parameters, array $
8282
*/
8383
protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
8484
{
85-
$showPrivate = isset($options['show_private']) && $options['show_private'];
85+
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
8686
$this->write("Container tags\n==============");
8787

88-
foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
88+
foreach ($this->findDefinitionsByTag($builder, $showHidden) as $tag => $definitions) {
8989
$this->write("\n\n".$tag."\n".str_repeat('-', strlen($tag)));
9090
foreach ($definitions as $serviceId => $definition) {
9191
$this->write("\n\n");
@@ -119,9 +119,9 @@ protected function describeContainerService($service, array $options = array(),
119119
*/
120120
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
121121
{
122-
$showPrivate = isset($options['show_private']) && $options['show_private'];
122+
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
123123

124-
$title = $showPrivate ? 'Public and private services' : 'Public services';
124+
$title = $showHidden ? 'Hidden services' : 'Services';
125125
if (isset($options['tag'])) {
126126
$title .= ' with tag `'.$options['tag'].'`';
127127
}
@@ -139,11 +139,11 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
139139
$service = $this->resolveServiceDefinition($builder, $serviceId);
140140

141141
if ($service instanceof Alias) {
142-
if ($showPrivate || ($service->isPublic() && !$service->isPrivate())) {
142+
if (!$showHidden) {
143143
$services['aliases'][$serviceId] = $service;
144144
}
145145
} elseif ($service instanceof Definition) {
146-
if (($showPrivate || ($service->isPublic() && !$service->isPrivate()))) {
146+
if (!($showHidden xor $service->hasTag('container.hidden'))) {
147147
$services['definitions'][$serviceId] = $service;
148148
}
149149
} else {

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ protected function describeContainerParameters(ParameterBag $parameters, array $
122122
*/
123123
protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
124124
{
125-
$showPrivate = isset($options['show_private']) && $options['show_private'];
125+
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
126126

127-
if ($showPrivate) {
128-
$options['output']->title('Symfony Container Public and Private Tags');
127+
if ($showHidden) {
128+
$options['output']->title('Symfony Container Hidden Tags');
129129
} else {
130-
$options['output']->title('Symfony Container Public Tags');
130+
$options['output']->title('Symfony Container Tags');
131131
}
132132

133-
foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
133+
foreach ($this->findDefinitionsByTag($builder, $showHidden) as $tag => $definitions) {
134134
$options['output']->section(sprintf('"%s" tag', $tag));
135135
$options['output']->listing(array_keys($definitions));
136136
}
@@ -165,13 +165,13 @@ protected function describeContainerService($service, array $options = array(),
165165
*/
166166
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
167167
{
168-
$showPrivate = isset($options['show_private']) && $options['show_private'];
168+
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
169169
$showTag = isset($options['tag']) ? $options['tag'] : null;
170170

171-
if ($showPrivate) {
172-
$title = 'Symfony Container Public and Private Services';
171+
if ($showHidden) {
172+
$title = 'Symfony Container Hidden Services';
173173
} else {
174-
$title = 'Symfony Container Public Services';
174+
$title = 'Symfony Container Services';
175175
}
176176

177177
if ($showTag) {
@@ -190,8 +190,8 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
190190
foreach ($serviceIds as $key => $serviceId) {
191191
$definition = $this->resolveServiceDefinition($builder, $serviceId);
192192
if ($definition instanceof Definition) {
193-
// filter out private services unless shown explicitly
194-
if (!$showPrivate && (!$definition->isPublic() || $definition->isPrivate())) {
193+
// filter out hidden services unless shown explicitly
194+
if ($showHidden xor $definition->hasTag('container.hidden')) {
195195
unset($serviceIds[$key]);
196196
continue;
197197
}
@@ -209,7 +209,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
209209
}
210210
}
211211
} elseif ($definition instanceof Alias) {
212-
if (!$showPrivate && (!$definition->isPublic() || $definition->isPrivate())) {
212+
if ($showHidden) {
213213
unset($serviceIds[$key]);
214214
continue;
215215
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function describeContainerParameters(ParameterBag $parameters, array $
5858
*/
5959
protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
6060
{
61-
$this->writeDocument($this->getContainerTagsDocument($builder, isset($options['show_private']) && $options['show_private']));
61+
$this->writeDocument($this->getContainerTagsDocument($builder, isset($options['show_hidden']) && $options['show_hidden']));
6262
}
6363

6464
/**
@@ -78,7 +78,7 @@ protected function describeContainerService($service, array $options = array(),
7878
*/
7979
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
8080
{
81-
$this->writeDocument($this->getContainerServicesDocument($builder, isset($options['tag']) ? $options['tag'] : null, isset($options['show_private']) && $options['show_private'], isset($options['show_arguments']) && $options['show_arguments'], isset($options['filter']) ? $options['filter'] : null));
81+
$this->writeDocument($this->getContainerServicesDocument($builder, isset($options['tag']) ? $options['tag'] : null, isset($options['show_hidden']) && $options['show_hidden'], isset($options['show_arguments']) && $options['show_arguments'], isset($options['filter']) ? $options['filter'] : null));
8282
}
8383

8484
/**
@@ -231,12 +231,12 @@ private function getContainerParametersDocument(ParameterBag $parameters): \DOMD
231231
return $dom;
232232
}
233233

234-
private function getContainerTagsDocument(ContainerBuilder $builder, bool $showPrivate = false): \DOMDocument
234+
private function getContainerTagsDocument(ContainerBuilder $builder, bool $showHidden = false): \DOMDocument
235235
{
236236
$dom = new \DOMDocument('1.0', 'UTF-8');
237237
$dom->appendChild($containerXML = $dom->createElement('container'));
238238

239-
foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
239+
foreach ($this->findDefinitionsByTag($builder, $showHidden) as $tag => $definitions) {
240240
$containerXML->appendChild($tagXML = $dom->createElement('tag'));
241241
$tagXML->setAttribute('name', $tag);
242242

@@ -269,7 +269,7 @@ private function getContainerServiceDocument($service, string $id, ContainerBuil
269269
return $dom;
270270
}
271271

272-
private function getContainerServicesDocument(ContainerBuilder $builder, string $tag = null, bool $showPrivate = false, bool $showArguments = false, callable $filter = null): \DOMDocument
272+
private function getContainerServicesDocument(ContainerBuilder $builder, string $tag = null, bool $showHidden = false, bool $showArguments = false, callable $filter = null): \DOMDocument
273273
{
274274
$dom = new \DOMDocument('1.0', 'UTF-8');
275275
$dom->appendChild($containerXML = $dom->createElement('container'));
@@ -283,7 +283,7 @@ private function getContainerServicesDocument(ContainerBuilder $builder, string
283283
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
284284
$service = $this->resolveServiceDefinition($builder, $serviceId);
285285

286-
if (($service instanceof Definition || $service instanceof Alias) && !($showPrivate || ($service->isPublic() && !$service->isPrivate()))) {
286+
if (($service instanceof Alias && $showHidden) || ($service instanceof Definition && ($showHidden xor $service->hasTag('container.hidden')))) {
287287
continue;
288288
}
289289

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class UnusedTagsPass implements CompilerPassInterface
2525
'annotations.cached_reader',
2626
'cache.pool.clearer',
2727
'console.command',
28+
'container.hidden',
2829
'container.hot_path',
2930
'container.service_locator',
3031
'container.service_subscriber',

0 commit comments

Comments
 (0)