8000 [FrameworkBundle] Respect debug mode when warm up annotations · symfony/symfony@f3ec396 · GitHub
[go: up one dir, main page]

Skip to content

Commit f3ec396

Browse files
Stratefabpot
authored andcommitted
[FrameworkBundle] Respect debug mode when warm up annotations
1 parent 985ef53 commit f3ec396

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
2828
{
2929
private $annotationReader;
3030
private $excludeRegexp;
31+
private $debug;
3132

3233
/**
3334
* @param Reader $annotationReader
3435
* @param string $phpArrayFile The PHP file where annotations are cached
3536
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached
37+
* @param bool $debug Run in debug mode
3638
*/
37-
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null)
39+
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null, $debug = false)
3840
{
3941
parent::__construct($phpArrayFile, $fallbackPool);
4042
$this->annotationReader = $annotationReader;
4143
$this->excludeRegexp = $excludeRegexp;
44+
$this->debug = $debug;
4245
}
4346

4447
/**
@@ -53,7 +56,7 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
5356
}
5457

5558
$annotatedClasses = include $annotatedClassPatterns;
56-
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter));
59+
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug);
5760

5861
foreach ($annotatedClasses as $class) {
5962
if (null !== $this->excludeRegexp && preg_match($this->excludeRegexp, $class)) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<argument>%kernel.cache_dir%/annotations.php</argument>
3939
<argument type="service" id="cache.annotations" />
4040
<argument>#^Symfony\\(?:Component\\HttpKernel\\|Bundle\\FrameworkBundle\\Controller\\(?!AbstractController$|Controller$))#</argument>
41+
<argument>%kernel.debug%</argument>
4142
</service>
4243

4344
<service id="annotations.cache" class="Symfony\Component\Cache\DoctrineProvider">
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
4+
5+
use Doctrine\Common\Annotations\AnnotationReader;
6+
use Doctrine\Common\Annotations\CachedReader;
7+
use Doctrine\Common\Annotations\Reader;
8+
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer;
9+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
10+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
11+
use Symfony\Component\Cache\Adapter\NullAdapter;
12+
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
13+
use Symfony\Component\Cache\DoctrineProvider;
14+
use Symfony\Component\Filesystem\Filesystem;
15+
16+
class AnnotationsCacheWarmerTest extends TestCase
17+
{
18+
private $cacheDir;
19+
20+
protected function setUp()
21+
{
22+
$this->cacheDir = sys_get_temp_dir().'/'.uniqid();
23+
$fs = new Filesystem();
24+
$fs->mkdir($this->cacheDir);
25+
parent::setUp();
26+
}
27+
28+
protected function tearDown()
29+
{
30+
$fs = new Filesystem();
31+
$fs->remove($this->cacheDir);
32+
parent::tearDown();
33+
}
34+
35+
public function testAnnotationsCacheWarmerWithDebugDisabled()
36+
{
37+
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export(array(__CLASS__), true)));
38+
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
39+
$reader = new AnnotationReader();
40+
$fallbackPool = new ArrayAdapter();
41+
$warmer = new AnnotationsCacheWarmer(
42+
$reader,
43+
$cacheFile,
44+
$fallbackPool,
45+
null
46+
);
47+
$warmer->warmUp($this->cacheDir);
48+
$this->assertFileExists($cacheFile);
49+
50+
// Assert cache is valid
51+
$reader = new CachedReader(
52+
$this->getReadOnlyReader(),
53+
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter()))
54+
);
55+
$refClass = new \ReflectionClass($this);
56+
$reader->getClassAnnotations($refClass);
57+
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
58+
$reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
59+
}
60+
61+
public function testAnnotationsCacheWarmerWithDebugEnabled()
62+
{
63+
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export(array(__CLASS__), true)));
64+
$cacheFile = tempnam($this->cacheDir, __FUNCTION__);
65+
$reader = new AnnotationReader();
66+
$fallbackPool = new ArrayAdapter();
67+
$warmer = new AnnotationsCacheWarmer(
68+
$reader,
69+
$cacheFile,
70+
$fallbackPool,
71+
null,
72+
true
73+
);
74+
$warmer->warmUp($this->cacheDir);
75+
$this->assertFileExists($cacheFile);
76+
// Assert cache is valid
77+
$reader = new CachedReader(
78+
$this->getReadOnlyReader(),
79+
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())),
80+
true
81+
);
82+
$refClass = new \ReflectionClass($this);
83+
$reader->getClassAnnotations($refClass);
84+
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
85+
$reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
86+
}
87+
88+
/**
89+
* @return \PHPUnit_Framework_MockObject_MockObject|Reader
90+
*/
91+
private function getReadOnlyReader()
92+
{
93+
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')->getMock();
94+
$readerMock->expects($this->exactly(0))->method('getClassAnnotations');
95+
$readerMock->expects($this->exactly(0))->method('getClassAnnotation');
96+
$readerMock->expects($this->exactly(0))->method('getMethodAnnotations');
97+
$readerMock->expects($this->exactly(0))->method('getMethodAnnotation');
98+
$readerMock->expects($this->exactly(0))->method('getPropertyAnnotations');
99+
$readerMock->expects($this->exactly(0))->method('getPropertyAnnotation');
100+
101+
return $readerMock;
102+
}
103+
}

0 commit comments

Comments
 (0)
0