8000 debug:container --types (classes/interfaces) by weaverryan · Pull Request #22624 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

debug:container --types (classes/interfaces) #22624

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 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function configure()
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application'),
new InputOption('parameter', null, InputOption::VALUE_REQUIRED, 'Displays a specific parameter for an application'),
new InputOption('parameters', null, InputOption::VALUE_NONE, 'Displays parameters for an application'),
new InputOption('types', null, InputOption::VALUE_NONE, 'Displays types (classes/interfaces) available in the container'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
))
Expand All @@ -63,6 +64,10 @@ protected function configure()

<info>php %command.full_name% validator</info>

To see available types that can be used for autowiring, use the <info>--types</info> flag:

<info>php %command.full_name% --types</info>

By default, private services are hidden. You can display all services by
using the <info>--show-private</info> flag:

Expand Down Expand Up @@ -100,7 +105,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->validateInput($input);
$object = $this->getContainerBuilder();

if ($input->getOption('parameters')) {
if ($input->getOption('types')) {
$options = array('show_private' => true);
$options['filter'] = array($this, 'filterToServiceTypes');
} elseif ($input->getOption('parameters')) {
$parameters = array();
foreach ($object->getParameterBag()->all() as $k => $v) {
$parameters[$k] = $object->resolveEnvPlaceholders($v);
Expand Down Expand Up @@ -221,4 +229,18 @@ private function findServiceIdsContaining(ContainerBuilder $builder, $name)

return $foundServiceIds;
}

/**
* @internal
*/
public function filterToServiceTypes($serviceId)
{
// filter out things that could not be valid class names
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)) {
return false;
}

// see if the class exists (only need to trigger autoload once)
return class_exists($serviceId) || interface_exists($serviceId, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
$data = array('definitions' => array(), 'aliases' => array(), 'services' => array());

if (isset($options['filter'])) {
$serviceIds = array_filter($serviceIds, $options['filter']);
}

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
$services = array('definitions' => array(), 'aliases' => array(), 'services' => array());

if (isset($options['filter'])) {
$serviceIds = array_filter($serviceIds, $options['filter']);
}

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$ser 8000 viceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$maxTags = array();

if (isset($options['filter'])) {
$serviceIds = array_filter($serviceIds, $options['filter']);
}

foreach ($serviceIds as $key => $serviceId) {
$definition = $this->resolveServiceDefinition($builder, $serviceId);
if ($definition instanceof Definition) {
Expand Down Expand Up @@ -220,8 +224,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o

$tableHeaders = array_merge(array('Service ID'), $tagsNames, array('Class name'));
$tableRows = array();
$rawOutput = isset($options['raw_text']) && $options['raw_text'];
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
$definition = $this->resolveServiceDefinition($builder, $serviceId);
$styledServiceId = $rawOutput ? $serviceId : sprintf('<fg=cyan>%s</fg=cyan>', $serviceId);
if ($definition instanceof Definition) {
if ($showTag) {
foreach ($definition->getTag($showTag) as $key => $tag) {
Expand All @@ -236,13 +242,13 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
}
}
} else {
$tableRows[] = array($serviceId, $definition->getClass());
$tableRows[] = array($styledServiceId, $definition->getClass());
}
} elseif ($definition instanceof Alias) {
$alias = $definition;
$tableRows[] = array_merge(array($serviceId, sprintf('alias for "%s"', $alias)), $tagsCount ? array_fill(0, $tagsCount, '') : array());
$tableRows[] = array_merge(array($styledServiceId, sprintf('alias for "%s"', $alias)), $tagsCount ? array_fill(0, $tagsCount, '') : array());
} else {
$tableRows[] = array_merge(array($serviceId, get_class($definition)), $tagsCount ? array_fill(0, $tagsCount, '') : array());
$tableRows[] = array_merge(array($styledServiceId, get_class($definition)), $tagsCount ? array_fill(0, $tagsCount, '') : array());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function describeContainerService($service, array $options = array(),
*/
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
{
$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']));
$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));
}

/**
Expand Down Expand Up @@ -307,16 +307,21 @@ private function getContainerServiceDocument($service, $id, ContainerBuilder $bu
* @param string|null $tag
* @param bool $showPrivate
* @param bool $showArguments
* @param callable $filter
*
* @return \DOMDocument
*/
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false, $showArguments = false)
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false, $showArguments = false, $filter = null)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($containerXML = $dom->createElement('container'));

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

if ($filter) {
$serviceIds = array_filter($serviceIds, $filter);
}

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ abstract protected function getFormat();
private function assertDescription($expectedDescription, $describedObject, array $options = array())
{
$options['raw_output'] = true;
$options['raw_text'] = true;
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);

if ('txt' === $this->getFormat()) {
Expand Down
0