-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
SimpleModelFactory.php
59 lines (46 loc) · 1.45 KB
/
SimpleModelFactory.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
<?php declare(strict_types = 1);
namespace Nextras\Orm\Model;
use Nette\Caching\Cache;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\Reflection\IMetadataParserFactory;
use Nextras\Orm\Entity\Reflection\MetadataParserFactory;
use Nextras\Orm\Repository\IRepository;
use function array_values;
class SimpleModelFactory
{
/** @var Cache */
private $cache;
/**
* @var IRepository[]
* @phpstan-var array<string, IRepository<IEntity>>
*/
private $repositories;
/** @var IMetadataParserFactory|null */
private $metadataParserFactory;
/**
* @param array<string, IRepository> $repositories
* @template E of \Nextras\Orm\Entity\IEntity
* @phpstan-param array<string, IRepository<E>> $repositories
*/
public function __construct(Cache $cache, array $repositories, IMetadataParserFactory $metadataParserFactory = null)
{
$this->cache = $cache;
$this->repositories = $repositories;
$this->metadataParserFactory = $metadataParserFactory;
}
/**
* @return Model
*/
public function create()
{
$config = Model::getConfiguration($this->repositories);
$parser = $this->metadataParserFactory ?? new MetadataParserFactory();
$loader = new SimpleRepositoryLoader(array_values($this->repositories));
$metadata = new MetadataStorage($config[2], $this->cache, $parser, $loader);
$model = new Model($config, $loader, $metadata);
foreach ($this->repositories as $repository) {
$repository->setModel($model);
}
return $model;
}
}