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