1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2021, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/hydrator |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sunrise\Hydrator\AnnotationReader; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
17
|
|
|
use Doctrine\Common\Annotations\Reader; |
18
|
|
|
use Generator; |
19
|
|
|
use LogicException; |
20
|
|
|
use ReflectionProperty; |
21
|
|
|
use Sunrise\Hydrator\AnnotationReaderInterface; |
22
|
|
|
|
23
|
|
|
use function class_exists; |
24
|
|
|
use function sprintf; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @link https://github.com/doctrine/annotations |
28
|
|
|
* |
29
|
|
|
* @since 3.1.0 |
30
|
|
|
*/ |
31
|
|
|
final class DoctrineAnnotationReader implements AnnotationReaderInterface |
32
|
|
|
{ |
33
|
|
|
private Reader $reader; |
34
|
|
|
|
35
|
|
|
public function __construct(Reader $reader) |
36
|
|
|
{ |
37
|
|
|
$this->reader = $reader; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws LogicException If the doctrine/annotations package isn't installed on the server. |
42
|
|
|
*/ |
43
|
|
|
public static function default(): self |
44
|
|
|
{ |
45
|
|
|
// @codeCoverageIgnoreStart |
46
|
|
|
if (!class_exists(AnnotationReader::class)) { |
47
|
|
|
throw new LogicException(sprintf( |
48
|
|
|
'The annotation reader {%s} requires the doctrine/annotations package, ' . |
49
|
|
|
'run the command `composer require doctrine/annotations` to resolve it.', |
50
|
|
|
__CLASS__, |
51
|
|
|
)); |
52
|
|
|
} // @codeCoverageIgnoreEnd |
53
|
|
|
|
54
|
|
|
return new self(new AnnotationReader()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function getAnnotations(string $name, $holder): Generator |
61
|
|
|
{ |
62
|
|
|
if ($holder instanceof ReflectionProperty) { |
63
|
|
|
$annotations = $this->reader->getPropertyAnnotations($holder); |
64
|
|
|
foreach ($annotations as $annotation) { |
65
|
|
|
if ($annotation instanceof $name) { |
66
|
|
|
yield $annotation; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|