Skip to content

Commit 0e9b5ce

Browse files
committed
Fix deprecations from Doctrine Annotations+Cache
1 parent 98e855f commit 0e9b5ce

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationException;
1515
use Doctrine\Common\Annotations\CachedReader;
16+
use Doctrine\Common\Annotations\PsrCachedReader;
1617
use Doctrine\Common\Annotations\Reader;
1718
use Psr\Cache\CacheItemPoolInterface;
1819
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -61,7 +62,10 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
6162
}
6263

6364
$annotatedClasses = include $annotatedClassPatterns;
64-
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug);
65+
$reader = class_exists(PsrCachedReader::class)
66+
? new PsrCachedReader($this->annotationReader, $arrayAdapter, $this->debug)
67+
: new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug)
68+
;
6569

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

DependencyInjection/FrameworkExtension.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

1414
use Doctrine\Common\Annotations\AnnotationRegistry;
15+
use Doctrine\Common\Annotations\PsrCachedReader;
1516
use Doctrine\Common\Annotations\Reader;
1617
use Http\Client\HttpClient;
1718
use Psr\Cache\CacheItemPoolInterface;
@@ -1423,14 +1424,20 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
14231424
}
14241425

14251426
if ('none' !== $config['cache']) {
1426-
if (!class_exists(\Doctrine\Common\Cache\CacheProvider::class)) {
1427+
if (class_exists(PsrCachedReader::class)) {
1428+
$container
1429+
->getDefinition('annotations.cached_reader')
1430+
->setClass(PsrCachedReader::class)
1431+
->replaceArgument(1, new Definition(ArrayAdapter::class))
1432+
;
1433+
} elseif (!class_exists(\Doctrine\Common\Cache\CacheProvider::class)) {
14271434
throw new LogicException('Annotations cannot be enabled as the Doctrine Cache library is not installed.');
14281435
}
14291436

14301437
$cacheService = $config['cache'];
14311438

14321439
if ('php_array' === $config['cache']) {
1433-
$cacheService = 'annotations.cache';
1440+
$cacheService = class_exists(PsrCachedReader::class) ? 'annotations.cache_adapter' : 'annotations.cache';
14341441

14351442
// Enable warmer only if PHP array is used for cache
14361443
$definition = $container->findDefinition('annotations.cache_warmer');
@@ -1447,7 +1454,7 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
14471454
->replaceArgument(2, $cacheDir)
14481455
;
14491456

1450-
$cacheService = 'annotations.filesystem_cache';
1457+
$cacheService = class_exists(PsrCachedReader::class) ? 'annotations.filesystem_cache_adapter' : 'annotations.filesystem_cache';
14511458
}
14521459

14531460
$container

Resources/config/annotations.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@
5050
<argument>%kernel.debug%</argument>
5151
</service>
5252

53+
<service id="annotations.cache_adapter" class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">
54+
<factory class="Symfony\Component\Cache\Adapter\PhpArrayAdapter" method="create" />
55+
<argument>%kernel.cache_dir%/annotations.php</argument>
56+
<argument type="service" id="cache.annotations" />
57+
<tag name="container.hot_path" />
58+
</service>
59+
5360
<service id="annotations.cache" class="Symfony\Component\Cache\DoctrineProvider">
54-
<argument type="service">
55-
<service class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">
56-
<factory class="Symfony\Component\Cache\Adapter\PhpArrayAdapter" method="create" />
57-
<argument>%kernel.cache_dir%/annotations.php</argument>
58-
<argument type="service" id="cache.annotations" />
59-
</service>
60-
</argument>
61+
<argument type="service" id="annotations.cache_adapter" />
6162
<tag name="container.hot_path" />
6263
</service>
6364

Tests/CacheWarmer/AnnotationsCacheWarmerTest.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\Common\Annotations\AnnotationReader;
66
use Doctrine\Common\Annotations\CachedReader;
7+
use Doctrine\Common\Annotations\PsrCachedReader;
78
use Doctrine\Common\Annotations\Reader;
89
use PHPUnit\Framework\MockObject\MockObject;
910
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer;
@@ -43,10 +44,16 @@ public function testAnnotationsCacheWarmerWithDebugDisabled()
4344
$this->assertFileExists($cacheFile);
4445

4546
// Assert cache is valid
46-
$reader = new CachedReader(
47-
$this->getReadOnlyReader(),
48-
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter()))
49-
);
47+
$reader = class_exists(PsrCachedReader::class)
48+
? new PsrCachedReader(
49+
$this->getReadOnlyReader(),
50+
new PhpArrayAdapter($cacheFile, new NullAdapter())
51+
)
52+
: new CachedReader(
53+
$this->getReadOnlyReader(),
54+
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter()))
55+
)
56+
;
5057
$refClass = new \ReflectionClass($this);
5158
$reader->getClassAnnotations($refClass);
5259
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
@@ -61,12 +68,21 @@ public function testAnnotationsCacheWarmerWithDebugEnabled()
6168
$warmer = new AnnotationsCacheWarmer($reader, $cacheFile, null, true);
6269
$warmer->warmUp($this->cacheDir);
6370
$this->assertFileExists($cacheFile);
71+
6472
// Assert cache is valid
65-
$reader = new CachedReader(
66-
$this->getReadOnlyReader(),
67-
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())),
68-
true
69-
);
73+
$phpArrayAdapter = new PhpArrayAdapter($cacheFile, new NullAdapter());
74+
$reader = class_exists(PsrCachedReader::class)
75+
? new PsrCachedReader(
76+
$this->getReadOnlyReader(),
77+
$phpArrayAdapter,
78+
true
79+
)
80+
: new CachedReader(
81+
$this->getReadOnlyReader(),
82+
new DoctrineProvider($phpArrayAdapter),
83+
true
84+
)
85+
;
7086
$refClass = new \ReflectionClass($this);
7187
$reader->getClassAnnotations($refClass);
7288
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
1313

