forked from nextras/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataStorage.php
More file actions
70 lines (58 loc) · 1.96 KB
/
MetadataStorage.php
File metadata and controls
70 lines (58 loc) · 1.96 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
<?php declare(strict_types = 1);
/**
* This file is part of the Nextras\Orm library.
* @license MIT
* @link https://github.com/nextras/orm
*/
namespace Nextras\Orm\Model;
use Nette\Caching\Cache;
use Nextras\Orm\Entity\Reflection\EntityMetadata;
use Nextras\Orm\Entity\Reflection\IMetadataParserFactory;
use Nextras\Orm\Entity\Reflection\MetadataValidator;
use Nextras\Orm\InvalidArgumentException;
use Nextras\Orm\InvalidStateException;
class MetadataStorage
{
/** @var EntityMetadata[] */
private static $metadata = [];
public static function get(string $className): EntityMetadata
{
if (!isset(static::$metadata[$className])) {
if (static::$metadata === []) {
throw new InvalidStateException("MetadataStorage::get() called too early. You have to instantiate your model first.");
}
throw new InvalidArgumentException("Entity metadata for '{$className}' does not exist.");
}
return static::$metadata[$className];
}
public function __construct(
array $entityClassesMap,
Cache $cache,
IMetadataParserFactory $metadataParserFactory,
IRepositoryLoader $repositoryLoader
)
{
$metadata = $cache->derive('metadata')->load(
$entityClassesMap,
function (& $dp) use ($entityClassesMap, $metadataParserFactory, $repositoryLoader) {
$metadata = [];
$annotationParser = $metadataParserFactory->create($entityClassesMap);
foreach (array_keys($entityClassesMap) as $className) {
$metadata[$className] = $annotationParser->parseMetadata($className, $dp[Cache::FILES]);
}
$validator = new MetadataValidator();
$validator->validate($metadata, $repositoryLoader);
return $metadata;
}
);
/** @var EntityMetadata $entityMetadata */
foreach ($metadata as $entityMetadata) {
foreach ($entityMetadata->getProperties() as $property) {
if ($property->relationship) {
$property->relationship->entityMetadata = $metadata[$property->relationship->entity];
}
}
}
static::$metadata += $metadata;
}
}