diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php index ec55e9c6fc..3c32cb1c4a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php @@ -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)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml index 6e8cc4f9e6..2b4ea42962 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml @@ -38,6 +38,7 @@ %kernel.cache_dir%/annotations.php #^Symfony\\(?:Component\\HttpKernel\\|Bundle\\FrameworkBundle\\Controller\\(?!AbstractController$|Controller$))# + %kernel.debug% diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php new file mode 100644 index 0000000000..b32274e7e7 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php @@ -0,0 +1,103 @@ +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('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('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; + } +}