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