Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 109a91c

Browse filesBrowse files
committed
Fixing problem where the class may not exist or it may exist, but its
parent class may not exist.
1 parent 7d5f9bc commit 109a91c
Copy full SHA for 109a91c

File tree

2 files changed

+16
-5
lines changed
Filter options

2 files changed

+16
-5
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

14+
use Symfony\Component\Config\Resource\ClassExistenceResource;
1415
use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Definition;
@@ -484,8 +485,18 @@ private function createAutowiredDefinition($type)
484485

485486
private function createTypeNotFoundMessage(TypedReference $reference, $label)
486487
{
487-
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), false)) {
488-
$message = sprintf('is type-hinted with "%s" but this class was not found.', $type);
488+
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), true)) {
489+
// either $type does not exist or a parent class does not exist
490+
try {
491+
$resource = new ClassExistenceResource($type, false);
492+
// isFresh() will explode ONLY if a parent class/trait does not exist
493+
$resource->isFresh(0);
494+
$classExists = false;
495+
} catch (\ReflectionException $e) {
496+
$classExists = true;
497+
}
498+
499+
$message = sprintf('has type "%s" but this class %s.', $type, $classExists ? 'cannot be loaded: its parent class may be missing' : 'was not found');
489500
} else {
490501
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
491502
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference));

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public function testDontTriggerAutowiring()
359359

360360
/**
361361
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
362-
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadTypeHintedArgument::__construct()" is type-hinted with "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class was not found.
362+
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadTypeHintedArgument::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class was not found.
363363
*/
364364
public function testClassNotFoundThrowsException()
365365
{
@@ -374,7 +374,7 @@ public function testClassNotFoundThrowsException()
374374

375375
/**
376376
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
377-
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadParentTypeHintedArgument::__construct()" is type-hinted with "Symfony\Component\DependencyInjection\Tests\Compiler\OptionalServiceClass" but this class was not found.
377+
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadParentTypeHintedArgument::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\OptionalServiceClass" but this class cannot be loaded: its parent class may be missing.
378378
*/
379379
public function testParentClassNotFoundThrowsException()
380380
{
@@ -761,7 +761,7 @@ public function testNotWireableCalls($method, $expectedMsg)
761761
public function provideNotWireableCalls()
762762
{
763763
return array(
764-
array('setNotAutowireable', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable()" is type-hinted with "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class was not found.'),
764+
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.'),
765765
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.'),
766766
array(null, 'Cannot autowire service "foo": method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setProtectedMethod()" must be public.'),
767767
);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.