diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php index c1fbfdf3d23d6..7dd50aa31a735 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php @@ -16,6 +16,7 @@ use Doctrine\Common\Annotations\Reader; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\Adapter\ArrayAdapter; +use Symfony\Component\Cache\Adapter\PhpArrayAdapter; use Symfony\Component\Cache\DoctrineProvider; /** @@ -76,6 +77,14 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter) return true; } + protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values) + { + // make sure we don't cache null values + $values = array_filter($values, function ($val) { return null !== $val; }); + + parent::warmUpPhpArrayAdapter($phpArrayAdapter, $values); + } + private function readAllComponents(Reader $reader, string $class) { $reflectionClass = new \ReflectionClass($class); diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php index 00df2aaedc79f..ec2c8df4ae5eb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php @@ -80,7 +80,9 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter) protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values) { // make sure we don't cache null values - parent::warmUpPhpArrayAdapter($phpArrayAdapter, array_filter($values)); + $values = array_filter($values, function ($val) { return null !== $val; }); + + parent::warmUpPhpArrayAdapter($phpArrayAdapter, $values); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php index ff03a66688004..9b257a2bf1b88 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; +use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\Cache\Adapter\NullAdapter; use Symfony\Component\Cache\Adapter\PhpArrayAdapter; use Symfony\Component\Cache\DoctrineProvider; @@ -120,6 +121,35 @@ public function testClassAutoloadExceptionWithUnrelatedException() spl_autoload_unregister($classLoader); } + public function testWarmupRemoveCacheMisses() + { + $cacheFile = tempnam($this->cacheDir, __FUNCTION__); + $warmer = $this->getMockBuilder(AnnotationsCacheWarmer::class) + ->setConstructorArgs([new AnnotationReader(), $cacheFile]) + ->setMethods(['doWarmUp']) + ->getMock(); + + $warmer->method('doWarmUp')->willReturnCallback(function ($cacheDir, ArrayAdapter $arrayAdapter) { + $arrayAdapter->getItem('foo_miss'); + + $item = $arrayAdapter->getItem('bar_hit'); + $item->set('data'); + $arrayAdapter->save($item); + + $item = $arrayAdapter->getItem('baz_hit_null'); + $item->set(null); + $arrayAdapter->save($item); + + return true; + }); + + $warmer->warmUp($this->cacheDir); + $data = include $cacheFile; + + $this->assertCount(1, $data[0]); + $this->assertTrue(isset($data[0]['bar_hit'])); + } + /** * @return MockObject|Reader */