forked from nbgrp/env-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvEnvVarProcessor.php
More file actions
59 lines (48 loc) · 1.61 KB
/
CsvEnvVarProcessor.php
File metadata and controls
59 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
// SPDX-License-Identifier: BSD-3-Clause
declare(strict_types=1);
namespace Nbgrp\EnvBundle;
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
// @codeCoverageIgnoreStart
if (class_exists(CsvEnvVarProcessor::class, false)) {
return;
}
// @codeCoverageIgnoreEnd
final class CsvEnvVarProcessor implements EnvVarProcessorInterface
{
/** @var array<string, string> */
private $delimiterMap;
/**
* @param array<string, string> $delimiterMap
*/
public function __construct(array $delimiterMap)
{
$this->delimiterMap = $delimiterMap;
}
#[\Override]
public static function getProvidedTypes(): array
{
// NB
throw new BadMethodCallException('Unexpected call of '.__FUNCTION__);
// NB
}
/**
* @return non-empty-list<string|null>
*/
#[\Override]
public function getEnv(string $prefix, string $name, \Closure $getEnv): array
{
if (\array_key_exists($prefix, $this->delimiterMap) === false) {
throw new RuntimeException('There is no delimiter for prefix "'.$prefix.'".');
}
$delimiter = $this->delimiterMap[$prefix];
$env = $getEnv($name);
if (!\is_string($env)) {
throw new RuntimeException('Environment variable "'.$name.'" should be a string.');
}
// @phpstan-ignore greaterOrEqual.alwaysTrue
return str_getcsv($env, $delimiter, '"', \PHP_VERSION_ID >= 70400 ? '' : '\\');
}
}