PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.0
Defender Security – Malware Scanner, Login Security & Firewall v5.7.0
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.7.0, at lib/packages/DI/Definition/EnvironmentVariableDefinition.php

88 lines 2.5 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 /** 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 }
88