PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.3.1
Defender Security – Malware Scanner, Login Security & Firewall v5.3.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / DI / Definition / EnvironmentVariableDefinition.php

EnvironmentVariableDefinition.php in Defender Security – Malware Scanner, Login Security & Firewall 5.3.1, at lib/packages/DI/Definition/EnvironmentVariableDefinition.php

113 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace WPMU_DEV\Defender\Vendor\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 <[email protected]>
12 */
13 class EnvironmentVariableDefinition implements Definition
14 {
15 /**
16 * Entry name.
17 * @var string
18 */
19 private $name = '';
20
21 /**
22 * The name of the environment variable.
23 * @var string
24 */
25 private $variableName;
26
27 /**
28 * Whether or not the environment variable definition is optional.
29 *
30 * If true and the environment variable given by $variableName has not been
31 * defined, $defaultValue is used.
32 *
33 * @var bool
34 */
35 private $isOptional;
36
37 /**
38 * The default value to use if the environment variable is optional and not provided.
39 * @var mixed
40 */
41 private $defaultValue;
42
43 /**
44 * @param string $variableName The name of the environment variable
45 * @param bool $isOptional Whether or not the environment variable definition is optional
46 * @param mixed $defaultValue The default value to use if the environment variable is optional and not provided
47 */
48 public function __construct(string $variableName, bool $isOptional = false, $defaultValue = null)
49 {
50 $this->variableName = $variableName;
51 $this->isOptional = $isOptional;
52 $this->defaultValue = $defaultValue;
53 }
54
55 public function getName() : string
56 {
57 return $this->name;
58 }
59
60 public function setName(string $name)
61 {
62 $this->name = $name;
63 }
64
65 /**
66 * @return string The name of the environment variable
67 */
68 public function getVariableName() : string
69 {
70 return $this->variableName;
71 }
72
73 /**
74 * @return bool Whether or not the environment variable definition is optional
75 */
76 public function isOptional() : bool
77 {
78 return $this->isOptional;
79 }
80
81 /**
82 * @return mixed The default value to use if the environment variable is optional and not provided
83 */
84 public function getDefaultValue()
85 {
86 return $this->defaultValue;
87 }
88
89 public function replaceNestedDefinitions(callable $replacer)
90 {
91 $this->defaultValue = $replacer($this->defaultValue);
92 }
93
94 public function __toString()
95 {
96 $str = ' variable = ' . $this->variableName . \PHP_EOL
97 . ' optional = ' . ($this->isOptional ? 'yes' : 'no');
98
99 if ($this->isOptional) {
100 if ($this->defaultValue instanceof Definition) {
101 $nestedDefinition = (string) $this->defaultValue;
102 $defaultValueStr = str_replace(\PHP_EOL, \PHP_EOL . ' ', $nestedDefinition);
103 } else {
104 $defaultValueStr = var_export($this->defaultValue, true);
105 }
106
107 $str .= \PHP_EOL . ' default = ' . $defaultValueStr;
108 }
109
110 return sprintf('Environment variable (' . \PHP_EOL . '%s' . \PHP_EOL . ')', $str);
111 }
112 }
113