8000 bug #26088 [FrameworkBundle] Fix using annotation_reader in compiler … · symfony/symfony@0aedb03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0aedb03

Browse files
bug #26088 [FrameworkBundle] Fix using annotation_reader in compiler pass to inject configured cache provider (Laizerox)
This PR was merged into the 3.4 branch. Discussion ---------- [FrameworkBundle] Fix using annotation_reader in compiler pass to inject configured cache provider | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26086 | License | MIT | Doc PR | - The compilation pass of AddAnnotationsCachedReaderPass relies on already removed definition `annotations.cached_reader` due to an alias to `annotation_reader`. Since the definition is being removed because of alias, configured annotation cache provider is not injected and will default back to ArrayCache. This PR replaces the use of `annotations.cached_reader` to `annotation_reader` to complete the injection of configured cache provider. Commits ------- dfd93da bug #26086 [FrameworkBundle] Fix using annotation_reader in compiler pass to inject configured cache provider
2 parents b3c7ba7 + dfd93da commit 0aedb03

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddAnnotationsCachedReaderPass.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ public function process(ContainerBuilder $container)
2626
{
2727
// "annotations.cached_reader" is wired late so that any passes using
2828
// "annotation_reader" at build time don't get any cache
29-
if ($container->hasDefinition('annotations.cached_reader')) {
30-
$reader = $container->getDefinition('annotations.cached_reader');
29+
foreach ($container->findTaggedServiceIds('annotations.cached_reader') as $id => $tags) {
30+
$reader = $container->getDefinition($id);
3131
$properties = $reader->getProperties();
3232

3333
if (isset($properties['cacheProviderBackup'])) {
3434
$provider = $properties['cacheProviderBackup']->getValues()[0];
3535
unset($properties['cacheProviderBackup']);
3636
$reader->setProperties($properties);
37-
$container->set('annotations.cached_reader', null);
38-
$container->setDefinition('annotations.cached_reader', $reader->replaceArgument(1, $provider));
37+
$container->set($id, null);
38+
$container->setDefinition($id, $reader->replaceArgument(1, $provider));
3939
}
4040
}
4141
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class UnusedTagsPass implements CompilerPassInterface
2323
{
2424
private $whitelist = array(
25+
'annotations.cached_reader',
2526
'cache.pool.clearer',
2627
'console.command',
2728
'container.hot_path',

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,9 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
13951395
->replaceArgument(2, $config['debug'])
13961396
// temporary property to lazy-reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
13971397
->setProperty('cacheProviderBackup', new ServiceClosureArgument(new Reference($cacheService)))
1398+
->addTag('annotations.cached_reader')
13981399
;
1400+
13991401
$container->setAlias('annotation_reader', 'annotations.cached_reader')->setPrivate(true);
14001402
$container->setAlias(Reader::class, new Alias('annotations.cached_reader', false));
14011403
} else {

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Cache\Adapter\ProxyAdapter;
2626
use Symfony\Component\Cache\Adapter\RedisAdapter;
2727
use Symfony\Component\DependencyInjection\ChildDefinition;
28+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2829
use Symfony\Component\DependencyInjection\ContainerBuilder;
2930
use Symfony\Component\DependencyInjection\Definition;
3031
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
@@ -589,10 +590,12 @@ public function testValidationService()
589590

590591
public function testAnnotations()
591592
{
592-
$container = $this->createContainerFromFile('full');
593+
$container = $this->createContainerFromFile('full', array(), true, false);
594+
$container->addCompilerPass(new TestAnnotationsPass());
595+
$container->compile();
593596

594597
$this->assertEquals($container->getParameter('kernel.cache_dir').'/annotations', $container->getDefinition('annotations.filesystem_cache')->getArgument(0));
595-
$this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotations.cached_reader')->getArgument(1));
598+
$this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotation_reader')->getArgument(1));
596599
}
597600

598601
public function testFileLinkFormat()
@@ -1051,10 +1054,10 @@ protected function createContainer(array $data = array())
10511054
), $data)));
10521055
}
10531056

1054-
protected function createContainerFromFile($file, $data = array(), $resetCompilerPasses = true)
1057+
protected function createContainerFromFile($file, $data = array(), $resetCompilerPasses = true, $compile = true)
10551058
{
10561059
$cacheKey = md5(get_class($this).$file.serialize($data));
1057-
if (isset(self::$containerCache[$cacheKey])) {
1060+
if ($compile && isset(self::$containerCache[$cacheKey])) {
10581061
return self::$containerCache[$cacheKey];
10591062
}
10601063
$container = $this->createContainer($data);
@@ -1065,7 +1068,12 @@ protected function createContainerFromFile($file, $data = array(), $resetCompile
10651068
$container->getCompilerPassConfig()->setOptimizationPasses(array());
10661069
$container->getCompilerPassConfig()->setRemovingPasses(array());
10671070
}
1068-
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass(), new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')));
1071+
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')));
1072+
$container->getCompilerPassConfig()->setAfterRemovingPasses(array(new AddAnnotationsCachedReaderPass()));
1073+
1074+
if (!$compile) {
1075+
return $container;
1076+
}
10691077
$container->compile();
10701078

10711079
return self::$containerCache[$cacheKey] = $container;
@@ -1158,3 +1166,15 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con
11581166
}
11591167
}
11601168
}
1169+
1170+
/**
1171+
* Simulates ReplaceAliasByActualDefinitionPass.
1172+
*/
1173+
class TestAnnotationsPass implements CompilerPassInterface
1174+
{
1175+
public function process(ContainerBuilder $container)
1176+
{
1177+
$container->setDefinition('annotation_reader', $container->getDefinition('annotations.cached_reader'));
1178+
$container->removeDefinition('annotations.cached_reader');
1179+
}
1180+
}

0 commit comments

Comments
 (0)
0