PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Dependencies / DI / Definition / EnvironmentVariableDefinition.php

EnvironmentVariableDefinition.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.5.2, at includes/Dependencies/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 WPDeveloper\BetterDocs\Dependencies\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 /**
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