1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace TheCodingMachine\TDBM; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use Doctrine\Common\Cache\VoidCache; |
9
|
|
|
use Doctrine\DBAL\Connection; |
10
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
11
|
|
|
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser; |
12
|
|
|
use TheCodingMachine\TDBM\Utils\Annotation\Autoincrement; |
13
|
|
|
use TheCodingMachine\TDBM\Utils\Annotation\UUID; |
14
|
|
|
use TheCodingMachine\TDBM\Utils\GeneratorEventDispatcher; |
15
|
|
|
use TheCodingMachine\TDBM\Utils\GeneratorListenerInterface; |
16
|
|
|
use TheCodingMachine\TDBM\Utils\NamingStrategyInterface; |
17
|
|
|
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinderInterface; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder; |
20
|
|
|
|
21
|
|
|
class Configuration implements ConfigurationInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $beanNamespace; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $daoNamespace; |
32
|
|
|
/** |
33
|
|
|
* @var Connection |
34
|
|
|
*/ |
35
|
|
|
private $connection; |
36
|
|
|
/** |
37
|
|
|
* @var Cache |
38
|
|
|
*/ |
39
|
|
|
private $cache; |
40
|
|
|
/** |
41
|
|
|
* @var SchemaAnalyzer |
42
|
|
|
*/ |
43
|
|
|
private $schemaAnalyzer; |
44
|
|
|
/** |
45
|
|
|
* @var LoggerInterface|null |
46
|
|
|
*/ |
47
|
|
|
private $logger; |
48
|
|
|
/** |
49
|
|
|
* @var GeneratorListenerInterface |
50
|
|
|
*/ |
51
|
|
|
private $generatorEventDispatcher; |
52
|
|
|
/** |
53
|
|
|
* @var NamingStrategyInterface |
54
|
|
|
*/ |
55
|
|
|
private $namingStrategy; |
56
|
|
|
/** |
57
|
|
|
* @var PathFinderInterface |
58
|
|
|
*/ |
59
|
|
|
private $pathFinder; |
60
|
|
|
/** |
61
|
|
|
* @var AnnotationParser |
62
|
|
|
*/ |
63
|
|
|
private $annotationParser; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $beanNamespace The namespace hosting the beans |
67
|
|
|
* @param string $daoNamespace The namespace hosting the DAOs |
68
|
|
|
* @param Connection $connection The connection to the database |
69
|
|
|
* @param NamingStrategyInterface $namingStrategy |
70
|
|
|
* @param Cache|null $cache The Doctrine cache to store database metadata |
71
|
|
|
* @param SchemaAnalyzer|null $schemaAnalyzer The schema analyzer that will be used to find shortest paths... Will be automatically created if not passed |
72
|
|
|
* @param LoggerInterface|null $logger The logger |
73
|
|
|
* @param GeneratorListenerInterface[] $generatorListeners A list of listeners that will be triggered when beans/daos are generated |
74
|
|
|
* @param AnnotationParser|null $annotationParser |
75
|
|
|
* @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException |
76
|
|
|
*/ |
77
|
|
|
public function __construct(string $beanNamespace, string $daoNamespace, Connection $connection, NamingStrategyInterface $namingStrategy, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null, LoggerInterface $logger = null, array $generatorListeners = [], AnnotationParser $annotationParser = null) |
78
|
|
|
{ |
79
|
|
|
$this->beanNamespace = rtrim($beanNamespace, '\\'); |
80
|
|
|
$this->daoNamespace = rtrim($daoNamespace, '\\'); |
81
|
|
|
$this->connection = $connection; |
82
|
|
|
$this->namingStrategy = $namingStrategy; |
83
|
|
|
if ($cache !== null) { |
84
|
|
|
$this->cache = $cache; |
85
|
|
|
} else { |
86
|
|
|
$this->cache = new VoidCache(); |
87
|
|
|
} |
88
|
|
|
if ($schemaAnalyzer !== null) { |
89
|
|
|
$this->schemaAnalyzer = $schemaAnalyzer; |
90
|
|
|
} else { |
91
|
|
|
$this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId()); |
92
|
|
|
} |
93
|
|
|
$this->logger = $logger; |
94
|
|
|
$this->generatorEventDispatcher = new GeneratorEventDispatcher($generatorListeners); |
95
|
|
|
$this->pathFinder = new PathFinder(); |
96
|
|
|
$this->annotationParser = $annotationParser ?: AnnotationParser::buildWithDefaultAnnotations([]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getBeanNamespace(): string |
103
|
|
|
{ |
104
|
|
|
return $this->beanNamespace; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getDaoNamespace(): string |
111
|
|
|
{ |
112
|
|
|
return $this->daoNamespace; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Connection |
117
|
|
|
*/ |
118
|
|
|
public function getConnection(): Connection |
119
|
|
|
{ |
120
|
|
|
return $this->connection; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return NamingStrategyInterface |
125
|
|
|
*/ |
126
|
|
|
public function getNamingStrategy(): NamingStrategyInterface |
127
|
|
|
{ |
128
|
|
|
return $this->namingStrategy; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return Cache |
133
|
|
|
*/ |
134
|
|
|
public function getCache(): Cache |
135
|
|
|
{ |
136
|
|
|
return $this->cache; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return SchemaAnalyzer |
141
|
|
|
*/ |
142
|
|
|
public function getSchemaAnalyzer(): SchemaAnalyzer |
143
|
|
|
{ |
144
|
|
|
return $this->schemaAnalyzer; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return LoggerInterface |
149
|
|
|
*/ |
150
|
|
|
public function getLogger(): ?LoggerInterface |
151
|
|
|
{ |
152
|
|
|
return $this->logger; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return GeneratorListenerInterface |
157
|
|
|
*/ |
158
|
|
|
public function getGeneratorEventDispatcher(): GeneratorListenerInterface |
159
|
|
|
{ |
160
|
|
|
return $this->generatorEventDispatcher; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Creates a unique cache key for the current connection. |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
private function getConnectionUniqueId(): string |
171
|
|
|
{ |
172
|
|
|
return hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns a class able to find the place of a PHP file based on the class name. |
177
|
|
|
* Useful to find the path where DAOs and beans should be written to. |
178
|
|
|
* |
179
|
|
|
* @return PathFinderInterface |
180
|
|
|
*/ |
181
|
|
|
public function getPathFinder(): PathFinderInterface |
182
|
|
|
{ |
183
|
|
|
return $this->pathFinder; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param PathFinderInterface $pathFinder |
188
|
|
|
*/ |
189
|
|
|
public function setPathFinder(PathFinderInterface $pathFinder): void |
190
|
|
|
{ |
191
|
|
|
$this->pathFinder = $pathFinder; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return AnnotationParser |
196
|
|
|
*/ |
197
|
|
|
public function getAnnotationParser(): AnnotationParser |
198
|
|
|
{ |
199
|
|
|
return $this->annotationParser; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|