• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nextras / orm / 15216031117

23 May 2025 05:36PM UTC coverage: 91.709% (-0.02%) from 91.73%
15216031117

push

github

web-flow
Merge pull request #744 from nextras/nette-di-lazy

Force initialization for Nette DI's lazy mode [closes #743]

0 of 2 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

4115 of 4487 relevant lines covered (91.71%)

4.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/src/Model/MetadataStorage.php
1
<?php declare(strict_types = 1);
2

3
namespace Nextras\Orm\Model;
4

5

6
use Nette\Caching\Cache;
7
use Nextras\Orm\Entity\Embeddable\EmbeddableContainer;
8
use Nextras\Orm\Entity\IEntity;
9
use Nextras\Orm\Entity\Reflection\EntityMetadata;
10
use Nextras\Orm\Entity\Reflection\IMetadataParserFactory;
11
use Nextras\Orm\Entity\Reflection\MetadataValidator;
12
use Nextras\Orm\Exception\InvalidArgumentException;
13
use Nextras\Orm\Exception\InvalidStateException;
14
use Nextras\Orm\Repository\IRepository;
15
use function array_keys;
16
use function array_shift;
17
use function assert;
18
use function key;
19

20

21
class MetadataStorage
22
{
23
        /** @var array<class-string<mixed>, EntityMetadata> */
24
        private static array $metadata = [];
25

26

27
        public static function get(string $className): EntityMetadata
28
        {
29
                if (!isset(self::$metadata[$className])) {
5✔
30
                        if (self::$metadata === []) {
5✔
31
                                throw new InvalidStateException("MetadataStorage::get() called too early. You have to instantiate your model first.");
5✔
32
                        }
33
                        throw new InvalidArgumentException("Entity metadata for '{$className}' does not exist.");
5✔
34
                }
35
                return self::$metadata[$className];
5✔
36
        }
37

38

39
        /**
40
         * @param array<class-string<IEntity>, class-string<IRepository<IEntity>>> $entityClassesMap
41
         */
42
        public function __construct(
43
                array $entityClassesMap,
44
                Cache $cache,
45
                IMetadataParserFactory $metadataParserFactory,
46
                IRepositoryLoader $repositoryLoader
47
        )
48
        {
49
                $metadata = $cache->derive('orm.metadata')->load(
5✔
50
                        $entityClassesMap,
51
                        function (&$dp) use ($entityClassesMap, $metadataParserFactory, $repositoryLoader): array {
5✔
52
                                /** @var EntityMetadata[] $metadata */
53
                                $metadata = [];
5✔
54
                                $toProcess = array_keys($entityClassesMap);
5✔
55
                                $annotationParser = $metadataParserFactory->create($entityClassesMap);
5✔
56

57
                                while (($className = array_shift($toProcess)) !== null) {
5✔
58
                                        $metadata[$className] = $annotationParser->parseMetadata($className, $dp[Cache::Files]);
5✔
59
                                        foreach ($metadata[$className]->getProperties() as $property) {
5✔
60
                                                if ($property->wrapper === EmbeddableContainer::class) {
5✔
61
                                                        assert($property->args !== null);
62
                                                        $toProcess[] = $property->args[EmbeddableContainer::class]['class'];
5✔
63
                                                }
64
                                        }
65
                                }
66

67
                                $validator = new MetadataValidator();
5✔
68
                                $validator->validate($metadata, $repositoryLoader);
5✔
69

70
                                return $metadata;
5✔
71
                        }
5✔
72
                );
73

74
                /** @var EntityMetadata[] $toProcess */
75
                $toProcess = $metadata;
5✔
76
                while (($entityMetadata = array_shift($toProcess)) !== null) {
5✔
77
                        foreach ($entityMetadata->getProperties() as $property) {
5✔
78
                                if ($property->relationship) {
5✔
79
                                        $property->relationship->entityMetadata = $metadata[$property->relationship->entity];
5✔
80
                                }
81
                                if ($property->wrapper === EmbeddableContainer::class) {
5✔
82
                                        $type = key($property->types);
5✔
83
                                        $property->args[EmbeddableContainer::class]['metadata'] = $metadata[$type];
5✔
84
                                        $toProcess[] = $metadata[$type];
5✔
85
                                }
86
                        }
87
                }
88

89
                self::$metadata += $metadata;
5✔
90
        }
5✔
91

92

93
        /**
94
         * Returns all Nextras' metadata loaded in runtime until now.
95
         *
96
         * In other words, two independent ORM instances share the static storage, and the data
97
         * returned in this method are only those that have been already loaded.
98
         *
99
         * @return array<class-string<mixed>, EntityMetadata>
100
         */
101
        public function getLoadedMetadata(): array
102
        {
NEW
103
                return self::$metadata;
×
104
        }
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc