8000 Adding a new debug:container --types option to dump classes/interface… · symfony/symfony@7242c86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7242c86

Browse files
committed
Adding a new debug:container --types option to dump classes/interfaces that can be used for type-hinting
1 parent 243e416 commit 7242c86

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function configure()
5050
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application'),
5151
new InputOption('parameter', null, InputOption::VALUE_REQUIRED, 'Displays a specific parameter for an application'),
5252
new InputOption('parameters', null, InputOption::VALUE_NONE, 'Displays parameters for an application'),
53+
new InputOption('types', null, InputOption::VALUE_NONE, 'Displays types (classes/interfaces) available in the container'),
5354
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
5455
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
5556
))
@@ -63,6 +64,10 @@ protected function configure()
6364
6465
<info>php %command.full_name% validator</info>
6566
67+
To see available types that can be used for autowiring, use the <info>--types</info> flag:
68+
69+
<info>php %command.full_name% --types</info>
70+
6671
By default, private services are hidden. You can display all services by
6772
using the <info>--show-private</info> flag:
6873
@@ -100,7 +105,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
100105
$this->validateInput($input);
101106
$object = $this->getContainerBuilder();
102107

103-
if ($input->getOption('parameters')) {
108+
if ($input->getOption('types')) {
109+
$options = array('show_private' => true);
110+
$options['filter'] = array($this, 'filterToServiceTypes');
111+
} elseif ($input->getOption('parameters')) {
104112
$parameters = array();
105113
foreach ($object->getParameterBag()->all() as $k => $v) {
106114
$parameters[$k] = $object->resolveEnvPlaceholders($v);
@@ -221,4 +229,18 @@ private function findServiceIdsContaining(ContainerBuilder $builder, $name)
221229

222230
return $foundServiceIds;
223231
}
232+
233+
/**
234+
* @internal
235+
*/
236+
public function filterToServiceTypes($serviceId)
237+
{
238+
// filter out things that could not be valid class names
239+
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $serviceId)) {
240+
return false;
241+
}
242+
243+
// see if the class exists (only need to trigger autoload once)
244+
return class_exists($serviceId) || interface_exists($serviceId, false);
245+
}
224246
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
105105
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
106106
$data = array('definitions' => array(), 'aliases' => array(), 'services' => array());
107107

108+
if (isset($options['filter'])) {
109+
$serviceIds = array_filter($serviceIds, $options['filter']);
110+
}
111+
108112
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
109113
$service = $this->resolveServiceDefinition($builder, $serviceId);
110114

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
131131
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
132132
$services = array('definitions' => array(), 'aliases' => array(), 'services' => array());
133133

134+
if (isset($options['filter'])) {
135+
$serviceIds = array_filter($serviceIds, $options['filter']);
136+
}
137+
134138
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
135139
$service = $this->resolveServiceDefinition($builder, $serviceId);
136140

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
186186
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
187187
$maxTags = array();
188188

189+
if (isset($options['filter'])) {
190+
$serviceIds = array_filter($serviceIds, $options['filter']);
191+
}
192+
189193
foreach ($serviceIds as $key => $serviceId) {
190194
$definition = $this->resolveServiceDefinition($builder, $serviceId);
191195
if ($definition instanceof Definition) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function describeContainerService($service, array $options = array(),
7979
*/
8080
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
8181
{
82-
$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']));
82+
$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));
8383
}
8484

8585
/**
@@ -307,16 +307,21 @@ private function getContainerServiceDocument($service, $id, ContainerBuilder $bu
307307
* @param string|null $tag
308308
* @param bool $showPrivate
309309
* @param bool $showArguments
310+
* @param callable $filter
310311
*
311312
* @return \DOMDocument
312313
*/
313-
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false, $showArguments = false)
314+
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false, $showArguments = false, $filter = null)
314315
{
315316
$dom = new \DOMDocument('1.0', 'UTF-8');
316317
$dom->appendChild($containerXML = $dom->createElement('container'));
317318

318319
$serviceIds = $tag ? array_keys($builder->findTaggedServiceIds($tag)) : $builder->getServiceIds();
319320

321+
if ($filter) {
322+
$serviceIds = array_filter($serviceIds, $filter);
323+
}
324+
320325
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
321326
$service = $this->resolveServiceDefinition($builder, $serviceId);
322327

0 commit comments

Comments
 (0)
0