1414
use Doctrine\Common\Annotations\Annotation;
15+
use Doctrine\Common\Annotations\PsrCachedReader;
1516
use Psr\Log\LoggerAwareInterface;
1617
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
1718
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
@@ -999,7 +1000,11 @@ public function testAnnotations()
9991000
$container->compile();
10001001

10011002
$this->assertEquals($container->getParameter('kernel.cache_dir').'/annotations', $container->getDefinition('annotations.filesystem_cache_adapter')->getArgument(2));
1002-
$this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotation_reader')->getArgument(1));
1003+
if (class_exists(PsrCachedReader::class)) {
1004+
$this->assertSame('annotations.filesystem_cache_adapter', (string) $container->getDefinition('annotation_reader')->getArgument(1));
1005+
} else {
1006+
$this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotation_reader')->getArgument(1));
1007+
}
10031008
}
10041009

10051010
public function testFileLinkFormat()

Tests/Functional/AutowiringTypesTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Doctrine\Common\Annotations\CachedReader;
16+
use Doctrine\Common\Annotations\PsrCachedReader;
1617
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface;
1718
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1819
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -35,7 +36,11 @@ public function testCachedAnnotationReaderAutowiring()
3536
static::bootKernel();
3637

3738
$annotationReader = static::$container->get('test.autowiring_types.autowired_services')->getAnnotationReader();
38-
$this->assertInstanceOf(CachedReader::class, $annotationReader);
39+
if (class_exists(PsrCachedReader::class)) {
40+
$this->assertInstanceOf(PsrCachedReader::class, $annotationReader);
41+
} else {
42+
$this->assertInstanceOf(CachedReader::class, $annotationReader);
43+
}
3944
}
4045

4146
/**

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=7.1.3",
2020
"ext-xml": "*",
2121
"symfony/cache": "^4.4|^5.0",
22-
"symfony/config": "^4.3.4|^5.0",
22+
"symfony/config": "^4.4.11|~5.0.11|^5.1.3",
2323
"symfony/dependency-injection": "^4.4.1|^5.0.1",
2424
"symfony/error-handler": "^4.4.1|^5.0.1",
2525
"symfony/http-foundation": "^4.4|^5.0",
@@ -31,7 +31,7 @@
3131
},
3232
"require-dev": {
3333
"doctrine/annotations": "^1.10.4",
34-
"doctrine/cache": "~1.0",
34+
"doctrine/cache": "^1.0|^2.0",
3535
"doctrine/persistence": "^1.3|^2.0",
3636
"paragonie/sodium_compat": "^1.8",
3737
"symfony/asset": "^3.4|^4.0|^5.0",

0 commit comments

Comments
 (0)