-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataRepository.php
More file actions
108 lines (86 loc) · 2.83 KB
/
MetadataRepository.php
File metadata and controls
108 lines (86 loc) · 2.83 KB
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
<?php
/*
* This file is part of the enhavo package.
*
* (c) WE ARE INDEED GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Enhavo\Component\Metadata;
use Doctrine\Persistence\Proxy;
use Enhavo\Component\Metadata\Exception\InvalidMetadataException;
class MetadataRepository
{
private array $metadata = [];
public function __construct(
private readonly MetadataFactory $factory,
private readonly bool $includeExtend = true,
private readonly bool $onlyExists = true,
) {
}
public function getAllMetadata(): array
{
$classes = $this->factory->getAllClasses();
foreach ($classes as $class) {
$this->getMetadata($class);
}
return $this->metadata;
}
public function getMetadata(string|object $class): ?Metadata
{
$className = $this->getClassName($class);
if (array_key_exists($className, $this->metadata)) {
return $this->metadata[$className];
}
$loaded = [];
$metadata = $this->factory->createMetadata($className, $this->includeExtend || !$this->onlyExists);
if ($this->includeExtend) {
$parents = [];
$this->getParents($className, $parents);
$parents = array_reverse($parents);
foreach ($parents as $parent) {
$loaded[] = $this->factory->loadMetadata($parent, $metadata);
}
} elseif (null === $metadata) {
return null;
}
$loaded[] = $this->factory->loadMetadata($className, $metadata);
if ($this->onlyExists && $this->includeExtend && !in_array(true, $loaded)) {
return null;
}
$this->metadata[$className] = $metadata;
return $metadata;
}
private function getParents($className, array &$parents): void
{
if (!class_exists($className)) {
throw InvalidMetadataException::classNotExists($className);
}
$parentClass = get_parent_class($className);
if (false !== $parentClass) {
$parents[] = $parentClass;
$this->getParents($parentClass, $parents);
}
}
public function hasMetadata($class): bool
{
$metadata = $this->getMetadata($class);
return null !== $metadata;
}
private function getClassName($class): false|string
{
if (is_string($class)) {
$className = $class;
} elseif (is_object($class)) {
if ($class instanceof Proxy) {
$className = get_parent_class($class);
} else {
$className = get_class($class);
}
} else {
throw InvalidMetadataException::invalidType($class);
}
return $className;
}
}