• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cweagans / composer-configurable-plugin / 11301889744

04 Oct 2024 10:05AM UTC coverage: 97.5%. Remained the same
11301889744

push

github

web-flow
Bump composer/composer from 2.8.0 to 2.8.1 (#59)

Bumps [composer/composer](https://github.com/composer/composer) from 2.8.0 to 2.8.1.
- [Release notes](https://github.com/composer/composer/releases)
- [Changelog](https://github.com/composer/composer/blob/main/CHANGELOG.md)
- [Commits](https://github.com/composer/composer/compare/2.8.0...2.8.1)

---
updated-dependencies:
- dependency-name: composer/composer
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

39 of 40 relevant lines covered (97.5%)

5.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.5
/src/ConfigurablePlugin.php
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;
27✔
45
        $this->pluginKey = $pluginKey;
27✔
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)) {
5✔
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)) {
4✔
64
            throw new \InvalidArgumentException('Config key ' . $key . ' was not declared in $configuration.');
1✔
65
        }
66

67
        // Start with the default value from configuration.
68
        $value = $this->configuration[$key]['default'];
3✔
69

70
        // If a value is set in composer.json, override the default.
71
        if (isset($this->extra[$this->pluginKey][$key])) {
3✔
72
            $value = $this->extra[$this->pluginKey][$key];
1✔
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) {
3✔
78
            $prevValue = $value;
2✔
79
            $value = getenv($this->getEnvvarName($key));
2✔
80
            switch ($this->configuration[$key]['type']) {
2✔
81
                case 'string':
2✔
82
                    // We don't need to do anything here. Envvars are all strings.
83
                    break;
2✔
84
                case 'int':
1✔
85
                    $value = (int)$value;
1✔
86
                    break;
1✔
87
                case 'bool':
1✔
88
                    $value = $this->castEnvvarToBool($value, $prevValue);
1✔
89
                    break;
1✔
90
                case 'list':
1✔
91
                    $value = $this->castEnvvarToList($value, $prevValue);
1✔
92
                    break;
1✔
93
            }
94
        }
95

96
        return $value;
3✔
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;
6✔
110
        $key = strtoupper($key);
6✔
111
        $key = str_replace('-', '_', $key);
6✔
112
        return $key;
6✔
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));
18✔
130

131
        // If it looks false-y, return FALSE.
132
        if ($value == 'false' || $value == '0' || $value == 'no') {
18✔
133
            return FALSE;
8✔
134
        }
135

136
        // If it looks truth-y, return TRUE.
137
        if ($value == 'true' || $value == '1' || $value == 'yes') {
10✔
138
            return TRUE;
9✔
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;
1✔
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));
4✔
161

162
        // Strip any empty values.
163
        $value = array_filter($value);
4✔
164

165
        // If we didn't get anything from the supplied value, better to just use the default.
166
        if (empty($value)) {
4✔
167
            return $default;
1✔
168
        }
169

170
        // Return the array.
171
        return $value;
3✔
172
    }
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc