Skip to content

Commit 2c0bc23

Browse files
Merge branch '3.0'
* 3.0: use stable 1.0.x Composer versions Fix typo in VarDumper README [DI] Fix AutowirePass fatal error with classes that have non-existing parents [DependencyInjection] Tests for AutowirePass with missing parent class [WebProfilerBunde] Give an absolute url in case the request occured from another domain Conflicts: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
2 parents b4228df + 4973244 commit 2c0bc23

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

Compiler/AutowirePass.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,32 @@ class AutowirePass implements CompilerPassInterface
3535
*/
3636
public function process(ContainerBuilder $container)
3737
{
38-
$this->container = $container;
39-
foreach ($container->getDefinitions() as $id => $definition) {
40-
if ($definition->isAutowired()) {
41-
$this->completeDefinition($id, $definition);
38+
$throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
39+
spl_autoload_register($throwingAutoloader);
40+
41+
try {
42+
$this->container = $container;
43+
foreach ($container->getDefinitions() as $id => $definition) {
44+
if ($definition->isAutowired()) {
45+
$this->completeDefinition($id, $definition);
46+
}
4247
}
48+
} catch (\Error $e) {
49+
} catch (\Exception $e) {
4350
}
4451

52+
spl_autoload_unregister($throwingAutoloader);
53+
4554
// Free memory and remove circular reference to container
4655
$this->container = null;
4756
$this->reflectionClasses = array();
4857
$this->definedTypes = array();
4958
$this->types = null;
5059
$this->ambiguousServiceTypes = array();
60+
61+
if (isset($e)) {
62+
throw $e;
63+
}
5164
}
5265

5366
/**
@@ -139,11 +152,11 @@ private function completeDefinition($id, Definition $definition)
139152
}
140153
}
141154
}
142-
} catch (\ReflectionException $reflectionException) {
155+
} catch (\ReflectionException $e) {
143156
// Typehint against a non-existing class
144157

145158
if (!$parameter->isDefaultValueAvailable()) {
146-
throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $reflectionException->getMessage()), 0, $reflectionException);
159+
throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e);
147160
}
148161

149162
$value = $parameter->getDefaultValue();
@@ -295,7 +308,7 @@ private function getReflectionClass($id, Definition $definition)
295308

296309
try {
297310
$reflector = new \ReflectionClass($class);
298-
} catch (\ReflectionException $reflectionException) {
311+
} catch (\ReflectionException $e) {
299312
$reflector = false;
300313
}
301314

Tests/Compiler/AutowirePassTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ public function testClassNotFoundThrowsException()
284284
$pass->process($container);
285285
}
286286

287+
/**
288+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
289+
* @expectedExceptionMessage Cannot autowire argument 2 for Symfony\Component\DependencyInjection\Tests\Compiler\BadParentTypeHintedArgument because the type-hinted class does not exist (Class Symfony\Component\DependencyInjection\Tests\Compiler\OptionalServiceClass does not exist).
290+
*/
291+
public function testParentClassNotFoundThrowsException()
292+
{
293+
$container = new ContainerBuilder();
294+
295+
$aDefinition = $container->register('a', __NAMESPACE__.'\BadParentTypeHintedArgument');
296+
$aDefinition->setAutowired(true);
297+
298+
$pass = new AutowirePass();
299+
$pass->process($container);
300+
}
301+
287302
public function testDontUseAbstractServices()
288303
{
289304
$container = new ContainerBuilder();
@@ -446,6 +461,21 @@ public function getCreateResourceTests()
446461
['ClassChangedConstructorArgs', false],
447462
);
448463
}
464+
465+
public function testIgnoreServiceWithClassNotExisting()
466+
{
467+
$container = new ContainerBuilder();
468+
469+
$container->register('class_not_exist', __NAMESPACE__.'\OptionalServiceClass');
470+
471+
$barDefinition = $container->register('bar', __NAMESPACE__.'\Bar');
472+
$barDefinition->setAutowired(true);
473+
474+
$pass = new AutowirePass();
475+
$pass->process($container);
476+
477+
$this->assertTrue($container->hasDefinition('bar'));
478+
}
449479
}
450480

451481
class Foo
@@ -558,6 +588,12 @@ public function __construct(Dunglas $k, NotARealClass $r)
558588
{
559589
}
560590
}
591+
class BadParentTypeHintedArgument
592+
{
593+
public function __construct(Dunglas $k, OptionalServiceClass $r)
594+
{
595+
}
596+
}
561597
class NotGuessableArgument
562598
{
563599
public function __construct(Foo $k)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
13+
14+
use Symfony\Bug\NotExistClass;
15+
16+
class OptionalServiceClass extends NotExistClass
17+
{
18+
}

0 commit comments

Comments
 (0)