1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Entity\Annotation; |
6
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Annotation\Entity as EntityAnnotation; |
8
|
|
|
use AmaTeam\ElasticSearch\API\Entity\EntityInterface; |
9
|
|
|
use AmaTeam\ElasticSearch\API\Entity\LoaderInterface; |
10
|
|
|
use AmaTeam\ElasticSearch\Entity\Entity; |
11
|
|
|
use AmaTeam\ElasticSearch\Utility\Classes; |
12
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
use ReflectionProperty; |
15
|
|
|
|
16
|
|
|
class Loader implements LoaderInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var AnnotationReader |
20
|
|
|
*/ |
21
|
|
|
private $annotationReader; |
22
|
|
|
/** |
23
|
|
|
* @var ClassAnnotationListenerInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $classListeners = []; |
26
|
|
|
/** |
27
|
|
|
* @var PropertyAnnotationListenerInterface[] |
28
|
|
|
*/ |
29
|
|
|
private $propertyListeners = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param AnnotationReader $annotationReader |
33
|
|
|
*/ |
34
|
|
|
public function __construct(AnnotationReader $annotationReader = null) |
35
|
|
|
{ |
36
|
|
|
$this->annotationReader = $annotationReader ?: new AnnotationReader(); |
37
|
|
|
$this->registerLoaders(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $className |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function exists(string $className): bool |
45
|
|
|
{ |
46
|
|
|
if (!class_exists($className)) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
$reflection = new ReflectionClass($className); |
50
|
|
|
return (bool) $this->annotationReader->getClassAnnotation($reflection, EntityAnnotation::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function load(string $className): ?EntityInterface |
54
|
|
|
{ |
55
|
|
|
$className = Classes::normalizeAbsoluteName($className); |
56
|
|
|
if (!$this->exists($className)) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
$entity = new Entity($className); |
60
|
|
|
$reflection = new ReflectionClass($className); |
61
|
|
|
foreach ($this->annotationReader->getClassAnnotations($reflection) as $annotation) { |
62
|
|
|
foreach ($this->classListeners as $listener) { |
63
|
|
|
$listener->accept($reflection, $entity, $annotation); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
foreach ($reflection->getProperties(~ReflectionProperty::IS_STATIC) as $property) { |
67
|
|
|
if ($property->getDeclaringClass()->getName() !== $reflection->getName()) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
foreach ($this->annotationReader->getPropertyAnnotations($property) as $annotation) { |
71
|
|
|
foreach ($this->propertyListeners as $listener) { |
72
|
|
|
$listener->accept($property, $entity, $annotation); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return $entity; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function registerLoaders() |
80
|
|
|
{ |
81
|
|
|
$this->classListeners = ListenerProvider::getClassListeners(); |
82
|
|
|
$this->propertyListeners = ListenerProvider::getPropertyListeners(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|