AnnotationReaderProvider.php
33 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Doctrine\Annotations; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\PSRArrayCache; |
| 9 | use MailPoetVendor\Doctrine\Common\Annotations\AnnotationReader; |
| 10 | use MailPoetVendor\Doctrine\Common\Annotations\AnnotationRegistry; |
| 11 | use MailPoetVendor\Doctrine\Common\Annotations\PsrCachedReader; |
| 12 | |
| 13 | class AnnotationReaderProvider { |
| 14 | /** @var PsrCachedReader */ |
| 15 | private $annotationReader; |
| 16 | |
| 17 | public function __construct() { |
| 18 | // register annotation reader if doctrine/annotations package is installed |
| 19 | // (i.e. in dev environment, on production metadata is dumped in the build) |
| 20 | $readAnnotations = class_exists(PsrCachedReader::class) && class_exists(AnnotationReader::class); |
| 21 | if ($readAnnotations) { |
| 22 | // autoload all annotation classes using registered loaders (Composer) |
| 23 | // (needed for Symfony\Validator constraint annotations to be loaded) |
| 24 | AnnotationRegistry::registerLoader('class_exists'); |
| 25 | $this->annotationReader = new PsrCachedReader(new AnnotationReader(), new PSRArrayCache()); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public function getAnnotationReader(): ?PsrCachedReader { |
| 30 | return $this->annotationReader; |
| 31 | } |
| 32 | } |
| 33 |