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

Skip to content

[FrameworkBundle] Add support for doctrine/annotations:1.13 || 2.0 #40338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ CHANGELOG
* Added `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`
* Add support for configuring UUID factory services
* Add tag `assets.package` to register asset packages
* Add support to use a PSR-6 compatible cache for Doctrine annotations
* Deprecate all other values than "none", "php_array" and "file" for `framework.annotation.cache`

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;

use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Connection;
use Symfony\Bundle\FullStack;
Expand Down Expand Up @@ -921,13 +922,16 @@ private function addValidationSection(ArrayNodeDefinition $rootNode, callable $e

private function addAnnotationsSection(ArrayNodeDefinition $rootNode, callable $willBeAvailable)
{
$doctrineCache = $willBeAvailable('doctrine/cache', Cache::class, 'doctrine/annotation');
$psr6Cache = $willBeAvailable('symfony/cache', PsrCachedReader::class, 'doctrine/annotation');

$rootNode
->children()
->arrayNode('annotations')
->info('annotation configuration')
->{$willBeAvailable('doctrine/annotations', Annotation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
->children()
->scalarNode('cache')->defaultValue($willBeAvailable('doctrine/cache', Cache::class, 'doctrine/annotation') ? 'php_array' : 'none')->end()
->scalarNode('cache')->defaultValue(($doctrineCache || $psr6Cache) ? 'php_array' : 'none')->end()
->scalarNode('file_cache_dir')->defaultValue('%kernel.cache_dir%/annotations')->end()
->booleanNode('debug')->defaultValue($this->debug)->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1481,13 +1481,50 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
->setMethodCalls([['registerLoader', ['class_exists']]]);
}

if ('none' !== $config['cache']) {
if ('none' === $config['cache']) {
$container->removeDefinition('annotations.cached_reader');
$container->removeDefinition('annotations.psr_cached_reader');

return;
}

$cacheService = $config['cache'];
if (\in_array($config['cache'], ['php_array', 'file'])) {
$isPsr6Service = $container->hasDefinition('annotations.psr_cached_reader');
} else {
$isPsr6Service = false;
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.');
}

if ($isPsr6Service) {
$container->removeDefinition('annotations.cached_reader');
$container->setDefinition('annotations.cached_reader', $container->getDefinition('annotations.psr_cached_reader'));

if ('php_array' === $config['cache']) {
$cacheService = 'annotations.psr_cache';

// Enable warmer only if PHP array is used for cache
$definition = $container->findDefinition('annotations.cache_warmer');
$definition->addTag('kernel.cache_warmer');
} elseif ('file' === $config['cache']) {
$cacheService = 'annotations.filesystem_cache_adapter';
$cacheDir = $container->getParameterBag()->resolveValue($config['file_cache_dir']);

if (!is_dir($cacheDir) && false === @mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir));
}

$container
->getDefinition('annotations.filesystem_cache_adapter')
->replaceArgument(2, $cacheDir)
;
}
} else {
// Legacy code for doctrine/annotations:<1.13
if (!class_exists(\Doctrine\Common\Cache\CacheProvider::class)) {
throw new LogicException('Annotations cannot be cached as the Doctrine Cache library is not installed. Try running "composer require doctrine/cache".');
}

$cacheService = $config['cache'];

if ('php_array' === $config['cache']) {
$cacheService = 'annotations.cache';

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

$cacheService = 'annotations.filesystem_cache';
}
}

$container
->getDefinition('annotations.cached_reader')
->replaceArgument(2, $config['debug'])
// temporary property to lazy-reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
->setProperty('cacheProviderBackup', new ServiceClosureArgument(new Reference($cacheService)))
->addTag('annotations.cached_reader')
;
$container
->getDefinition('annotations.cached_reader')
->replaceArgument(2, $config['debug'])
// temporary property to lazy-reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
->setProperty('cacheProviderBackup', new ServiceClosureArgument(new Reference($cacheService)))
->addTag('annotations.cached_reader')
;

$container->setAlias('annotation_reader', 'annotations.cached_reader');
$container->setAlias(Reader::class, new Alias('annotations.cached_reader', false));
} else {
$container->removeDefinition('annotations.cached_reader');
}
$container->setAlias('annotation_reader', 'annotations.cached_reader');
$container->setAlias(Reader::class, new Alias('annotations.cached_reader', false));
$container->removeDefinition('annotations.psr_cached_reader');
}

private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Annotations\Reader;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand All @@ -36,7 +37,7 @@
->args([
service('annotations.reader'),
inline_service(DoctrineProvider::class)->args([
inline_service(ArrayAdapter::class)
inline_service(ArrayAdapter::class),
]),
abstract_arg('Debug-Flag'),
])
Expand Down Expand Up @@ -74,4 +75,22 @@

->alias('annotation_reader', 'annotations.reader')
->alias(Reader::class, 'annotation_reader');

if (class_exists(PsrCachedReader::class)) {
$container->services()
->set('annotations.psr_cached_reader', PsrCachedReader::class)
->args([
service('annotations.reader'),
inline_service(ArrayAdapter::class),
abstract_arg('Debug-Flag'),
])
->set('annotations.psr_cache', PhpArrayAdapter::class)
->factory([PhpArrayAdapter::class, 'create'])
->args([
param('kernel.cache_dir').'/annotations.php',
service('cache.annotations'),
])
->tag('container.hot_path')
;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;

use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\PsrCachedReader;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
Expand Down Expand Up @@ -578,7 +579,7 @@ public function testNullSessionHandler()

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

/**
Expand All @@ -597,7 +598,7 @@ public function testNullSessionHandlerLegacy()

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

public function testRequest()
Expand Down Expand Up @@ -980,7 +981,7 @@ public function testAnnotations()
$container->compile();

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

public function testFileLinkFormat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
Expand All @@ -33,7 +34,7 @@ public function testCachedAnnotationReaderAutowiring()
static::bootKernel();

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

public function testEventDispatcherAutowiring()
Expand Down