[FrameworkBundle] Respect debug mode when warm up annotations

This commit is contained in:
Artur Eshenbrener 2018-03-13 19:09:28 +03:00 committed by Fabien Potencier
parent 985ef53b51
commit f3ec39616c
3 changed files with 109 additions and 2 deletions

View File

@ -28,17 +28,20 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
{
private $annotationReader;
private $excludeRegexp;
private $debug;
/**
* @param Reader $annotationReader
* @param string $phpArrayFile The PHP file where annotations are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached
* @param bool $debug Run in debug mode
*/
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null)
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null, $debug = false)
{
parent::__construct($phpArrayFile, $fallbackPool);
$this->annotationReader = $annotationReader;
$this->excludeRegexp = $excludeRegexp;
$this->debug = $debug;
}
/**
@ -53,7 +56,7 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
}
$annotatedClasses = include $annotatedClassPatterns;
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter));
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug);
foreach ($annotatedClasses as $class) {
if (null !== $this->excludeRegexp && preg_match($this->excludeRegexp, $class)) {

View File

@ -38,6 +38,7 @@
<argument>%kernel.cache_dir%/annotations.php</argument>
<argument type="service" id="cache.annotations" />
<argument>#^Symfony\\(?:Component\\HttpKernel\\|Bundle\\FrameworkBundle\\Controller\\(?!AbstractController$|Controller$))#</argument>
<argument>%kernel.debug%</argument>
</service>
<service id="annotations.cache" class="Symfony\Component\Cache\DoctrineProvider">

View File

@ -0,0 +1,103 @@
<?php
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
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;
use Symfony\Component\Filesystem\Filesystem;
class AnnotationsCacheWarmerTest extends TestCase
{
private $cacheDir;
protected function setUp()
{
$this->cacheDir = sys_get_temp_dir().'/'.uniqid();
$fs = new Filesystem();
$fs->mkdir($this->cacheDir);
parent::setUp();
}
protected function tearDown()
{
$fs = new Filesystem();
$fs->remove($this->cacheDir);
parent::tearDown();
}
public function testAnnotationsCacheWarmerWithDebugDisabled()
{
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export(array(__CLASS__), true)));
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
$reader = new AnnotationReader();
$fallbackPool = new ArrayAdapter();
$warmer = new AnnotationsCacheWarmer(
$reader,
$cacheFile,
$fallbackPool,
null
);
$warmer->warmUp($this->cacheDir);
$this->assertFileExists($cacheFile);
// Assert cache is valid
$reader = new CachedReader(
$this->getReadOnlyReader(),
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter()))
);
$refClass = new \ReflectionClass($this);
$reader->getClassAnnotations($refClass);
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
$reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
}
public function testAnnotationsCacheWarmerWithDebugEnabled()
{
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export(array(__CLASS__), true)));
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
$reader = new AnnotationReader();
$fallbackPool = new ArrayAdapter();
$warmer = new AnnotationsCacheWarmer(
$reader,
$cacheFile,
$fallbackPool,
null,
true
);
$warmer->warmUp($this->cacheDir);
$this->assertFileExists($cacheFile);
// Assert cache is valid
$reader = new CachedReader(
$this->getReadOnlyReader(),
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())),
true
);
$refClass = new \ReflectionClass($this);
$reader->getClassAnnotations($refClass);
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
$reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|Reader
*/
private function getReadOnlyReader()
{
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')->getMock();
$readerMock->expects($this->exactly(0))->method('getClassAnnotations');
$readerMock->expects($this->exactly(0))->method('getClassAnnotation');
$readerMock->expects($this->exactly(0))->method('getMethodAnnotations');
$readerMock->expects($this->exactly(0))->method('getMethodAnnotation');
$readerMock->expects($this->exactly(0))->method('getPropertyAnnotations');
$readerMock->expects($this->exactly(0))->method('getPropertyAnnotation');
return $readerMock;
}
}