Skip to content

[FrameworkBundle] Fix using annotation_reader in compiler pass to inject configured cache provider #26088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public function process(ContainerBuilder $container)
{
// "annotations.cached_reader" is wired late so that any passes using
// "annotation_reader" at build time don't get any cache
if ($container->hasDefinition('annotations.cached_reader')) {
$reader = $container->getDefinition('annotations.cached_reader');
foreach ($container->findTaggedServiceIds('annotations.cached_reader') as $id => $tags) {
$reader = $container->getDefinition($id);
$properties = $reader->getProperties();

if (isset($properties['cacheProviderBackup'])) {
$provider = $properties['cacheProviderBackup']->getValues()[0];
unset($properties['cacheProviderBackup']);
$reader->setProperties($properties);
$container->set('annotations.cached_reader', null);
$container->setDefinition('annotations.cached_reader', $reader->replaceArgument(1, $provider));
$container->set($id, null);
$container->setDefinition($id, $reader->replaceArgument(1, $provider));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class UnusedTagsPass implements CompilerPassInterface
{
private $whitelist = array(
'annotations.cached_reader',
'cache.pool.clearer',
'console.command',
'container.hot_path',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,9 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
->replaceArgument(2, $config['debug'])
// temporary property to lazy-reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
->setProperty('cacheProviderBackup', new ServiceClosureArgument(new Reference($cacheService)))
->addTag('annotations.cached_reader')
;

$container->setAlias('annotation_reader', 'annotations.cached_reader')->setPrivate(true);
$container->setAlias(Reader::class, new Alias('annotations.cached_reader', false));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
Expand Down Expand Up @@ -589,10 +590,12 @@ public function testValidationService()

public function testAnnotations()
{
$container = $this->createContainerFromFile('full');
$container = $this->createContainerFromFile('full', array(), true, false);
$container->addCompilerPass(new TestAnnotationsPass());
$container->compile();

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

public function testFileLinkFormat()
Expand Down Expand Up @@ -1051,10 +1054,10 @@ protected function createContainer(array $data = array())
), $data)));
}

protected function createContainerFromFile($file, $data = array(), $resetCompilerPasses = true)
protected function createContainerFromFile($file, $data = array(), $resetCompilerPasses = true, $compile = true)
{
$cacheKey = md5(get_class($this).$file.serialize($data));
if (isset(self::$containerCache[$cacheKey])) {
if ($compile && isset(self::$containerCache[$cacheKey])) {
return self::$containerCache[$cacheKey];
}
$container = $this->createContainer($data);
Expand All @@ -1065,7 +1068,12 @@ protected function createContainerFromFile($file, $data = array(), $resetCompile
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
}
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass(), new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')));
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')));
$container->getCompilerPassConfig()->setAfterRemovingPasses(array(new AddAnnotationsCachedReaderPass()));

if (!$compile) {
return $container;
}
$container->compile();

return self::$containerCache[$cacheKey] = $container;
Expand Down Expand Up @@ -1158,3 +1166,15 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con
}
}
}

/**
* Simulates ReplaceAliasByActualDefinitionPass.
*/
class TestAnnotationsPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->setDefinition('annotation_reader', $container->getDefinition('annotations.cached_reader'));
$container->removeDefinition('annotations.cached_reader');
}
}