8000 Fix deprecations from Doctrine Annotations+Cache · symfony/symfony@3b8dfc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b8dfc7

Browse files
committed
Fix deprecations from Doctrine Annotations+Cache
1 parent d3ebc5f commit 3b8dfc7

File tree

15 files changed

+110
-47
lines changed

15 files changed

+110
-47
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"cache/integration-tests": "dev-master",
120120
"composer/package-versions-deprecated": "^1.8",
121121
"doctrine/annotations": "^1.10.4",
122-
"doctrine/cache": "~1.6",
122+
"doctrine/cache": "^1.6|^2.0",
123123
"doctrine/collections": "~1.0",
124124
"doctrine/data-fixtures": "^1.1",
125125
"doctrine/dbal": "^2.6|^3.0",

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"symfony/var-dumper": "^3.4|^4.0|^5.0",
4141
"symfony/translation": "^3.4|^4.0|^5.0",
4242
"doctrine/annotations": "^1.10.4",
43-
"doctrine/cache": "~1.6",
4443
"doctrine/collections": "~1.0",
4544
"doctrine/data-fixtures": "^1.1",
4645
"doctrine/dbal": "^2.6|^3.0",

src/Symfony/Bundle/FrameworkBundle/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)) {

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php

Lines changed: 24 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__));
@@ -62,11 +69,19 @@ public function testAnnotationsCacheWarmerWithDebugEnabled()
6269
$warmer->warmUp($this->cacheDir);
6370
$this->assertFileExists($cacheFile);
6471
// Assert cache is valid
65-
$reader = new CachedReader(
66-
$this->getReadOnlyReader(),
67-
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())),
68-
true
69-
);
72+
$phpArrayAdapter = new PhpArrayAdapter($cacheFile, new NullAdapter());
73+
$reader = class_exists(PsrCachedReader::class)
74+
? new PsrCachedReader(
75+
$this->getReadOnlyReader(),
76+
$phpArrayAdapter,
77+
true
78+
)
79+
: new CachedReader(
80+
$this->getReadOnlyReader(),
81+
new DoctrineProvider($phpArrayAdapter),
82+
true
83+
)
84+
;
7085
$refClass = new \ReflectionClass($this);
7186
$reader->getClassAnnotations($refClass);
7287
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));

src/Symfony/Bundle/FrameworkBundle/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()

