Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bug #26634 [DI] Cleanup remainings from autoregistration (nicolas-gre…
…kas)

This PR was merged into the 4.0 branch.

Discussion
----------

[DI] Cleanup remainings from autoregistration

| Q             | A
| ------------- | ---
| Branch?       | 4.0
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25959
| License       | MIT
| Doc PR        | -

Commits
-------

74afff8 [DI] Cleanup remainings from autoregistration
  • Loading branch information
nicolas-grekas committed Mar 22, 2018
2 parents 8d7131a + 74afff8 commit 07512bb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
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()) {
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
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

0 comments on commit 07512bb

Please sign in to comment.