|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Lamoda\MultiEnv\FileReader; |
|
6
|
|
|
|
|
7
|
|
|
use Lamoda\MultiEnv\FileReader\Exception\EnvFileReaderException; |
|
8
|
|
|
use Lamoda\MultiEnv\FileReader\FileNameResolver\FileNameResolverInterface; |
|
9
|
|
|
use Lamoda\MultiEnv\FileReader\PathResolver\PathResolverInterface; |
|
10
|
|
|
use Lamoda\MultiEnv\Model\HostId; |
|
11
|
|
|
|
|
12
|
|
|
final class DotEnvV2FileReaderAdapter implements EnvFileReaderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var PathResolverInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $pathResolver; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var FileNameResolverInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $fileNameResolver; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private $isEnvFileAlreadyLoaded; |
|
28
|
|
|
|
|
29
|
9 |
|
public function __construct(PathResolverInterface $pathResolver, FileNameResolverInterface $fileNameResolver) |
|
30
|
|
|
{ |
|
31
|
|
|
// @codeCoverageIgnoreStart |
|
32
|
|
|
if (!class_exists(\Dotenv\Dotenv::class)) { |
|
33
|
|
|
throw EnvFileReaderException::becauseAdapterCanNotBeCreated(__CLASS__, \Dotenv\Dotenv::class); |
|
34
|
|
|
} |
|
35
|
|
|
// @codeCoverageIgnoreEnd |
|
36
|
9 |
|
$this->pathResolver = $pathResolver; |
|
37
|
9 |
|
$this->fileNameResolver = $fileNameResolver; |
|
38
|
9 |
|
$this->isEnvFileAlreadyLoaded = false; |
|
39
|
9 |
|
} |
|
40
|
|
|
|
|
41
|
15 |
|
public function readEnvFile(HostId $hostId): void |
|
42
|
|
|
{ |
|
43
|
15 |
|
if ($this->isEnvFileAlreadyLoaded) { |
|
44
|
1 |
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
15 |
|
$dotEnv = new \Dotenv\Dotenv( |
|
49
|
15 |
|
$this->pathResolver->resolvePathToEnvFile($hostId), |
|
50
|
15 |
|
$this->fileNameResolver->resolveEnvFileName($hostId) |
|
51
|
|
|
); |
|
52
|
15 |
|
$dotEnv->overload(); |
|
53
|
14 |
|
$this->isEnvFileAlreadyLoaded = true; |
|
54
|
1 |
|
} catch (\Dotenv\Exception\ExceptionInterface $exception) { |
|
55
|
1 |
|
throw EnvFileReaderException::becauseEnvFileCanNotBeProcessed($exception); |
|
56
|
|
|
} |
|
57
|
14 |
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|