Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fdabaf2

Browse files
committed
feature #40338 [FrameworkBundle] Add support for doctrine/annotations:1.13 || 2.0 (Nyholm)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [FrameworkBundle] Add support for doctrine/annotations:1.13 || 2.0 | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | | License | MIT | Doc PR | This PR will deprecate passing any other values that "none", "php_array" and "file" to `framework.annotation.cache`. It will also support `doctrine/annotations:^1.13 || ^2.0`. It will use the PSR-6 cache if possible. Commits ------- 91e844b [FrameworkBundle] Add support for doctrine/annotations:1.13 || 2.0
2 parents 3a92844 + 91e844b commit fdabaf2

File tree

6 files changed

+84
-21
lines changed

6 files changed

+84
-21
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ CHANGELOG
1414
* Added `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`
1515
* Add support for configuring UUID factory services
1616
* Add tag `assets.package` to register asset packages
17+
* Add support to use a PSR-6 compatible cache for Doctrine annotations
18+
* Deprecate all other values than "none", "php_array" and "file" for `framework.annotation.cache`
1719

1820
5.2.0
1921
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

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

1414
use Doctrine\Common\Annotations\Annotation;
15+
use Doctrine\Common\Annotations\PsrCachedReader;
1516
use Doctrine\Common\Cache\Cache;
1617
use Doctrine\DBAL\Connection;
1718
use Symfony\Bundle\FullStack;
@@ -921,13 +922,16 @@ private function addValidationSection(ArrayNodeDefinition $rootNode, callable $e
921922

922923
private function addAnnotationsSection(ArrayNodeDefinition $rootNode, callable $willBeAvailable)
923924
{
925+
$doctrineCache = $willBeAvailable('doctrine/cache', Cache::class, 'doctrine/annotation');
926+
$psr6Cache = $willBeAvailable('symfony/cache', PsrCachedReader::class, 'doctrine/annotation');
927+
924928
$rootNode
925929
->children()
926930
->arrayNode('annotations')
927931
->info('annotation configuration')
928932
->{$willBeAvailable('doctrine/annotations', Annotation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
929933
->children()
930-
->scalarNode('cache')->defaultValue($willBeAvailable('doctrine/cache', Cache::class, 'doctrine/annotation') ? 'php_array' : 'none')->end()
934+
->scalarNode('cache')->defaultValue(($doctrineCache || $psr6Cache) ? 'php_array' : 'none')->end()
931935
->scalarNode('file_cache_dir')->defaultValue('%kernel.cache_dir%/annotations')->end()
932936
->booleanNode('debug')->defaultValue($this->debug)->end()
933937
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,13 +1481,50 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
14811481
->setMethodCalls([['registerLoader', ['class_exists']]]);
14821482
}
14831483

1484-
if ('none' !== $config['cache']) {
1484+
if ('none' === $config['cache']) {
1485+
$container->removeDefinition('annotations.cached_reader');
1486+
$container->removeDefinition('annotations.psr_cached_reader');
1487+
1488+
return;
1489+
}
1490+
1491+
$cacheService = $config['cache'];
1492+
if (\in_array($config['cache'], ['php_array', 'file'])) {
1493+
$isPsr6Service = $container->hasDefinition('annotations.psr_cached_reader');
1494+
} else {
1495+
$isPsr6Service = false;
1496+
trigger_deprecation('symfony/framework-bundle', '5.3', 'Using a custom service for "framework.annotation.cache" is deprecated, only values "none", "php_array" and "file" are valid in version 6.0.');
1497+
}
1498+
1499+
if ($isPsr6Service) {
1500+
$container->removeDefinition('annotations.cached_reader');
1501+
$container->setDefinition('annotations.cached_reader', $container->getDefinition('annotations.psr_cached_reader'));
1502+
1503+
if ('php_array' === $config['cache']) {
1504+
$cacheService = 'annotations.psr_cache';
1505+
1506+
// Enable warmer only if PHP array is used for cache
1507+
$definition = $container->findDefinition('annotations.cache_warmer');
1508+
$definition->addTag('kernel.cache_warmer');
1509+
} elseif ('file' === $config['cache']) {
1510+
$cacheService = 'annotations.filesystem_cache_adapter';
1511+
$cacheDir = $container->getParameterBag()->resolveValue($config['file_cache_dir']);
1512+
1513+
if (!is_dir($cacheDir) && false === @mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
1514+
throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir));
1515+
}
1516+
1517+
$container
1518+
->getDefinition('annotations.filesystem_cache_adapter')
1519+
->replaceArgument(2, $cacheDir)
1520+
;
1521+
}
1522+
} else {
1523+
// Legacy code for doctrine/annotations:<1.13
14851524
if (!class_exists(\Doctrine\Common\Cache\CacheProvider::class)) {
14861525
throw new LogicException('Annotations cannot be cached as the Doctrine Cache library is not installed. Try running "composer require doctrine/cache".');
14871526
}
14881527

1489-
$cacheService = $config['cache'];
1490-
14911528
if ('php_array' === $config['cache']) {
14921529
$cacheService = 'annotations.cache';
14931530

@@ -1508,20 +1545,19 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
15081545

15091546
$cacheService = 'annotations.filesystem_cache';
15101547
}
1548+
}
15111549

1512-
$container
1513-
->getDefinition('annotations.cached_reader')
1514-
->replaceArgument(2, $config['debug'])
1515-
// temporary property to lazy-reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
1516-
->setProperty('cacheProviderBackup', new ServiceClosureArgument(new Reference($cacheService)))
1517-
->addTag('annotations.cached_reader')
1518-
;
1550+
$container
1551+
->getDefinition('annotations.cached_reader')
1552+
->replaceArgument(2, $config['debug'])
1553+
// temporary property to lazy-reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
1554+
->setProperty('cacheProviderBackup', new ServiceClosureArgument(new Reference($cacheService)))
1555+
->addTag('annotations.cached_reader')
1556+
;
15191557

1520-
$container->setAlias('annotation_reader', 'annotations.cached_reader');
1521-
$container->setAlias(Reader::class, new Alias('annotations.cached_reader', false));
1522-
} else {
1523-
$container->removeDefinition('annotations.cached_reader');
1524-
}
1558+
$container->setAlias('annotation_reader', 'annotations.cached_reader');
1559+
$container->setAlias(Reader::class, new Alias('annotations.cached_reader', false));
1560+
$container->removeDefinition('annotations.psr_cached_reader');
15251561
}
15261562

