Skip to content

Commit 1bb7055

Browse files
committed
feature #21419 [DI][Config] Add & use ReflectionClassResource (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI][Config] Add & use ReflectionClassResource | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #21079 | License | MIT | Doc PR | - With new changes comming to 3.3, we need a more generic reflection tracking logic than the one already managed by the autowiring subsystem. This PR adds a new ReflectionClassResource in the Config component, and a new ContainerBuilder::getReflectionClass() method in the DI one (for fetching+tracking reflection-related info). ReflectionClassResource tracks changes to any public or protected properties/method. PR updated and ready, best viewed [ignoring whitespaces](https://github.com/symfony/symfony/pull/21419/files?w=1). changelog: * added `ReflectionClassResource` class * added second `$exists` constructor argument to `ClassExistenceResource` - with a special mode that prevents fatal errors from happening when some parent class is broken (logic generalized from AutowiringPass) * made `ClassExistenceResource` also work with interfaces and traits * added `ContainerBuilder::getReflectionClass()` for retrieving and tracking reflection class info * deprecated `ContainerBuilder::getClassResource()`, use `ContainerBuilder::getReflectionClass()` or `ContainerBuilder::addObjectResource()` instead Commits ------- 37e44939ef [DI][Config] Add & use ReflectionClassResource
2 parents 4e6379c + b3367d9 commit 1bb7055

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

DependencyInjection/Compiler/LoggingTranslatorPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\Translation\TranslatorInterface;
19+
use Symfony\Component\Translation\TranslatorBagInterface;
1720

1821
/**
1922
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
@@ -31,7 +34,10 @@ public function process(ContainerBuilder $container)
3134
$definition = $container->getDefinition((string) $translatorAlias);
3235
$class = $container->getParameterBag()->resolveValue($definition->getClass());
3336

34-
if (is_subclass_of($class, 'Symfony\Component\Translation\TranslatorInterface') && is_subclass_of($class, 'Symfony\Component\Translation\TranslatorBagInterface')) {
37+
if (!$r = $container->getReflectionClass($class)) {
38+
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $translatorAlias));
39+
}
40+
if ($r->isSubclassOf(TranslatorInterface::class) && $r->isSubclassOf(TranslatorBagInterface::class)) {
3541
$container->getDefinition('translator.logging')->setDecoratedService('translator');
3642
$container->getDefinition('translation.warmer')->replaceArgument(0, new Reference('translator.logging.inner'));
3743
}

Tests/DependencyInjection/Compiler/LoggingTranslatorPassTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function testProcess()
5454
->method('getParameterBag')
5555
->will($this->returnValue($parameterBag));
5656

57+
$container->expects($this->once())
58+
->method('getReflectionClass')
59+
->with('Symfony\Bundle\FrameworkBundle\Translation\Translator')
60+
->will($this->returnValue(new \ReflectionClass('Symfony\Bundle\FrameworkBundle\Translation\Translator')));
61+
5762
$pass = new LoggingTranslatorPass();
5863
$pass->process($container);
5964
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"symfony/dependency-injection": "~3.3",
2323
"symfony/config": "~3.3",
2424
"symfony/event-dispatcher": "~3.3",
25-
"symfony/http-foundation": "~3.1",
25+
"symfony/http-foundation": "~3.3",
2626
"symfony/http-kernel": "~3.3",
2727
"symfony/polyfill-mbstring": "~1.0",
2828
"symfony/filesystem": "~2.8|~3.0",

0 commit comments

Comments
 (0)