PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.0.1
Defender Security – Malware Scanner, Login Security & Firewall v6.0.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 / vendor / cweagans / composer-configurable-plugin / src / ConfigurablePlugin.php

ConfigurablePlugin.php in Defender Security – Malware Scanner, Login Security & Firewall 6.0.1, at vendor/cweagans/composer-configurable-plugin/src/ConfigurablePlugin.php

174 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace cweagans\Composer;
4
5 /**
6 * @file
7 * Provides a generic way for Composer plugins to configure themselves.
8 */
9
10 trait ConfigurablePlugin {
11
12 /**
13 * Holds information about configurable values.
14 *
15 * @var array
16 */
17 protected array $configuration = [];
18
19 /**
20 * Holds the 'extra' key from composer.json.
21 *
22 * @var array
23 */
24 protected array $extra;
25
26 /**
27 * Holds the name of the sub-key in extra to look for this plugin's config.
28 *
29 * @var string
30 */
31 protected string $pluginKey;
32
33 /**
34 * Set up the ConfigurablePlugin trait.
35 *
36 * @param array $extra
37 * The 'extra' section from composer.json.
38 * @param string $pluginKey
39 * The subkey in extra to search for config. This will often be related to
40 * the plugin name. For instance, cweagans/composer-patches uses "patches-config".
41 */
42 public function configure(array $extra, string $pluginKey): void
43 {
44 $this->extra = $extra;
45 $this->pluginKey = $pluginKey;
46 }
47
48 /**
49 * Retrieve a specified configuration value.
50 *
51 * @param string $key
52 * The configuration key to get a value for.
53 * @return string|int|bool|array
54 * The value of the config key.
55 */
56 public function getConfig($key): mixed
57 {
58 // Bail out early if we don't have any information from the plugin or
59 // if the requested config key was not described to us.
60 if (is_null($this->extra) || is_null($this->pluginKey)) {
61 throw new \LogicException('You must call ConfigurablePlugin::configure() before attempting to retrieve a config value.');
62 }
63 if (!array_key_exists($key, $this->configuration)) {
64 throw new \InvalidArgumentException('Config key ' . $key . ' was not declared in $configuration.');
65 }
66
67 // Start with the default value from configuration.
68 $value = $this->configuration[$key]['default'];
69
70 // If a value is set in composer.json, override the default.
71 if (isset($this->extra[$this->pluginKey][$key])) {
72 $value = $this->extra[$this->pluginKey][$key];
73 }
74
75 // If a value is set in the environment, override anything that we've
76 // previously found.
77 if (getenv($this->getEnvvarName($key)) !== FALSE) {
78 $prevValue = $value;
79 $value = getenv($this->getEnvvarName($key));
80 switch ($this->configuration[$key]['type']) {
81 case 'string':
82 // We don't need to do anything here. Envvars are all strings.
83 break;
84 case 'int':
85 $value = (int)$value;
86 break;
87 case 'bool':
88 $value = $this->castEnvvarToBool($value, $prevValue);
89 break;
90 case 'list':
91 $value = $this->castEnvvarToList($value, $prevValue);
92 break;
93 }
94 }
95
96 return $value;
97 }
98
99 /**
100 * Convert a config key name into an environment var name.
101 *
102 * @param $key
103 * The key to convert.
104 * @return string
105 * An envvar-ified version of $key
106 */
107 public function getEnvvarName($key): string
108 {
109 $key = $this->pluginKey . '_' . $key;
110 $key = strtoupper($key);
111 $key = str_replace('-', '_', $key);
112 return $key;
113 }
114
115 /**
116 * Get a boolean value from the environment.
117 *
118 * @param string $value
119 * The value retrieved from the environment.
120 * @param bool $default
121 * The default value to use if we can't figure out what the user wants.
122 *
123 * @return bool
124 */
125 public function castEnvvarToBool($value, $default): bool
126 {
127 // Everything is strtolower()'d because that cuts the number of cases
128 // to look for in half.
129 $value = trim(strtolower($value));
130
131 // If it looks false-y, return FALSE.
132 if ($value == 'false' || $value == '0' || $value == 'no') {
133 return FALSE;
134 }
135
136 // If it looks truth-y, return TRUE.
137 if ($value == 'true' || $value == '1' || $value == 'yes') {
138 return TRUE;
139 }
140
141 // Otherwise, just return the default value that we were given. Ain't
142 // nobody got time to look for a million different ways of saying yes
143 // or no.
144 return $default;
145 }
146
147 /**
148 * Get an array from the environment.
149 *
150 * @param string $value
151 * The value retrieved from the environment.
152 * @param array $default
153 * The default value to use if we can't figure out what the user wants.
154 *
155 * @return array
156 */
157 public function castEnvvarToList($value, $default): array
158 {
159 // Trim any extra whitespace and then split the string on commas.
160 $value = explode(',', trim($value));
161
162 // Strip any empty values.
163 $value = array_filter($value);
164
165 // If we didn't get anything from the supplied value, better to just use the default.
166 if (empty($value)) {
167 return $default;
168 }
169
170 // Return the array.
171 return $value;
172 }
173 }
174