Skip to content

Commit 650850a

Browse files
Merge branch '4.0'
* 4.0: (32 commits) [Form] fix tests and deps [Cache] Rely on mock for Doctrine ArrayCache [FrameworkBundle] Respect debug mode when warm up annotations [Console] Fix docblock of DescriptorInterface::describe [Config] Handle nullable node name + fix inheritdocs [Security] added userChecker to SimpleAuthenticationProvider [Debug] fix test Fix typo in test method name Fixes #26563 (open_basedir restriction in effect) [Debug] Reset previous exception handler ealier to prevent infinite loop add hint in Github pull request template [Validator] Fix docblock of ClassMetadata#members [BrowserKit] Fix cookie path handling when $domain is null [DoctrineBridge] Don't rely on ClassMetadataInfo->hasField in DoctrineOrmTypeGuesser anymore [BrowserKit] Improves CookieJar::get [BrowserKit] Fix Cookie's PHPDoc [DomCrawler] Change bad wording in ChoiceFormField::untick [DomCrawler] Fix the PHPDoc of ChoiceFormField::setValue [DomCrawler] Avoid a useless call to strtolower [FrameworkBundle] HttpCache is not longer abstract ...
2 parents 0a94052 + c229de8 commit 650850a

File tree

4 files changed

+113
-7
lines changed

4 files changed

+113
-7
lines changed

CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ 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
3637
*/
37-
public function __construct(Reader $annotationReader, string $phpArrayFile, CacheItemPoolInterface $fallbackPool, string $excludeRegexp = null)
38+
public function __construct(Reader $annotationReader, string $phpArrayFile, CacheItemPoolInterface $fallbackPool, string $excludeRegexp = null, bool $debug = false)
3839
{
3940
parent::__construct($phpArrayFile, $fallbackPool);
4041
$this->annotationReader = $annotationReader;
4142
$this->excludeRegexp = $excludeRegexp;
43+
$this->debug = $debug;
4244
}
4345

4446
/**
@@ -53,7 +55,7 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
5355
}
5456

5557
$annotatedClasses = include $annotatedClassPatterns;
56-
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter));
58+
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug);
5759

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

HttpCache/HttpCache.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\HttpCache;
1313

14-
use Symfony\Component\HttpKernel\HttpKernelInterface;
14+
use Symfony\Component\HttpKernel\KernelInterface;
1515
use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache;
1616
use Symfony\Component\HttpKernel\HttpCache\Esi;
1717
use Symfony\Component\HttpKernel\HttpCache\Store;
@@ -23,16 +23,16 @@
2323
*
2424
* @author Fabien Potencier <[email protected]>
2525
*/
26-
abstract class HttpCache extends BaseHttpCache
26+
class HttpCache extends BaseHttpCache
2727
{
2828
protected $cacheDir;
2929
protected $kernel;
3030

3131
/**
32-
* @param HttpKernelInterface $kernel An HttpKernelInterface instance
33-
* @param string $cacheDir The cache directory (default used if null)
32+
* @param KernelInterface $kernel A KernelInterface instance
33+
* @param string $cacheDir The cache directory (default used if null)
3434
*/
35-
public function __construct(HttpKernelInterface $kernel, string $cacheDir = null)
35+
public function __construct(KernelInterface $kernel, string $cacheDir = null)
3636
{
3737
$this->kernel = $kernel;
3838
$this->cacheDir = $cacheDir;

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)