1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
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\Schema\LockFileSchemaManager; |
||
12 | use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser; |
||
13 | use TheCodingMachine\TDBM\Utils\CodeGeneratorEventDispatcher; |
||
14 | use TheCodingMachine\TDBM\Utils\CodeGeneratorListenerInterface; |
||
15 | use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy; |
||
16 | use TheCodingMachine\TDBM\Utils\GeneratorEventDispatcher; |
||
17 | use TheCodingMachine\TDBM\Utils\GeneratorListenerInterface; |
||
18 | use TheCodingMachine\TDBM\Utils\NamingStrategyInterface; |
||
19 | use TheCodingMachine\TDBM\Utils\PathFinder\PathFinderInterface; |
||
20 | use Psr\Log\LoggerInterface; |
||
21 | use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder; |
||
22 | use TheCodingMachine\TDBM\Utils\RootProjectLocator; |
||
23 | |||
24 | use function hash; |
||
25 | use function serialize; |
||
26 | |||
27 | class Configuration implements ConfigurationInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $beanNamespace; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $daoNamespace; |
||
37 | /** @var string */ |
||
38 | private $resultIteratorNamespace; |
||
39 | /** |
||
40 | * @var Connection |
||
41 | */ |
||
42 | private $connection; |
||
43 | /** |
||
44 | * @var Cache |
||
45 | */ |
||
46 | private $cache; |
||
47 | /** |
||
48 | * @var SchemaAnalyzer |
||
49 | */ |
||
50 | private $schemaAnalyzer; |
||
51 | /** |
||
52 | * @var LoggerInterface|null |
||
53 | */ |
||
54 | private $logger; |
||
55 | /** |
||
56 | * @var GeneratorListenerInterface |
||
57 | */ |
||
58 | private $generatorEventDispatcher; |
||
59 | /** |
||
60 | * @var CodeGeneratorListenerInterface |
||
61 | */ |
||
62 | private $codeGeneratorListener; |
||
63 | /** |
||
64 | * @var NamingStrategyInterface |
||
65 | */ |
||
66 | private $namingStrategy; |
||
67 | /** |
||
68 | * @var PathFinderInterface |
||
69 | */ |
||
70 | private $pathFinder; |
||
71 | /** |
||
72 | * @var AnnotationParser |
||
73 | */ |
||
74 | private $annotationParser; |
||
75 | private $lockFilePath; |
||
76 | |||
77 | /** |
||
78 | * @param string $beanNamespace The namespace hosting the beans |
||
79 | * @param string $daoNamespace The namespace hosting the DAOs |
||
80 | * @param string $resultIteratorNamespace The namespace hosting the ResultIterators |
||
81 | * @param Connection $connection The connection to the database |
||
82 | * @param NamingStrategyInterface|null $namingStrategy |
||
83 | * @param Cache|null $cache The Doctrine cache to store database metadata |
||
84 | * @param SchemaAnalyzer|null $schemaAnalyzer The schema analyzer that will be used to find shortest paths... Will be automatically created if not passed |
||
85 | * @param LoggerInterface|null $logger The logger |
||
86 | * @param GeneratorListenerInterface[] $generatorListeners A list of listeners that will be triggered when beans/daos are generated |
||
87 | * @param AnnotationParser|null $annotationParser |
||
88 | * @param CodeGeneratorListenerInterface[] $codeGeneratorListeners A list of listeners that can alter code generation of each bean/dao |
||
89 | * @param string|null $lockFilePath |
||
90 | * @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException |
||
91 | */ |
||
92 | public function __construct( |
||
93 | string $beanNamespace, |
||
94 | string $daoNamespace, |
||
95 | Connection $connection, |
||
96 | NamingStrategyInterface $namingStrategy = null, |
||
97 | Cache $cache = null, |
||
98 | SchemaAnalyzer $schemaAnalyzer = null, |
||
99 | LoggerInterface $logger = null, |
||
100 | array $generatorListeners = [], |
||
101 | AnnotationParser $annotationParser = null, |
||
102 | array $codeGeneratorListeners = [], |
||
103 | string $resultIteratorNamespace = null, |
||
104 | string $lockFilePath = null |
||
105 | ) { |
||
106 | $this->beanNamespace = rtrim($beanNamespace, '\\'); |
||
107 | $this->daoNamespace = rtrim($daoNamespace, '\\'); |
||
108 | if ($resultIteratorNamespace === null) { |
||
109 | $baseNamespace = explode('\\', $this->daoNamespace); |
||
110 | array_pop($baseNamespace); |
||
111 | $baseNamespace[] = 'ResultIterator'; |
||
112 | $resultIteratorNamespace = implode('\\', $baseNamespace); |
||
113 | } |
||
114 | $this->resultIteratorNamespace = rtrim($resultIteratorNamespace, '\\'); |
||
115 | $this->connection = $connection; |
||
116 | if ($cache !== null) { |
||
117 | $this->cache = $cache; |
||
118 | } else { |
||
119 | $this->cache = new VoidCache(); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||
120 | } |
||
121 | $this->lockFilePath = $lockFilePath; |
||
122 | $schemaLockFileDumper = new SchemaLockFileDumper($this->connection, $this->cache, $this->getLockFilePath()); |
||
123 | $lockFileSchemaManager = new LockFileSchemaManager($this->connection->createSchemaManager(), $schemaLockFileDumper); |
||
124 | if ($schemaAnalyzer !== null) { |
||
125 | $this->schemaAnalyzer = $schemaAnalyzer; |
||
126 | } else { |
||
127 | $this->schemaAnalyzer = new SchemaAnalyzer($lockFileSchemaManager, $this->cache, $this->getConnectionUniqueId()); |
||
128 | } |
||
129 | $this->logger = $logger; |
||
130 | $this->generatorEventDispatcher = new GeneratorEventDispatcher($generatorListeners); |
||
131 | $this->pathFinder = new PathFinder(); |
||
132 | $this->annotationParser = $annotationParser ?: AnnotationParser::buildWithDefaultAnnotations([]); |
||
133 | $this->codeGeneratorListener = new CodeGeneratorEventDispatcher($codeGeneratorListeners); |
||
134 | $this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy($this->annotationParser, $lockFileSchemaManager); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getBeanNamespace(): string |
||
141 | { |
||
142 | return $this->beanNamespace; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getDaoNamespace(): string |
||
149 | { |
||
150 | return $this->daoNamespace; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getResultIteratorNamespace(): string |
||
157 | { |
||
158 | return $this->resultIteratorNamespace; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return Connection |
||
163 | */ |
||
164 | public function getConnection(): Connection |
||
165 | { |
||
166 | return $this->connection; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @return NamingStrategyInterface |
||
171 | */ |
||
172 | public function getNamingStrategy(): NamingStrategyInterface |
||
173 | { |
||
174 | return $this->namingStrategy; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return Cache |
||
179 | */ |
||
180 | public function getCache(): Cache |
||
181 | { |
||
182 | return $this->cache; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return SchemaAnalyzer |
||
187 | */ |
||
188 | public function getSchemaAnalyzer(): SchemaAnalyzer |
||
189 | { |
||
190 | return $this->schemaAnalyzer; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return LoggerInterface |
||
195 | */ |
||
196 | public function getLogger(): ?LoggerInterface |
||
197 | { |
||
198 | return $this->logger; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return GeneratorListenerInterface |
||
203 | */ |
||
204 | public function getGeneratorEventDispatcher(): GeneratorListenerInterface |
||
205 | { |
||
206 | return $this->generatorEventDispatcher; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @return CodeGeneratorListenerInterface |
||
211 | */ |
||
212 | public function getCodeGeneratorListener(): CodeGeneratorListenerInterface |
||
213 | { |
||
214 | return $this->codeGeneratorListener; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Creates a unique cache key for the current connection. |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | private function getConnectionUniqueId(): string |
||
223 | { |
||
224 | return hash('md4', serialize($this->connection->getParams())); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns a class able to find the place of a PHP file based on the class name. |
||
229 | * Useful to find the path where DAOs and beans should be written to. |
||
230 | * |
||
231 | * @return PathFinderInterface |
||
232 | */ |
||
233 | public function getPathFinder(): PathFinderInterface |
||
234 | { |
||
235 | return $this->pathFinder; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param PathFinderInterface $pathFinder |
||
240 | */ |
||
241 | public function setPathFinder(PathFinderInterface $pathFinder): void |
||
242 | { |
||
243 | $this->pathFinder = $pathFinder; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return AnnotationParser |
||
248 | */ |
||
249 | public function getAnnotationParser(): AnnotationParser |
||
250 | { |
||
251 | return $this->annotationParser; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @internal |
||
256 | */ |
||
257 | public static function getDefaultLockFilePath(): string |
||
258 | { |
||
259 | return RootProjectLocator::getRootLocationPath().'tdbm.lock.yml'; |
||
260 | } |
||
261 | |||
262 | public function getLockFilePath(): string |
||
263 | { |
||
264 | return $this->lockFilePath ?: self::getDefaultLockFilePath(); |
||
265 | } |
||
266 | } |
||
267 |