1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta; |
4
|
|
|
|
5
|
|
|
use Composer\Autoload\ClassLoader; |
6
|
|
|
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\ConfigException; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Config |
14
|
|
|
* |
15
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta |
16
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
17
|
|
|
*/ |
18
|
|
|
class Config implements ConfigInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
private static $projectRootDirectory; |
22
|
|
|
private $config = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Config constructor. |
26
|
|
|
* |
27
|
|
|
* @param array|mixed[] $server |
28
|
|
|
* |
29
|
|
|
* @throws ConfigException |
30
|
|
|
* @throws DoctrineStaticMetaException |
31
|
|
|
*/ |
32
|
16 |
|
public function __construct(array $server) |
33
|
|
|
{ |
34
|
16 |
|
foreach (static::REQUIRED_PARAMS as $key) { |
35
|
16 |
|
if (!\array_key_exists($key, $server)) { |
36
|
4 |
|
throw new ConfigException( |
37
|
4 |
|
'required config param ' . $key . ' is not set in $server' |
38
|
|
|
); |
39
|
|
|
} |
40
|
12 |
|
$this->config[$key] = $server[$key]; |
41
|
|
|
} |
42
|
12 |
|
foreach (self::PARAMS as $key) { |
43
|
12 |
|
if (\array_key_exists($key, $server)) { |
44
|
12 |
|
$this->config[$key] = $server[$key]; |
45
|
12 |
|
continue; |
46
|
|
|
} |
47
|
12 |
|
$this->config[$key] = $this->get($key); |
48
|
|
|
} |
49
|
|
|
|
50
|
12 |
|
$this->validateConfig(); |
51
|
12 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $key |
55
|
|
|
* @param mixed $default |
56
|
|
|
* |
57
|
|
|
* @return mixed|string |
58
|
|
|
* @throws DoctrineStaticMetaException |
59
|
|
|
*/ |
60
|
12 |
|
public function get(string $key, $default = ConfigInterface::NO_DEFAULT_VALUE) |
61
|
|
|
{ |
62
|
12 |
|
if (!isset(static::REQUIRED_PARAMS[$key]) |
63
|
12 |
|
&& !isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key]) |
64
|
12 |
|
&& !isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key]) |
65
|
|
|
) { |
66
|
|
|
throw new ConfigException( |
67
|
|
|
'Invalid config param ' |
68
|
|
|
. $key |
69
|
|
|
. ', should be one of ' |
70
|
|
|
. print_r(static::PARAMS, true) |
71
|
|
|
); |
72
|
|
|
} |
73
|
12 |
|
if (isset($this->config[$key])) { |
74
|
12 |
|
return $this->config[$key]; |
75
|
|
|
} |
76
|
12 |
|
if (ConfigInterface::NO_DEFAULT_VALUE !== $default) { |
77
|
|
|
return $default; |
78
|
|
|
} |
79
|
12 |
|
if (isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])) { |
80
|
12 |
|
return static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key]; |
81
|
|
|
} |
82
|
12 |
|
if (isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key])) { |
83
|
12 |
|
$method = static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key]; |
84
|
|
|
|
85
|
12 |
|
return $this->$method(); |
86
|
|
|
} |
87
|
|
|
throw new ConfigException( |
88
|
|
|
'No config set for param ' . $key . ' and no default provided' |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @throws ConfigException |
94
|
|
|
* @throws DoctrineStaticMetaException |
95
|
|
|
*/ |
96
|
12 |
|
private function validateConfig(): void |
97
|
|
|
{ |
98
|
12 |
|
$errors = []; |
99
|
12 |
|
$typeHelper = new TypeHelper(); |
100
|
12 |
|
foreach (ConfigInterface::PARAM_TYPES as $param => $requiredType) { |
101
|
12 |
|
$value = $this->get($param); |
102
|
12 |
|
if (self::TYPE_BOOL === $requiredType |
103
|
12 |
|
&& is_numeric($value) |
104
|
12 |
|
&& \in_array((int)$value, [0, 1], true) |
105
|
|
|
) { |
106
|
2 |
|
$this->config[$param] = ($value === 1); |
107
|
2 |
|
continue; |
108
|
|
|
} |
109
|
12 |
|
if (\is_object($value)) { |
110
|
12 |
|
if (!($value instanceof $requiredType)) { |
111
|
|
|
$actualType = \get_class($value); |
112
|
|
|
$errors[] = |
113
|
|
|
' ERROR ' . $param . ' is not an instance of the required object [' . $requiredType . ']' |
114
|
|
|
. 'currently configured as an object of the class [' . $actualType . ']'; |
115
|
|
|
} |
116
|
12 |
|
continue; |
117
|
|
|
} |
118
|
12 |
|
$actualType = $typeHelper->getType($value); |
119
|
12 |
|
if ($actualType !== $requiredType) { |
120
|
|
|
$valueString = var_export($value, true); |
121
|
|
|
$errors[] = ' ERROR ' . $param . ' is not of the required type [' . $requiredType . ']' |
122
|
|
|
. ' currently configured as type [' . $actualType . '] with value: ' . $valueString; |
123
|
|
|
} |
124
|
|
|
} |
125
|
12 |
|
if ([] !== $errors) { |
126
|
|
|
throw new ConfigException(implode("\n\n", $errors)); |
127
|
|
|
} |
128
|
12 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Default Entities path, calculated default |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
* @throws DoctrineStaticMetaException |
135
|
|
|
*/ |
136
|
10 |
|
private function calculateEntitiesPath(): string |
137
|
|
|
{ |
138
|
|
|
try { |
139
|
10 |
|
return self::getProjectRootDirectory() . '/src/Entities'; |
140
|
|
|
} catch (\Exception $e) { |
141
|
|
|
throw new DoctrineStaticMetaException( |
142
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
143
|
|
|
$e->getCode(), |
144
|
|
|
$e |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
12 |
|
private function calculateEntitiesCustomDataPath(): string |
150
|
|
|
{ |
151
|
|
|
try { |
152
|
12 |
|
return self::getProjectRootDirectory() . '/tests/Assets/Entity/FakerDataFillers'; |
153
|
|
|
} catch (\Exception $e) { |
154
|
|
|
throw new DoctrineStaticMetaException( |
155
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
156
|
|
|
$e->getCode(), |
157
|
|
|
$e |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the absolute path to the root of the current project |
164
|
|
|
* |
165
|
|
|
* It does this by working from the Composer autoloader which we know will be in a certain place in `vendor` |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
* @throws DoctrineStaticMetaException |
169
|
|
|
*/ |
170
|
12 |
|
public static function getProjectRootDirectory(): string |
171
|
|
|
{ |
172
|
|
|
try { |
173
|
12 |
|
if (null === self::$projectRootDirectory) { |
174
|
|
|
$reflection = new \ts\Reflection\ReflectionClass(ClassLoader::class); |
175
|
|
|
self::$projectRootDirectory = \dirname($reflection->getFileName(), 3); |
176
|
|
|
} |
177
|
|
|
|
178
|
12 |
|
return self::$projectRootDirectory; |
179
|
|
|
} catch (\Exception $e) { |
180
|
|
|
throw new DoctrineStaticMetaException( |
181
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
182
|
|
|
$e->getCode(), |
183
|
|
|
$e |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Default Entities path, calculated default |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
* @throws DoctrineStaticMetaException |
193
|
|
|
*/ |
194
|
12 |
|
private function calculateProxyDir(): string |
195
|
|
|
{ |
196
|
|
|
try { |
197
|
12 |
|
$dir = self::getProjectRootDirectory() . '/cache/Proxies'; |
198
|
12 |
|
if (!is_dir($dir) && !(mkdir($dir, 0777, true) && is_dir($dir))) { |
199
|
|
|
throw new \RuntimeException( |
200
|
|
|
'Proxy directory ' . $dir . ' does not exist and failed trying to create it' |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
12 |
|
return $dir; |
205
|
|
|
} catch (\Exception $e) { |
206
|
|
|
throw new DoctrineStaticMetaException( |
207
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
208
|
|
|
$e->getCode(), |
209
|
|
|
$e |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return UnderscoreNamingStrategy |
216
|
|
|
*/ |
217
|
12 |
|
private function getUnderscoreNamingStrategy(): UnderscoreNamingStrategy |
218
|
|
|
{ |
219
|
12 |
|
return new UnderscoreNamingStrategy(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return string |
224
|
|
|
* @throws DoctrineStaticMetaException |
225
|
|
|
*/ |
226
|
12 |
|
private function getFilesystemCachePath(): string |
227
|
|
|
{ |
228
|
12 |
|
$path = self::getProjectRootDirectory() . '/cache/dsm'; |
229
|
12 |
|
if (!is_dir($path) && !(mkdir($path, 0777, true) && is_dir($path))) { |
230
|
|
|
throw new \RuntimeException('Failed creating default cache path at ' . $path); |
231
|
|
|
} |
232
|
|
|
|
233
|
12 |
|
return $path; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
* @throws DoctrineStaticMetaException |
239
|
|
|
*/ |
240
|
12 |
|
private function calculateMigrationsDirectory(): string |
241
|
|
|
{ |
242
|
12 |
|
$path = self::getProjectRootDirectory() . '/migrations'; |
243
|
12 |
|
if (!is_dir($path) && !(mkdir($path, 0777, true) && is_dir($path))) { |
244
|
|
|
throw new \RuntimeException('Failed creating default migrations directory at ' . $path); |
245
|
|
|
} |
246
|
|
|
|
247
|
12 |
|
return $path; |
248
|
|
|
} |
249
|
|
|
|
250
|
12 |
|
private function calculateProjectRootNamespace(): string |
251
|
|
|
{ |
252
|
12 |
|
$namespaceHelper = new NamespaceHelper(); |
253
|
|
|
|
254
|
12 |
|
return $namespaceHelper->getProjectRootNamespaceFromComposerJson(); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|