|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Entity; |
|
6
|
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Entity\EntityInterface; |
|
8
|
|
|
use AmaTeam\ElasticSearch\API\Entity\LoaderInterface; |
|
9
|
|
|
use AmaTeam\ElasticSearch\API\Entity\ProviderInterface; |
|
10
|
|
|
use AmaTeam\ElasticSearch\Entity\Annotation\Loader; |
|
11
|
|
|
use AmaTeam\ElasticSearch\Entity\Inheritance\Resolver; |
|
12
|
|
|
use BadMethodCallException; |
|
13
|
|
|
|
|
14
|
|
|
class Provider implements ProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var LoaderInterface[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $loaders = []; |
|
20
|
|
|
|
|
21
|
|
|
public function get(string $name): EntityInterface |
|
22
|
|
|
{ |
|
23
|
|
|
$entity = $this->find($name); |
|
24
|
|
|
if ($entity) { |
|
25
|
|
|
return $entity; |
|
26
|
|
|
} |
|
27
|
|
|
throw new BadMethodCallException("Can't get entity for class `$name`"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function find(string $name): ?EntityInterface |
|
31
|
|
|
{ |
|
32
|
|
|
foreach ($this->loaders as $loader) { |
|
33
|
|
|
$entity = $loader->load($name); |
|
34
|
|
|
if ($entity) { |
|
35
|
|
|
return $entity; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function exists(string $name): bool |
|
42
|
|
|
{ |
|
43
|
|
|
foreach ($this->loaders as $loader) { |
|
44
|
|
|
if ($loader->exists($name)) { |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritDoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getHierarchy(string $name): array |
|
55
|
|
|
{ |
|
56
|
|
|
$hierarchy = $this->findHierarchy($name); |
|
57
|
|
|
if ($hierarchy !== null) { |
|
58
|
|
|
return $hierarchy; |
|
59
|
|
|
} |
|
60
|
|
|
throw new BadMethodCallException("Can't get entity for class `$name`"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function findHierarchy(string $name): ?array |
|
67
|
|
|
{ |
|
68
|
|
|
$tip = $this->find($name); |
|
69
|
|
|
if (!$tip) { |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
$cursor = $tip; |
|
73
|
|
|
$hierarchy = []; |
|
74
|
|
|
while ($cursor !== null) { |
|
75
|
|
|
array_unshift($hierarchy, $cursor); |
|
76
|
|
|
$parent = $cursor->getParentName(); |
|
77
|
|
|
$cursor = $parent ? $this->find($parent) : null; |
|
78
|
|
|
} |
|
79
|
|
|
return $hierarchy; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritDoc |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getResolved(string $name): EntityInterface |
|
86
|
|
|
{ |
|
87
|
|
|
return Resolver::resolve(...$this->getHierarchy($name)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function findResolved(string $name): ?EntityInterface |
|
91
|
|
|
{ |
|
92
|
|
|
$hierarchy = $this->findHierarchy($name); |
|
93
|
|
|
return $hierarchy ? Resolver::resolve(...$hierarchy) : null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function registerLoader(LoaderInterface $loader): ProviderInterface |
|
97
|
|
|
{ |
|
98
|
|
|
if (!in_array($loader, $this->loaders)) { |
|
99
|
|
|
$this->loaders[] = $loader; |
|
100
|
|
|
} |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function deregisterLoader(LoaderInterface $loader): ProviderInterface |
|
105
|
|
|
{ |
|
106
|
|
|
$this->loaders = array_filter($this->loaders, function ($item) use ($loader) { |
|
107
|
|
|
return $item !== $loader; |
|
108
|
|
|
}); |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public static function createDefault(): Provider |
|
113
|
|
|
{ |
|
114
|
|
|
$provider = new static(); |
|
115
|
|
|
$provider->registerLoader(new Loader()); |
|
116
|
|
|
return $provider; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|