8000 [DI] Prevent AutowirePass from triggering irrelevant deprecations · symfony/symfony@0dbb1eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 0dbb1eb

Browse files
committed
[DI] Prevent AutowirePass from triggering irrelevant deprecations
1 parent 200b45e commit 0dbb1eb

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ private function getReflectionClass($id, Definition $definition)
301301
return false;
302302
}
303303

304+
if ($deprecated = $definition->isDeprecated()) {
305+
$prevErrorHandler = set_error_handler(function ($level, $message, $file, $line, $context) use (&$prevErrorHandler) {
306+
if (E_USER_DEPRECATED === $level) {
307+
return false;
308+
}
309+
310+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line, $context) : false;
311+
});
312+
}
313+
304314
$class = $this->container->getParameterBag()->resolveValue($class);
305315

306316
try {
@@ -309,6 +319,10 @@ private function getReflectionClass($id, Definition $definition)
309319
$reflector = false;
310320
}
311321

322+
if ($deprecated) {
323+
restore_error_handler();
324+
}
325+
312326
return $this->reflectionClasses[$id] = $reflector;
313327
}
314328
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\Tests\Fixtures\DeprecatedClass;
1819

1920
/**
2021
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -442,6 +443,17 @@ public function testIgnoreServiceWithClassNotExisting()
442443
$this->assertTrue($container->hasDefinition('bar'));
443444
}
444445

446+
public function testIgnoreDeprecatedServices()
447+
{
448+
$container = new ContainerBuilder();
449+
$container->register('deprecated', DeprecatedClass::class)->setDeprecated(true);
450+
$container->register('foo', __NAMESPACE__.'\Foo');
451+
$container->register('bar', __NAMESPACE__.'\Bar')->setAutowired(true);
452+
453+
$pass = new AutowirePass();
454+
$pass->process($container);
455+
}
456+
445457
public function testEmptyStringIsKept()
446458
{
447459
$container = new ContainerBuilder();
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\Fixtures;
13+
14+
@trigger_error('deprecated', E_USER_DEPRECATED);
15+
16+
class DeprecatedClass
17+
{
18+
}

0 commit comments

Comments
 (0)
0