/
PhpDocRepositoryFinder.php
154 lines (122 loc) · 4.33 KB
/
PhpDocRepositoryFinder.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php declare(strict_types = 1);
namespace Nextras\Orm\Bridges\NetteDI;
use Nette\DI\ContainerBuilder;
use Nette\Utils\Reflection;
use Nextras\Orm\Exception\InvalidStateException;
use Nextras\Orm\Exception\RuntimeException;
use Nextras\Orm\Model\Model;
use Nextras\Orm\Repository\IRepository;
use ReflectionClass;
class PhpDocRepositoryFinder implements IRepositoryFinder
{
/**
* @var string
* @phpstan-var class-string<\Nextras\Orm\Model\IModel>
*/
protected $modelClass;
/** @var ContainerBuilder */
protected $builder;
/** @var OrmExtension */
protected $extension;
public function __construct(string $modelClass, ContainerBuilder $containerBuilder, OrmExtension $extension)
{
$this->modelClass = $modelClass;
$this->builder = $containerBuilder;
$this->extension = $extension;
}
public function loadConfiguration(): ?array
{
$repositories = $this->findRepositories($this->modelClass);
$repositoriesMap = [];
foreach ($repositories as $repositoryName => $repositoryClass) {
$this->setupMapperService($repositoryName, $repositoryClass);
$this->setupRepositoryService($repositoryName, $repositoryClass);
$repositoriesMap[$repositoryClass] = $this->extension->prefix('repositories.' . $repositoryName);
}
$this->setupRepositoryLoader($repositoriesMap);
return $repositories;
}
public function beforeCompile(): ?array
{
return null;
}
/**
* @return array<string, string>
* @phpstan-param class-string<\Nextras\Orm\Model\IModel> $modelClass
* @phpstan-return array<string, class-string<IRepository<\Nextras\Orm\Entity\IEntity>>>
*/
protected function findRepositories(string $modelClass): array
{
if ($modelClass === Model::class) {
throw new InvalidStateException('Your model has to inherit from ' . Model::class . '. Use compiler extension configuration - model key.');
}
$modelReflection = new ReflectionClass($modelClass);
$classFileName = $modelReflection->getFileName();
assert($classFileName !== false);
$this->builder->addDependency($classFileName);
$repositories = [];
preg_match_all(
'~^ [ \t*]* @property(?:|-read) [ \t]+ ([^\s$]+) [ \t]+ \$ (\w+) ()~mx',
(string) $modelReflection->getDocComment(), $matches, PREG_SET_ORDER
);
/**
* @var string $type
* @var string $name
*/
foreach ($matches as [, $type, $name]) {
/** @phpstan-var class-string<IRepository<\Nextras\Orm\Entity\IEntity>> $type */
$type = Reflection::expandClassName($type, $modelReflection);
if (!class_exists($type)) {
throw new RuntimeException("Repository '{$type}' does not exist.");
}
$rc = new ReflectionClass($type);
assert($rc->implementsInterface(IRepository::class), sprintf(
'Property "%s" of class "%s" with type "%s" does not implement interface %s.',
$modelClass, $name, $type, IRepository::class
));
$repositories[$name] = $type;
}
return $repositories;
}
protected function setupMapperService(string $repositoryName, string $repositoryClass): void
{
$mapperName = $this->extension->prefix('mappers.' . $repositoryName);
if ($this->builder->hasDefinition($mapperName)) {
return;
}
$mapperClass = str_replace('Repository', 'Mapper', $repositoryClass);
if (!class_exists($mapperClass)) {
throw new InvalidStateException("Unknown mapper for '{$repositoryName}' repository.");
}
$this->builder->addDefinition($mapperName)
->setType($mapperClass)
->setArguments([
'cache' => $this->extension->prefix('@cache'),
]);
}
protected function setupRepositoryService(string $repositoryName, string $repositoryClass): void
{
$serviceName = $this->extension->prefix('repositories.' . $repositoryName);
if ($this->builder->hasDefinition($serviceName)) {
return;
}
$this->builder->addDefinition($serviceName)
->setType($repositoryClass)
->setArguments([
$this->extension->prefix('@mappers.' . $repositoryName),
$this->extension->prefix('@dependencyProvider'),
])
->addSetup('setModel', [$this->extension->prefix('@model')]);
}
/**
* @param array<class-string<IRepository<\Nextras\Orm\Entity\IEntity>>, string> $repositoriesMap
*/
protected function setupRepositoryLoader(array $repositoriesMap): void
{
$this->builder->addDefinition($this->extension->prefix('repositoryLoader'))
->setType(RepositoryLoader::class)
->setArguments([
'repositoryNamesMap' => $repositoriesMap,
]);
}
}