-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayEnvVarProcessor.php
More file actions
82 lines (71 loc) · 1.74 KB
/
Copy pathArrayEnvVarProcessor.php
File metadata and controls
82 lines (71 loc) · 1.74 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types = 1);
namespace C0ntax\EnvProvidersBundle;
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* Class ArrayEnvVarProcessor
*
* @package C0ntax\EnvProvidersBundle
*/
class ArrayEnvVarProcessor implements EnvVarProcessorInterface
{
/** @var array */
private $config;
/**
* ArrayEnvVarProcessor constructor.
*
* @param array $config
*/
public function __construct(array $config)
{
$this->setConfig($config);
}
/**
* @param string $prefix
* @param string $name
* @param \Closure $getEnv
* @return array
* @throws RuntimeException
*/
public function getEnv($prefix, $name, \Closure $getEnv): ?array
{
if ('array' === $prefix) {
$env = $getEnv($name);
if ($env === null || $env === '') {
if ($this->getConfig()['return_null_if_empty']) {
return null;
} else {
return [];
}
}
return preg_split('/\s*,\s*/', $env);
}
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
}
/**
* @return array
*/
public static function getProvidedTypes(): array
{
return [
'array' => 'array',
];
}
/**
* @return array
*/
private function getConfig(): array
{
return $this->config;
}
/**
* @param array $config
* @return ArrayEnvVarProcessor
*/
private function setConfig(array $config): ArrayEnvVarProcessor
{
$this->config = $config;
return $this;
}
}