1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace DI\Definition;
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* Defines a reference to an environment variable, with fallback to a default
|
|
|
9 |
* value if the environment variable is not defined.
|
|
|
10 |
*
|
|
|
11 |
* @author James Harris <james.harris@icecave.com.au>
|
|
|
12 |
*/
|
|
|
13 |
class EnvironmentVariableDefinition implements Definition
|
|
|
14 |
{
|
|
|
15 |
/** Entry name. */
|
|
|
16 |
private string $name = '';
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* @param string $variableName The name of the environment variable
|
|
|
20 |
* @param bool $isOptional Whether or not the environment variable definition is optional. If true and the environment variable given by $variableName has not been defined, $defaultValue is used.
|
|
|
21 |
* @param mixed $defaultValue The default value to use if the environment variable is optional and not provided
|
|
|
22 |
*/
|
|
|
23 |
public function __construct(
|
|
|
24 |
private string $variableName,
|
|
|
25 |
private bool $isOptional = false,
|
|
|
26 |
private mixed $defaultValue = null,
|
|
|
27 |
) {
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function getName() : string
|
|
|
31 |
{
|
|
|
32 |
return $this->name;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function setName(string $name) : void
|
|
|
36 |
{
|
|
|
37 |
$this->name = $name;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @return string The name of the environment variable
|
|
|
42 |
*/
|
|
|
43 |
public function getVariableName() : string
|
|
|
44 |
{
|
|
|
45 |
return $this->variableName;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* @return bool Whether or not the environment variable definition is optional
|
|
|
50 |
*/
|
|
|
51 |
public function isOptional() : bool
|
|
|
52 |
{
|
|
|
53 |
return $this->isOptional;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @return mixed The default value to use if the environment variable is optional and not provided
|
|
|
58 |
*/
|
|
|
59 |
public function getDefaultValue() : mixed
|
|
|
60 |
{
|
|
|
61 |
return $this->defaultValue;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function replaceNestedDefinitions(callable $replacer) : void
|
|
|
65 |
{
|
|
|
66 |
$this->defaultValue = $replacer($this->defaultValue);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function __toString() : string
|
|
|
70 |
{
|
|
|
71 |
$str = ' variable = ' . $this->variableName . \PHP_EOL
|
|
|
72 |
. ' optional = ' . ($this->isOptional ? 'yes' : 'no');
|
|
|
73 |
|
|
|
74 |
if ($this->isOptional) {
|
|
|
75 |
if ($this->defaultValue instanceof Definition) {
|
|
|
76 |
$nestedDefinition = (string) $this->defaultValue;
|
|
|
77 |
$defaultValueStr = str_replace(\PHP_EOL, \PHP_EOL . ' ', $nestedDefinition);
|
|
|
78 |
} else {
|
|
|
79 |
$defaultValueStr = var_export($this->defaultValue, true);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$str .= \PHP_EOL . ' default = ' . $defaultValueStr;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
return sprintf('Environment variable (' . \PHP_EOL . '%s' . \PHP_EOL . ')', $str);
|
|
|
86 |
}
|
|
|
87 |
}
|