15271563
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Doctrine\Common\Annotations\AnnotationRegistry;
1616
use Doctrine\Common\Annotations\CachedReader;
17+
use Doctrine\Common\Annotations\PsrCachedReader;
1718
use Doctrine\Common\Annotations\Reader;
1819
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer;
1920
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -36,7 +37,7 @@
3637
->args([
3738
service('annotations.reader'),
3839
inline_service(DoctrineProvider::class)->args([
39-
inline_service(ArrayAdapter::class)
40+
inline_service(ArrayAdapter::class),
4041
]),
4142
abstract_arg('Debug-Flag'),
4243
])
@@ -74,4 +75,22 @@
7475

7576
->alias('annotation_reader', 'annotations.reader')
7677
->alias(Reader::class, 'annotation_reader');
78+
79+
if (class_exists(PsrCachedReader::class)) {
80+
$container->services()
81+
->set('annotations.psr_cached_reader', PsrCachedReader::class)
82+
->args([
83+
service('annotations.reader'),
84+
inline_service(ArrayAdapter::class),
85+
abstract_arg('Debug-Flag'),
86+
])
87+
->set('annotations.psr_cache', PhpArrayAdapter::class)
88+
->factory([PhpArrayAdapter::class, 'create'])
89+
->args([
90+
param('kernel.cache_dir').'/annotations.php',
91+
service('cache.annotations'),
92+
])
93+
->tag('container.hot_path')
94+
;
95+
}
7796
};

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 4 additions & 3 deletions
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\Cache\CacheItemPoolInterface;
1617
use Psr\Log\LoggerAwareInterface;
1718
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
@@ -578,7 +579,7 @@ public function testNullSessionHandler()
578579

579580
$expected = ['session', 'initialized_session', 'logger', 'session_collector'];
580581
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
581-
$this->assertSame(false, $container->getDefinition('session.storage.factory.native')->getArgument(3));
582+
$this->assertFalse($container->getDefinition('session.storage.factory.native')->getArgument(3));
582583
}
583584

584585
/**
@@ -597,7 +598,7 @@ public function testNullSessionHandlerLegacy()
597598

598599
$expected = ['session', 'initialized_session', 'logger', 'session_collector'];
599600
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
600-
$this->assertSame(false, $container->getDefinition('session.storage.factory.native')->getArgument(3));
601+
$this->assertFalse($container->getDefinition('session.storage.factory.native')->getArgument(3));
601602
}
602603

603604
public function testRequest()
@@ -980,7 +981,7 @@ public function testAnnotations()
980981
$container->compile();
981982

982983
$this->assertEquals($container->getParameter('kernel.cache_dir').'/annotations', $container->getDefinition('annotations.filesystem_cache_adapter')->getArgument(2));
983-
$this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotation_reader')->getArgument(1));
984+
$this->assertSame(class_exists(PsrCachedReader::class) ? 'annotations.filesystem_cache_adapter' : 'annotations.filesystem_cache', (string) $container->getDefinition('annotation_reader')->getArgument(1));
984985
}
985986

986987
public function testFileLinkFormat()

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php

Lines changed: 2 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\Component\Cache\Adapter\FilesystemAdapter;
1718
use Symfony\Component\EventDispatcher\EventDispatcher;
1819
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
@@ -33,7 +34,7 @@ public function testCachedAnnotationReaderAutowiring()
3334
static::bootKernel();
3435

3536
$annotationReader = static::$container->get('test.autowiring_types.autowired_services')->getAnnotationReader();
36-
$this->assertInstanceOf(CachedReader::class, $annotationReader);
37+
$this->assertInstanceOf(class_exists(PsrCachedReader::class) ? PsrCachedReader::class : CachedReader::class, $annotationReader);
3738
}
3839

3940
public function testEventDispatcherAutowiring()

0 commit comments

Comments
 (0)