Skip to content

Commit 2a9805e

Browse files
richardmiller-zzBart van den Burg
authored andcommitted
[FrameworkBundle] Adding a option to debug services by tag
1 parent 0fc0fb3 commit 2a9805e

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

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

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ protected function configure()
4444
->setDefinition(array(
4545
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
4646
new InputOption('show-private', null, InputOption::VALUE_NONE, 'Use to show public *and* private services'),
47+
new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Show all services with a specific tag'),
48+
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application')
4749
))
4850
->setDescription('Displays current services for an application')
4951
->setHelp(<<<EOF
@@ -59,6 +61,14 @@ protected function configure()
5961
using the --show-private flag:
6062
6163
<info>php %command.full_name% --show-private</info>
64+
65+
Use the --tags option to display tagged <comment>public</comment> services grouped by tag:
66+
67+
<info>php %command.full_name% --tags</info>
68+
69+
Find all services with a specific tag by specifying the tag name with the --tag option:
70+
71+
<info>php %command.full_name% --tag=form.type</info>
6272
EOF
6373
)
6474
;
@@ -72,26 +82,40 @@ protected function execute(InputInterface $input, OutputInterface $output)
7282
$name = $input->getArgument('name');
7383

7484
$this->containerBuilder = $this->getContainerBuilder();
75-
$serviceIds = $this->containerBuilder->getServiceIds();
85+
86+
if ($input->getOption('tags')) {
87+
$this->outputTags($output, $input->getOption('show-private'));
88+
return;
89+
}
90+
91+
$tag = $input->getOption('tag');
92+
if (null !== $tag) {
93+
$serviceIds = array_keys($this->containerBuilder->findTaggedServiceIds($tag));
94+
} else {
95+
$serviceIds = $this->containerBuilder->getServiceIds();
96+
}
7697

7798
// sort so that it reads like an index of services
7899
asort($serviceIds);
79100

80101
if ($name) {
81102
$this->outputService($output, $name);
82103
} else {
83-
$this->outputServices($output, $serviceIds, $input->getOption('show-private'));
104+
$this->outputServices($output, $serviceIds, $input->getOption('show-private'), $tag);
84105
}
85106
}
86107

87-
protected function outputServices(OutputInterface $output, $serviceIds, $showPrivate = false)
108+
protected function outputServices(OutputInterface $output, $serviceIds, $showPrivate = false, $showTagAttributes = null)
88109
{
89110
// set the label to specify public or public+private
90111
if ($showPrivate) {
91112
$label = '<comment>Public</comment> and <comment>private</comment> services';
92113
} else {
93114
$label = '<comment>Public</comment> services';
94115
}
116+
if ($showTagAttributes) {
117+
$label .= ' with tag <info>'.$showTagAttributes.'</info>';
118+
}
95119

96120
$output->writeln($this->getHelper('formatter')->formatSection('container', $label));
97121

@@ -136,6 +160,23 @@ protected function outputServices(OutputInterface $output, $serviceIds, $showPri
136160
$service = $definition;
137161
$output->writeln(sprintf($format, $serviceId, '', get_class($service)));
138162
}
163+
164+
if (null !== $showTagAttributes) {
165+
$tags = $definition->getTag($showTagAttributes);
166+
foreach($tags as $tag) {
167+
$output->write(' <comment>'.$showTagAttributes.' tag attributes</comment>: ');
168+
if (count($tag)) {
169+
$arguments = array();
170+
foreach($tag as $key => $value) {
171+
$arguments[] = $key;
172+
$arguments[] = $value;
173+
}
174+
$output->writeln(vsprintf(implode(", ", array_fill(0, count($tag), '<info>%s</info>: %s')), $arguments));
175+
} else {
176+
$output->writeln('none');
177+
}
178+
}
179+
}
139180
}
140181
}
141182

@@ -223,4 +264,45 @@ protected function resolveServiceDefinition($serviceId)
223264
// the service has been injected in some special way, just return the service
224265
return $this->containerBuilder->get($serviceId);
225266
}
267+
268+
/**
269+
* Renders list of tagged services grouped by tag
270+
*
271+
* @param OutputInterface $output
272+
* @param bool $showPrivate
273+
*/
274+
protected function outputTags(OutputInterface $output, $showPrivate = false)
275+
{
276+
$tags = $this->containerBuilder->findTags();
277+
asort($tags);
278+
279+
$label = 'Tagged services';
280+
$output->writeln($this->getHelper('formatter')->formatSection('container', $label));
281+
282+
foreach ($tags as $tag) {
283+
$serviceIds = $this->containerBuilder->findTaggedServiceIds($tag);
284+
285+
foreach ($serviceIds as $serviceId => $attributes) {
286+
$definition = $this->resolveServiceDefinition($serviceId);
287+
if ($definition instanceof Definition) {
288+
if (!$showPrivate && !$definition->isPublic()) {
289+
unset($serviceIds[$serviceId]);
290+
continue;
291+
}
292+
}
293+
}
294+
295+
if (count($serviceIds) === 0) {
296+
continue;
297+
}
298+
299+
$output->writeln($this->getHelper('formatter')->formatSection('tag', $tag));
300+
301+
foreach ($serviceIds as $serviceId => $attributes) {
302+
$output->writeln($serviceId);
303+
}
304+
305+
$output->writeln('');
306+
}
307+
}
226308
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,21 @@ public function findTaggedServiceIds($name)
857857
return $tags;
858858
}
859859

860+
/**
861+
* Returns all tags the defined services use.
862+
*
863+
* @return array An array of tags
864+
*/
865+
public function findTags()
866+
{
867+
$tags = array();
868+
foreach ($this->getDefinitions() as $id => $definition) {
869+
$tags = array_merge(array_keys($definition->getTags()), $tags);
870+
}
871+
872+
return array_unique($tags);
873+
}
874+
860875
/**
861876
* Returns the Service Conditionals.
862877
*

0 commit comments

Comments
 (0)