From 7e0b52e5addec5c530ce9dfc250ff3a4954c9a73 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 1 Feb 2021 20:35:09 +0100 Subject: [PATCH 1/6] Add annotation reader cache --- src/Config/Parser/AnnotationParser.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index f988d5411..ada3487ea 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -6,18 +6,23 @@ use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\AnnotationRegistry; +use Doctrine\Common\Annotations\CachedReader; +use Doctrine\Common\Annotations\Reader; +use Doctrine\Common\Cache\ApcuCache; +use Doctrine\Common\Cache\PhpFileCache; use Overblog\GraphQLBundle\Config\Parser\MetadataParser\MetadataParser; use ReflectionClass; use ReflectionMethod; use ReflectionProperty; use Reflector; use RuntimeException; +use function apcu_enabled; class AnnotationParser extends MetadataParser { const METADATA_FORMAT = '@%s'; - protected static ?AnnotationReader $annotationReader = null; + protected static ?Reader $annotationReader = null; protected static function getMetadatas(Reflector $reflector): array { @@ -32,18 +37,28 @@ protected static function getMetadatas(Reflector $reflector): array return []; } - protected static function getAnnotationReader(): AnnotationReader + protected static function getAnnotationReader(): Reader { if (null === self::$annotationReader) { if (!class_exists(AnnotationReader::class) || !class_exists(AnnotationRegistry::class)) { // @codeCoverageIgnoreStart - throw new RuntimeException('In order to use graphql annotation, you need to require doctrine annotations'); + throw new RuntimeException('In order to use graphql annotations, you need to require doctrine annotations'); // @codeCoverageIgnoreEnd } AnnotationRegistry::registerLoader('class_exists'); - self::$annotationReader = new AnnotationReader(); + + // @codeCoverageIgnoreStart + if (extension_loaded('apcu') && apcu_enabled()) { + $annotationCache = new ApcuCache(); + } else { + $annotationCache = new PhpFileCache(sys_get_temp_dir().__DIR__); + } + $annotationCache->setNamespace(__DIR__); + // @codeCoverageIgnoreEnd + + self::$annotationReader = new CachedReader(new AnnotationReader(), $annotationCache, true); } return self::$annotationReader; From ce6b73a0dd0f83ad15fe3e3a0b47ed10ed8f0343 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 2 Feb 2021 10:09:08 +0100 Subject: [PATCH 2/6] Fix annotation cache key/namespace --- src/Config/Parser/AnnotationParser.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index ada3487ea..dbda983ea 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -48,15 +48,15 @@ protected static function getAnnotationReader(): Reader } AnnotationRegistry::registerLoader('class_exists'); - + $cacheKey = md5(__DIR__); // @codeCoverageIgnoreStart if (extension_loaded('apcu') && apcu_enabled()) { $annotationCache = new ApcuCache(); } else { - $annotationCache = new PhpFileCache(sys_get_temp_dir().__DIR__); + $annotationCache = new PhpFileCache(sys_get_temp_dir().$cacheKey); } - $annotationCache->setNamespace(__DIR__); // @codeCoverageIgnoreEnd + $annotationCache->setNamespace($cacheKey); self::$annotationReader = new CachedReader(new AnnotationReader(), $annotationCache, true); } From d58be758ecd043864f2743bf46acc0db65359784 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 2 Feb 2021 10:20:22 +0100 Subject: [PATCH 3/6] Fix cache path --- src/Config/Parser/AnnotationParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index dbda983ea..8377da842 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -53,7 +53,7 @@ protected static function getAnnotationReader(): Reader if (extension_loaded('apcu') && apcu_enabled()) { $annotationCache = new ApcuCache(); } else { - $annotationCache = new PhpFileCache(sys_get_temp_dir().$cacheKey); + $annotationCache = new PhpFileCache(join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $cacheKey])); } // @codeCoverageIgnoreEnd $annotationCache->setNamespace($cacheKey); From 78e63b24ab6521b86e8fb5196c40b31c2a0f57e4 Mon Sep 17 00:00:00 2001 From: Timur Murtukov Date: Fri, 23 Apr 2021 17:06:56 +0200 Subject: [PATCH 4/6] Review code * Import functions * Make property non-nullable * Edit exception message --- src/Config/Parser/AnnotationParser.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index 8377da842..11cfa9ba5 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -17,12 +17,16 @@ use Reflector; use RuntimeException; use function apcu_enabled; +use function class_exists; +use function extension_loaded; +use function md5; +use function sys_get_temp_dir; class AnnotationParser extends MetadataParser { const METADATA_FORMAT = '@%s'; - protected static ?Reader $annotationReader = null; + protected static Reader $annotationReader; protected static function getMetadatas(Reflector $reflector): array { @@ -39,11 +43,10 @@ protected static function getMetadatas(Reflector $reflector): array protected static function getAnnotationReader(): Reader { - if (null === self::$annotationReader) { - if (!class_exists(AnnotationReader::class) || - !class_exists(AnnotationRegistry::class)) { + if (!isset(self::$annotationReader)) { + if (!class_exists(AnnotationReader::class) || !class_exists(AnnotationRegistry::class)) { // @codeCoverageIgnoreStart - throw new RuntimeException('In order to use graphql annotations, you need to require doctrine annotations'); + throw new RuntimeException("In order to use annotations, you need to install 'doctrine/annotations' first. See: 'https://www.doctrine-project.org/projects/annotations.html'"); // @codeCoverageIgnoreEnd } From 3e37a3f04fc02f5a8c34df1fc6bc77b54e5b0c6e Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 20 May 2021 21:09:11 +0200 Subject: [PATCH 5/6] Update AnnotationParser.php --- src/Config/Parser/AnnotationParser.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index 5232cda99..ff43d56c0 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -21,6 +21,7 @@ use function extension_loaded; use function md5; use function sys_get_temp_dir; +use function implode; class AnnotationParser extends MetadataParser { @@ -56,7 +57,7 @@ protected static function getAnnotationReader(): Reader if (extension_loaded('apcu') && apcu_enabled()) { $annotationCache = new ApcuCache(); } else { - $annotationCache = new PhpFileCache(join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $cacheKey])); + $annotationCache = new PhpFileCache(implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $cacheKey])); } // @codeCoverageIgnoreEnd $annotationCache->setNamespace($cacheKey); From 45ef65bbbc049fdd2becfd813d138017679f42f7 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 20 May 2021 21:13:02 +0200 Subject: [PATCH 6/6] Update AnnotationParser.php --- src/Config/Parser/AnnotationParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index ff43d56c0..f96c6e45e 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -19,9 +19,9 @@ use function apcu_enabled; use function class_exists; use function extension_loaded; +use function implode; use function md5; use function sys_get_temp_dir; -use function implode; class AnnotationParser extends MetadataParser {