Skip to content

[DI] Cleanup remainings from autoregistration #26634

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
Mar 22, 2018
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 @@ -29,7 +29,6 @@ class AutowirePass extends AbstractRecursivePass
{
private $types;
private $ambiguousServiceTypes = array();
private $autowired = array();
private $lastFailure;
private $throwOnAutowiringException;

Expand All @@ -48,7 +47,6 @@ public function process(ContainerBuilder $container)
} finally {
$this->types = null;
$this->ambiguousServiceTypes = array();
$this->autowired = array();
}
}

Expand All @@ -73,7 +71,7 @@ protected function processValue($value, $isRoot = false)
private function doProcessValue($value, $isRoot = false)
{
if ($value instanceof TypedReference) {
if ($ref = $this->getAutowiredReference($value, $value->getRequiringClass() ? sprintf('for "%s" in "%s"', $value->getType(), $value->getRequiringClass()) : '')) {
if ($ref = $this->getAutowiredReference($value)) {
return $ref;
}
$this->container->log($this, $this->createTypeNotFoundMessage($value, 'it'));
Expand Down Expand Up @@ -190,7 +188,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
continue;
}

if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, !$parameter->isOptional() ? $class : ''), 'for '.sprintf('argument "$%s" of method "%s()"', $parameter->name, $class.'::'.$method))) {
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, !$parameter->isOptional() ? $class : ''))) {
$failureMessage = $this->createTypeNotFoundMessage($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));

if ($parameter->isDefaultValueAvailable()) {
Expand Down Expand Up @@ -224,30 +222,14 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
/**
* @return TypedReference|null A reference to the service matching the given type, if any
*/
private function getAutowiredReference(TypedReference $reference, $deprecationMessage)
private function getAutowiredReference(TypedReference $reference)
{
$this->lastFailure = null;
$type = $reference->getType();

if ($type !== (string) $reference || ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract())) {
return $reference;
}

if (!$reference->canBeAutoregistered()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is canBeAutoregistered still used anywhere ? AFAICT, this PR removes the only usages of the method. We should probably deprecate it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably deprecate it.

Yes, we can now in 4.1

return;
}

if (null === $this->types) {
$this->populateAvailableTypes();
}

if (isset($this->types[$type]) || isset($this->ambiguousServiceTypes[$type])) {
return;
}

if (isset($this->autowired[$type])) {
return $this->autowired[$type] ? new TypedReference($this->autowired[$type], $type) : null;
}
}

/**
Expand Down Expand Up @@ -351,6 +333,9 @@ private function createTypeAlternatives(TypedReference $reference)
if ($message = $this->getAliasesSuggestionForType($type = $reference->getType())) {
return ' '.$message;
}
if (null === $this->types) {
$this->populateAvailableTypes();
}

$servicesAndAliases = $this->container->getServiceIds();
if (!$this->container->has($type) && false !== $key = array_search(strtolower($type), array_map('strtolower', $servicesAndAliases))) {
Expand All @@ -359,8 +344,6 @@ private function createTypeAlternatives(TypedReference $reference)
$message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
} elseif (isset($this->types[$type])) {
$message = sprintf('the existing "%s" service', $this->types[$type]);
} elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered()) {
return ' It cannot be auto-registered because it is from a different root namespace.';
} else {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ public function provideNotWireableCalls()
{
return array(
array('setNotAutowireable', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class was not found.'),
array('setDifferentNamespace', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setDifferentNamespace()" references class "stdClass" but no such service exists. It cannot be auto-registered because it is from a different root namespace.'),
array('setDifferentNamespace', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setDifferentNamespace()" references class "stdClass" but no such service exists.'),
array(null, 'Invalid service "foo": method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setProtectedMethod()" must be public.'),
);
}
Expand Down