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\Exception\ConfigException; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Config |
12
|
|
|
* |
13
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta |
14
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
15
|
|
|
*/ |
16
|
|
|
class Config implements ConfigInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private static $projectRootDirectory; |
20
|
|
|
private $config = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Config constructor. |
24
|
|
|
* |
25
|
|
|
* @param array $server |
26
|
|
|
* |
27
|
|
|
* @throws ConfigException |
28
|
|
|
*/ |
29
|
151 |
|
public function __construct(array $server) |
30
|
|
|
{ |
31
|
151 |
|
foreach (static::REQUIRED_PARAMS as $key) { |
32
|
151 |
|
if (!array_key_exists($key, $server)) { |
33
|
1 |
|
throw new ConfigException( |
34
|
1 |
|
'required config param ' . $key . ' is not set in $server' |
35
|
|
|
); |
36
|
|
|
} |
37
|
150 |
|
$this->config[$key] = $server[$key]; |
38
|
|
|
} |
39
|
150 |
|
foreach (static::OPTIONAL_PARAMS_WITH_DEFAULTS as $key => $value) { |
40
|
150 |
|
if (array_key_exists($key, $server)) { |
41
|
150 |
|
$this->config[$key] = $server[$key]; |
42
|
|
|
} |
43
|
|
|
} |
44
|
150 |
|
foreach (static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS as $key => $value) { |
45
|
150 |
|
if (array_key_exists($key, $server)) { |
46
|
150 |
|
$this->config[$key] = $server[$key]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
150 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $key |
53
|
|
|
* @param mixed $default |
54
|
|
|
* |
55
|
|
|
* @return mixed|string |
56
|
|
|
* @throws DoctrineStaticMetaException |
57
|
|
|
*/ |
58
|
149 |
|
public function get(string $key, $default = ConfigInterface::NO_DEFAULT_VALUE) |
59
|
|
|
{ |
60
|
149 |
|
if (!isset(static::REQUIRED_PARAMS[$key]) |
61
|
149 |
|
&& !isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key]) |
62
|
149 |
|
&& !isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key]) |
63
|
|
|
) { |
64
|
|
|
throw new ConfigException( |
65
|
|
|
'Invalid config param ' |
66
|
|
|
. $key |
67
|
|
|
. ', should be one of ' |
68
|
|
|
. print_r(static::PARAMS, true) |
69
|
|
|
); |
70
|
|
|
} |
71
|
149 |
|
if (isset($this->config[$key])) { |
72
|
147 |
|
return $this->config[$key]; |
73
|
|
|
} |
74
|
147 |
|
if (ConfigInterface::NO_DEFAULT_VALUE !== $default) { |
75
|
|
|
return $default; |
76
|
|
|
} |
77
|
147 |
|
if (isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])) { |
78
|
146 |
|
return static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key]; |
79
|
|
|
} |
80
|
146 |
|
if (isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key])) { |
81
|
146 |
|
$method = static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key]; |
82
|
|
|
|
83
|
146 |
|
return $this->$method(); |
84
|
|
|
} |
85
|
|
|
throw new ConfigException( |
86
|
|
|
'No config set for param ' . $key . ' and no default provided' |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Default Entities path, calculated default |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
* @throws DoctrineStaticMetaException |
95
|
|
|
*/ |
96
|
1 |
|
private function calculateEntitiesPath(): string |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
1 |
|
return self::getProjectRootDirectory() . '/src/Entities'; |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
throw new DoctrineStaticMetaException( |
102
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
103
|
|
|
$e->getCode(), |
104
|
|
|
$e |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the absolute path to the root of the current project |
111
|
|
|
* |
112
|
|
|
* It does this by working from the Composer autoloader which we know will be in a certain place in `vendor` |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
* @throws DoctrineStaticMetaException |
116
|
|
|
*/ |
117
|
148 |
|
public static function getProjectRootDirectory(): string |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
148 |
|
if (null === self::$projectRootDirectory) { |
121
|
|
|
$reflection = new \ts\Reflection\ReflectionClass(ClassLoader::class); |
122
|
|
|
self::$projectRootDirectory = \dirname($reflection->getFileName(), 3); |
123
|
|
|
} |
124
|
|
|
|
125
|
148 |
|
return self::$projectRootDirectory; |
126
|
|
|
} catch (\Exception $e) { |
127
|
|
|
throw new DoctrineStaticMetaException( |
128
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
129
|
|
|
$e->getCode(), |
130
|
|
|
$e |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Default Entities path, calculated default |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
* @throws DoctrineStaticMetaException |
140
|
|
|
*/ |
141
|
145 |
|
private function calculateProxyDir(): string |
142
|
|
|
{ |
143
|
|
|
try { |
144
|
145 |
|
$dir = self::getProjectRootDirectory() . '/cache/Proxies'; |
145
|
145 |
|
if (!is_dir($dir) && !(mkdir($dir, 0777, true) && is_dir($dir))) { |
146
|
|
|
throw new \RuntimeException( |
147
|
|
|
'Proxy directory ' . $dir . ' does not exist and failed trying to create it' |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
145 |
|
return $dir; |
152
|
|
|
} catch (\Exception $e) { |
153
|
|
|
throw new DoctrineStaticMetaException( |
154
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
155
|
|
|
$e->getCode(), |
156
|
|
|
$e |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return UnderscoreNamingStrategy |
163
|
|
|
*/ |
164
|
145 |
|
private function getUnderscoreNamingStrategy(): UnderscoreNamingStrategy |
165
|
|
|
{ |
166
|
145 |
|
return new UnderscoreNamingStrategy(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return string |
171
|
|
|
* @throws DoctrineStaticMetaException |
172
|
|
|
*/ |
173
|
|
|
private function getFilesystemCachePath(): string |
174
|
|
|
{ |
175
|
|
|
$path = self::getProjectRootDirectory() . '/cache/dsm'; |
176
|
|
|
if (!is_dir($path) && !(mkdir($path, 0777, true) && is_dir($path))) { |
177
|
|
|
throw new \RuntimeException('Failed creating default cache path at ' . $path); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $path; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|