8000 Consistently use var $container to reference the container builder+configurator by nicolas-grekas · Pull Request #50282 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Consistently use var $container to reference the container builder+configurator #50282

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

Merged
merged 1 commit into from
May 10, 2023
Merged
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 @@ -26,7 +26,7 @@
*/
trait BuildDebugContainerTrait
{
protected $containerBuilder;
protected ContainerBuilder $container;

/**
* Loads the ContainerBuilder from the cache.
Expand All @@ -35,8 +35,8 @@ trait BuildDebugContainerTrait
*/
protected function getContainerBuilder(KernelInterface $kernel): ContainerBuilder
{
if ($this->containerBuilder) {
return $this->containerBuilder;
if (isset($this->container)) {
return $this->container;
}

if (!$kernel->isDebug() || !$kernel->getContainer()->getParameter('debug.container.dump') || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
Expand All @@ -59,6 +59,6 @@ protected function getContainerBuilder(KernelInterface $kernel): ContainerBuilde
$container->getCompilerPassConfig()->setBeforeRemovingPasses([]);
}

return $this->containerBuilder = $container;
return $this->container = $container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@ protected function validateInput(InputInterface $input): void
}
}

private function findProperServiceName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $builder, string $name, bool $showHidden): string
private function findProperServiceName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $container, string $name, bool $showHidden): string
{
$name = ltrim($name, '\\');

if ($builder->has($name) || !$input->isInteractive()) {
if ($container->has($name) || !$input->isInteractive()) {
return $name;
}

$matchingServices = $this->findServiceIdsContaining($builder, $name, $showHidden);
$matchingServices = $this->findServiceIdsContaining($container, $name, $showHidden);
if (!$matchingServices) {
throw new InvalidArgumentException(sprintf('No services found that match "%s".', $name));
}
Expand All @@ -281,13 +281,13 @@ private function findProperServiceName(InputInterface $input, SymfonyStyle $io,
return $io->choice('Select one of the following services to display its information', $matchingServices);
}

private function findProperTagName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $builder, string $tagName): string
private function findProperTagName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $container, string $tagName): string
{
if (\in_array($tagName, $builder->findTags(), true) || !$input->isInteractive()) {
if (\in_array($tagName, $container->findTags(), true) || !$input->isInteractive()) {
return $tagName;
}

$matchingTags = $this->findTagsContaining($builder, $tagName);
$matchingTags = $this->findTagsContaining($container, $tagName);
if (!$matchingTags) {
throw new InvalidArgumentException(sprintf('No tags found that match "%s".', $tagName));
}
Expand All @@ -299,15 +299,15 @@ private function findProperTagName(InputInterface $input, SymfonyStyle $io, Cont
return $io->choice('Select one of the following tags to display its information', $matchingTags);
}

private function findServiceIdsContaining(ContainerBuilder $builder, string $name, bool $showHidden): array
private function findServiceIdsContaining(ContainerBuilder $container, string $name, bool $showHidden): array
{
$serviceIds = $builder->getServiceIds();
$serviceIds = $container->getServiceIds();
$foundServiceIds = $foundServiceIdsIgnoringBackslashes = [];
foreach ($serviceIds as $serviceId) {
if (!$showHidden && str_starts_with($serviceId, '.')) {
continue;
}
if (!$showHidden && $builder->hasDefinition($serviceId) && $builder->getDefinition($serviceId)->hasTag('container.excluded')) {
if (!$showHidden && $container->hasDefinition($serviceId) && $container->getDefinition($serviceId)->hasTag('container.excluded')) {
continue;
}
if (false !== stripos(str_replace('\\', '', $serviceId), $name)) {
Expand All @@ -321,9 +321,9 @@ private function findServiceIdsContaining(ContainerBuilder $builder, string $nam
return $foundServiceIds ?: $foundServiceIdsIgnoringBackslashes;
}

private function findTagsContaining(ContainerBuilder $builder, string $tagName): array
private function findTagsContaining(ContainerBuilder $container, string $tagName): array
{
$tags = $builder->findTags();
$tags = $container->findTags();
$foundTags = [];
foreach ($tags as $tag) {
if (str_contains($tag, $tagName)) {
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#[AsCommand(name: 'lint:container', description: 'Ensure that arguments injected into services match type declarations')]
final class ContainerLintCommand extends Command
{
private ContainerBuilder $containerBuilder;
private ContainerBuilder $container;

protected function configure(): void
{
Expand Down Expand Up @@ -70,8 +70,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function getContainerBuilder(): ContainerBuilder
{
if (isset($this->containerBuilder)) {
return $this->containerBuilder;
if (isset($this->container)) {
return $this->container;
}

$kernel = $this->getApplication()->getKernel();
Expand Down Expand Up @@ -108,6 +108,6 @@ private function getContainerBuilder(): ContainerBuilder

$container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);

return $this->containerBuilder = $container;
return $this->container = $container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);
$errorIo = $io->getErrorStyle();

$builder = $this->getContainerBuilder($this->getApplication()->getKernel());
$serviceIds = $builder->getServiceIds();
$container = $this->getContainerBuilder($this->getApplication()->getKernel());
$serviceIds = $container->getServiceIds();
$serviceIds = array_filter($serviceIds, $this->filterToServiceTypes(...));

if ($search = $input->getArgument('search')) {
Expand All @@ -98,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$previousId = '-';
$serviceIdsNb = 0;
foreach ($serviceIds as $serviceId) {
if ($builder->hasDefinition($serviceId) && $builder->getDefinition($serviceId)->hasTag('container.excluded')) {
if ($container->hasDefinition($serviceId) && $container->getDefinition($serviceId)->hasTag('container.excluded')) {
continue;
}
$text = [];
Expand All @@ -119,11 +119,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$serviceLine = sprintf('<fg=yellow;href=%s>%s</>', $fileLink, $serviceId);
}

if ($builder->hasAlias($serviceId)) {
if ($container->hasAlias($serviceId)) {
$hasAlias[$serviceId] = true;
$serviceAlias = $builder->getAlias($serviceId);
$serviceAlias = $container->getAlias($serviceId);

if ($builder->hasDefinition($serviceAlias) && $decorated = $builder->getDefinition($serviceAlias)->getTag('container.decorator')) {
if ($container->hasDefinition($serviceAlias) && $decorated = $container->getDefinition($serviceAlias)->getTag('container.decorator')) {
$serviceLine .= ' <fg=cyan>('.$decorated[0]['id'].')</>';
} else {
$serviceLine .= ' <fg=cyan>('.$serviceAlias.')</>';
Expand All @@ -135,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} elseif (!$all) {
++$serviceIdsNb;
continue;
} elseif ($builder->getDefinition($serviceId)->isDeprecated()) {
} elseif ($container->getDefinition($serviceId)->isDeprecated()) {
$serviceLine .= ' - <fg=magenta>deprecated</>';
}
$text[] = $serviceLine;
Expand Down Expand Up @@ -169,9 +169,9 @@ private function getFileLink(string $class): string
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('search')) {
$builder = $this->getContainerBuilder($this->getApplication()->getKernel());
$container = $this->getContainerBuilder($this->getApplication()->getKernel());

$suggestions->suggestValues(array_filter($builder->getServiceIds(), $this->filterToServiceTypes(...)));
$suggestions->suggestValues(array_filter($container->getServiceIds(), $this->filterToServiceTypes(...)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ abstract protected function describeRoute(Route $route, array $options = []): vo

abstract protected function describeContainerParameters(ParameterBag $parameters, array $options = []): void;

abstract protected function describeContainerTags(ContainerBuilder $builder, array $options = []): void;
abstract protected function describeContainerTags(ContainerBuilder $container, array $options = []): void;

/**
* Describes a container service by its name.
Expand All @@ -94,21 +94,21 @@ abstract protected function describeContainerTags(ContainerBuilder $builder, arr
*
* @param Definition|Alias|object $service
*/
abstract protected function describeContainerService(object $service, array $options = [], ContainerBuilder $builder = null): void;
abstract protected function describeContainerService(object $service, array $options = [], ContainerBuilder $container = null): void;

/**
* Describes container services.
*
* Common options are:
* * tag: filters described services by given tag
*/
abstract protected function describeContainerServices(ContainerBuilder $builder, array $options = []): void;
abstract protected function describeContainerServices(ContainerBuilder $container, array $options = []): void;

abstract protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void;
abstract protected function describeContainerDeprecations(ContainerBuilder $container, array $options = []): void;

abstract protected function describeContainerDefinition(Definition $definition, array $options = [], ContainerBuilder $builder = null): void;
abstract protected function describeContainerDefinition(Definition $definition, array $options = [], ContainerBuilder $container = null): void;

abstract protected function describeContainerAlias(Alias $alias, array $options = [], ContainerBuilder $builder = null): void;
abstract protected function describeContainerAlias(Alias $alias, array $options = [], ContainerBuilder $container = null): void;

abstract protected function describeContainerParameter(mixed $parameter, array $options = []): void;

Expand Down Expand Up @@ -170,34 +170,34 @@ protected function formatParameter(mixed $value): string
return (string) $value;
}

protected function resolveServiceDefinition(ContainerBuilder $builder, string $serviceId): mixed
protected function resolveServiceDefinition(ContainerBuilder $container, string $serviceId): mixed
{
if ($builder->hasDefinition($serviceId)) {
return $builder->getDefinition($serviceId);
if ($container->hasDefinition($serviceId)) {
return $container->getDefinition($serviceId);
}

// Some service IDs don't have a Definition, they're aliases
if ($builder->hasAlias($serviceId)) {
return $builder->getAlias($serviceId);
if ($container->hasAlias($serviceId)) {
return $container->getAlias($serviceId);
}

if ('service_container' === $serviceId) {
return (new Definition(ContainerInterface::class))->setPublic(true)->setSynthetic(true);
}

// the service has been injected in some special way, just return the service
return $builder->get($serviceId);
return $container->get($serviceId);
}

protected function findDefinitionsByTag(ContainerBuilder $builder, bool $showHidden): array
protected function findDefinitionsByTag(ContainerBuilder $container, bool $showHidden): array
{
$definitions = [];
$tags = $builder->findTags();
$tags = $container->findTags();
asort($tags);

foreach ($tags as $tag) {
foreach ($builder->findTaggedServiceIds($tag) as $serviceId => $attributes) {
$definition = $this->resolveServiceDefinition($builder, $serviceId);
foreach ($container->findTaggedServiceIds($tag) as $serviceId => $attributes) {
$definition = $this->resolveServiceDefinition($container, $serviceId);

if ($showHidden xor '.' === ($serviceId[0] ?? null)) {
continue;
Expand Down Expand Up @@ -334,12 +334,12 @@ private function getContainerEnvVars(ContainerBuilder $container): array
return array_values($envs);
}

protected function getServiceEdges(ContainerBuilder $builder, string $serviceId): array
protected function getServiceEdges(ContainerBuilder $container, string $serviceId): array
{
try {
return array_values(array_unique(array_map(
fn (ServiceReferenceGraphEdge $edge) => $edge->getSourceNode()->getId(),
$builder->getCompiler()->getServiceReferenceGraph()->getNode($serviceId)->getInEdges()
$container->getCompiler()->getServiceReferenceGraph()->getNode($serviceId)->getInEdges()
)));
} catch (InvalidArgumentException $exception) {
return [];
Expand Down
Loading
0