-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetadataFactory.php
139 lines (113 loc) · 4.03 KB
/
MetadataFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Metadata;
use Klipper\Component\Metadata\Exception\ObjectMetadataNotFoundException;
use Klipper\Component\Metadata\Loader\ChoiceNameCollection;
use Klipper\Component\Metadata\Loader\ObjectMetadataNameCollection;
use Klipper\Component\Resource\Domain\DomainInterface;
use Klipper\Component\Resource\Domain\DomainManagerInterface;
use Klipper\Component\Resource\Exception\InvalidArgumentException;
/**
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class MetadataFactory implements MetadataFactoryInterface
{
protected MetadataRegistryInterface $register;
protected DomainManagerInterface $domainManager;
protected ?ChoiceNameCollection $cacheChoiceNames = null;
/**
* @param MetadataRegistryInterface $register The metadata register
* @param DomainManagerInterface $domainManager The resource domain manager
*/
public function __construct(
MetadataRegistryInterface $register,
DomainManagerInterface $domainManager
) {
$this->register = $register;
$this->domainManager = $domainManager;
}
public function getManagedClasses(): ObjectMetadataNameCollection
{
return $this->register->getNames();
}
public function isManagedClass(string $class): bool
{
return $this->domainManager->has($class);
}
public function isManagedByName(string $name): bool
{
$names = $this->getManagedClasses()->all();
return isset($names[$name]);
}
public function getManagedClass(string $class): string
{
return $this->getManagedDomain($class)->getClass();
}
public function create(string $class): ObjectMetadataInterface
{
$domain = $this->getManagedDomain($class);
return $this->getBuilder($domain->getClass())->build();
}
public function createChoice(string $name): ChoiceInterface
{
$builder = $this->register->getChoice($name);
// force to init all choices
if (null === $builder && null === $this->cacheChoiceNames) {
$this->getChoiceNames();
$builder = $this->register->getChoice($name);
}
return ($builder ?? new ChoiceBuilder($name, null, []))->build();
}
public function getChoiceNames(): ChoiceNameCollection
{
if (null === $this->cacheChoiceNames) {
foreach ($this->getManagedClasses()->all() as $class) {
$this->getBuilder($class);
}
$this->cacheChoiceNames = $this->register->getChoiceNames();
}
return $this->cacheChoiceNames;
}
public function isChoiceManaged(string $name): bool
{
$names = $this->getChoiceNames()->all();
return \in_array($name, $names, true);
}
/**
* Get the object metadata builder.
*
* @param string $class The class name
*/
protected function getBuilder(string $class): ObjectMetadataBuilderInterface
{
$builder = $this->register->getBuilder($class);
return $builder ?? $this->register->guessConfig(new ObjectMetadataBuilder($class));
}
/**
* Get the managed resource domain.
*
* @param string $class The class name
*
* @throws ObjectMetadataNotFoundException When the class is not registered in doctrine
*/
private function getManagedDomain(string $class): DomainInterface
{
try {
$names = $this->getManagedClasses()->all();
$class = $names[$class] ?? $class;
$domain = $this->domainManager->get($class);
} catch (InvalidArgumentException $e) {
throw new ObjectMetadataNotFoundException($class);
} catch (\ReflectionException $e) {
throw new ObjectMetadataNotFoundException($class);
}
return $domain;
}
}