src/Symfony/Bundle/FrameworkBundle/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
/**

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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",

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"symfony/framework-bundle": "^4.4|^5.0",
3838
"symfony/web-link": "^3.4|^4.0|^5.0",
3939
"doctrine/annotations": "^1.10.4",
40-
"doctrine/cache": "~1.0"
40+
"doctrine/cache": "^1.0|^2.0"
4141
},
4242
"conflict": {
4343
"symfony/dependency-injection": "<4.1",

src/Symfony/Component/Cache/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"require-dev": {
3232
"cache/integration-tests": "dev-master",
33-
"doctrine/cache": "^1.6",
33+
"doctrine/cache": "^1.6|^2.0",
3434
"doctrine/dbal": "^2.6|^3.0",
3535
"predis/predis": "^1.1",
3636
"psr/simple-cache": "^1.0",

src/Symfony/Component/Serializer/composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
},
2222
"require-dev": {
2323
"doctrine/annotations": "^1.10.4",
24-
"doctrine/cache": "~1.0",
2524
"phpdocumentor/reflection-docblock": "^3.2|^4.0|^5.0",
2625
"symfony/cache": "^3.4|^4.0|^5.0",
2726
"symfony/config": "^3.4|^4.0|^5.0",
@@ -49,8 +48,7 @@
4948
"symfony/config": "For using the XML mapping loader.",
5049
"symfony/property-access": "For using the ObjectNormalizer.",
5150
"symfony/http-foundation": "For using a MIME type guesser within the DataUriNormalizer.",
52-
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
53-
"doctrine/cache": "For using the default cached annotation reader and metadata cache."
51+
"doctrine/annotations": "For using the annotation mapping."
5452
},
5553
"autoload": {
5654
"psr-4": { "Symfony\\Component\\Serializer\\": "" },

src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping\Cache;
1313

1414
use Doctrine\Common\Cache\ArrayCache;
15+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
16+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1517
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
1618

1719
/**
@@ -21,6 +23,9 @@ class DoctrineCacheTest extends AbstractCacheTest
2123
{
2224
protected function setUp(): void
2325
{
24-
$this->cache = new DoctrineCache(new ArrayCache());
26+
$this->cache = class_exists(DoctrineProvider::class)
27+
? new DoctrineCache(DoctrineProvider::wrap(new ArrayAdapter()))
28+
: new DoctrineCache(new ArrayCache())
29+
;
2530
}
2631
}

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Doctrine\Common\Annotations\CachedReader;
16+
use Doctrine\Common\Annotations\PsrCachedReader;
1617
use Doctrine\Common\Annotations\Reader;
1718
use Doctrine\Common\Cache\ArrayCache;
18-
use Doctrine\Common\Cache\CacheProvider;
19+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
1920
use Psr\Cache\CacheItemPoolInterface;
2021
use Symfony\Component\Cache\Adapter\ArrayAdapter;
21-
use Symfony\Component\Cache\DoctrineProvider;
22+
use Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider;
2223
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
2324
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2425
use Symfony\Component\Validator\Exception\LogicException;
@@ -199,19 +200,7 @@ public function enableAnnotationMapping(Reader $annotationReader = null)
199200
throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
200201
}
201202

202-
if (null === $annotationReader) {
203-
if (!class_exists(AnnotationReader::class) || !class_exists(CacheProvider::class)) {
204-
throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.');
205-
}
206-
207-
if (class_exists(ArrayAdapter::class)) {
208-
$annotationReader = new CachedReader(new AnnotationReader(), new DoctrineProvider(new ArrayAdapter()));
209-
} else {
210-
$annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
211-
}
212-
}
213-
214-
$this->annotationReader = $annotationReader;
203+
$this->annotationReader = $annotationReader ?? $this->createAnnotationReader();
215204

216205
return $this;
217206
}
@@ -386,4 +375,39 @@ public function getValidator()
386375

387376
return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
388377
}
378+
379+
private function createAnnotationReader(): Reader
380+
{
381+
if (!class_exists(AnnotationReader::class)) {
382+
throw new LogicException('Enabling annotation based constraint mapping requires the package doctrine/annotations to be installed.');
383+
}
384+
385+
// Doctrine Annotation >= 1.13, Symfony Cache
386+
if (class_exists(PsrCachedReader::class) && class_exists(ArrayAdapter::class)) {
387+
return new PsrCachedReader(new AnnotationReader(), new ArrayAdapter());
388+
}
389+
390+
// Doctrine Annotations < 1.13, Doctrine Cache >= 1.11, Symfony Cache
391+
if (class_exists(CachedReader::class) && class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) {
392+
return new CachedReader(new AnnotationReader(), DoctrineProvider::wrap(new ArrayAdapter()));
393+
}
394+
395+
// Doctrine Annotations < 1.13, Doctrine Cache < 1.11, Symfony Cache
396+
if (class_exists(CachedReader::class) && !class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) {
397+
return new CachedReader(new AnnotationReader(), new SymfonyDoctrineProvider(new ArrayAdapter()));
398+
}
399+
400+
// Doctrine Annotations < 1.13, Doctrine Cache < 1.11
401+
if (class_exists(CachedReader::class) && class_exists(ArrayCache::class)) {
402+
return new CachedReader(new AnnotationReader(), new ArrayCache());
403+
}
404+
405+
// Doctrine Annotation >= 1.13, Doctrine Cache >= 2, no Symfony Cache
406+
if (class_exists(PsrCachedReader::class)) {
407+
throw new LogicException('Enabling annotation based constraint mapping requires the package symfony/cache to be installed.');
408+
}
409+
410+
// Doctrine Annotation (<1.13 || >2), no Doctrine Cache, no Symfony Cache
411+
throw new LogicException('Enabling annotation based constraint mapping requires the package doctrine/cache 1.x to be installed.');
412+
}
389413
}

src/Symfony/Component/Validator/composer.json

Lines changed: 1 addition & 7B98 ; 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"symfony/property-info": "^3.4|^4.0|^5.0",
3737
"symfony/translation": "^4.2",
3838
"doctrine/annotations": "^1.10.4",
39-
"doctrine/cache": "~1.0",
39+
"doctrine/cache": "^1.0|^2.0",
4040
"egulias/email-validator": "^2.1.10|^3"
4141
},
4242
"conflict": {

0 commit comments

Comments
 (0)
0