Navigation Menu

Skip to content

Commit

Permalink
bug #26086 [FrameworkBundle] Fix using annotation_reader in compiler …
Browse files Browse the repository at this point in the history
…pass to inject configured cache provider
  • Loading branch information
Laizerox authored and nicolas-grekas committed Feb 19, 2018
1 parent 39e88ed commit dfd93da
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
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
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
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
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');
}
}

0 comments on commit dfd93da

Please sign in to comment.