Skip to content

Commit c72a690

Browse files
Cleanup config handling
1 parent f79fd16 commit c72a690

2 files changed

Lines changed: 143 additions & 51 deletions

File tree

src/Config.php

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CaptainHook\App;
1313

14+
use CaptainHook\App\Config\Helper;
1415
use CaptainHook\App\Config\Run;
1516
use CaptainHook\App\Storage\File;
1617
use InvalidArgumentException;
@@ -92,93 +93,80 @@ class Config
9293
*/
9394
public function __construct(string $path, bool $fileExists = false, array $settings = [])
9495
{
95-
$settings = $this->setupPlugins($settings);
96-
$settings = $this->setupCustom($settings);
97-
$settings = $this->setupRunConfig($settings);
96+
$this->initializeHookConfigs();
97+
$this->setupSettings($settings);
98+
$this->setupPlugins($settings);
99+
$this->setupCustom($settings);
100+
$this->setupRunConfig($settings);
98101

102+
// remember that the path given was absolute, important for 'install' handling
99103
if (Check::isAbsolutePath($path)) {
100104
$this->pathProvidedIsAbsolute = true;
101105
}
102-
103106
$this->path = File::makePathAbsolute($path);
104107
$this->fileExists = $fileExists;
105-
$this->settings = $settings;
108+
}
106109

107-
foreach (Hooks::getValidHooks() as $hook => $value) {
108-
$this->hooks[$hook] = new Config\Hook($hook);
109-
}
110+
/**
111+
* Set up base config settings
112+
*
113+
* Basically everything that is not a run-config, plugin-config, or custom-config settings.
114+
*
115+
* @param array<string, mixed> $settings
116+
* @return void
117+
*/
118+
private function setupSettings(array $settings): void
119+
{
120+
$this->settings = Helper::extractBaseSettings($settings);
110121
}
111122

112123
/**
113124
* Extract custom settings from Captain Hook ones
114125
*
115126
* @param array<string, mixed> $settings
116-
* @return array<string, mixed>
127+
* @return void
117128
*/
118-
private function setupCustom(array $settings): array
129+
private function setupCustom(array $settings): void
119130
{
120131
/* @var array<string, mixed> $custom */
121132
$this->custom = $settings['custom'] ?? [];
122-
unset($settings['custom']);
123-
124-
return $settings;
125133
}
126134

127135
/**
128136
* Setup all configured plugins
129137
*
130138
* @param array<string, mixed> $settings
131-
* @return array<string, mixed>
139+
* @return void
132140
*/
133-
private function setupPlugins(array $settings): array
141+
private function setupPlugins(array $settings): void
134142
{
135-
/* @var array<int, array<string, mixed>> $pluginSettings */
136-
$pluginSettings = $settings['plugins'] ?? [];
137-
unset($settings['plugins']);
138-
139-
foreach ($pluginSettings as $plugin) {
140-
$name = (string) $plugin['plugin'];
141-
$options = isset($plugin['options']) && is_array($plugin['options'])
142-
? $plugin['options']
143-
: [];
144-
$this->plugins[$name] = new Config\Plugin($name, $options);
145-
}
146-
return $settings;
143+
$this->plugins = Helper::createPluginConfigs($settings);
147144
}
148145

149146
/**
150-
* Extract all running related settings into a run configuration
147+
* Extract all run-related settings into a run configuration
151148
*
152149
* @param array<string, mixed> $settings
153-
* @return array<string, mixed>
150+
* @return void
154151
*/
155-
private function setupRunConfig(array $settings): array
152+
private function setupRunConfig(array $settings): void
156153
{
157-
// extract the legacy settings
158-
$settingsToMove = [
159-
Config\Settings::RUN_MODE,
160-
Config\Settings::RUN_EXEC,
161-
Config\Settings::RUN_PATH,
162-
Config\Settings::RUN_GIT
163-
];
164-
$config = [];
165-
foreach ($settingsToMove as $setting) {
166-
if (!empty($settings[$setting])) {
167-
$config[substr($setting, 4)] = $settings[$setting];
168-
}
169-
unset($settings[$setting]);
170-
}
171-
// make sure the new run configuration supersedes the legacy settings
172-
if (isset($settings['run']) && is_array($settings['run'])) {
173-
$config = array_merge($config, $settings['run']);
174-
unset($settings['run']);
154+
$settings = Helper::cleanupRunConfig($settings);
155+
$this->runConfig = new Run($settings['run'] ?? []);
156+
}
157+
158+
/**
159+
* Initialize all hook configs
160+
*/
161+
private function initializeHookConfigs(): void
162+
{
163+
foreach (Hooks::getValidHooks() as $hook => $value) {
164+
$this->hooks[$hook] = new Config\Hook($hook);
175165
}
176-
$this->runConfig = new Run($config);
177-
return $settings;
178166
}
179167

180168
/**
181-
* Is configuration loaded from file
169+
* Is the configuration loaded from a file?
182170
*
183171
* @return bool
184172
*/

src/Config/Helper.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook.
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Config;
13+
14+
/**
15+
* Config Helper
16+
*
17+
* @package CaptainHook
18+
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
19+
* @link https://github.com/captainhook-git/captainhook
20+
* @since Class available since Release 5.28.2
21+
* @internal
22+
*/
23+
final class Helper
24+
{
25+
/**
26+
* Create plugin configs from the settings array
27+
*
28+
* @param array<string, mixed> $settings
29+
* @return array<string, \CaptainHook\App\Config\Plugin>
30+
*/
31+
public static function createPluginConfigs(array $settings): array
32+
{
33+
/* @var array<int, array<string, mixed>> $pluginSettings */
34+
$pluginSettings = $settings['plugins'] ?? [];
35+
unset($settings['plugins']);
36+
37+
$plugins = [];
38+
foreach ($pluginSettings as $plugin) {
39+
$name = (string) $plugin['plugin'];
40+
$options = isset($plugin['options']) && is_array($plugin['options']) ? $plugin['options'] : [];
41+
$plugins[$name] = new Plugin($name, $options);
42+
}
43+
return $plugins;
44+
}
45+
46+
/**
47+
* Clean up legacy run configuration settings
48+
*
49+
* @param array<string, mixed> $settings
50+
* @return array<string, mixed>
51+
*/
52+
public static function cleanupRunConfig(array $settings): array
53+
{
54+
// extract the legacy settings
55+
$settingsToMove = [
56+
Settings::RUN_MODE,
57+
Settings::RUN_EXEC,
58+
Settings::RUN_PATH,
59+
Settings::RUN_GIT
60+
];
61+
$config = [];
62+
foreach ($settingsToMove as $setting) {
63+
if (!empty($settings[$setting])) {
64+
$config[substr($setting, 4)] = $settings[$setting];
65+
}
66+
unset($settings[$setting]);
67+
}
68+
// make sure the new run configuration supersedes the legacy settings
69+
if (isset($settings['run']) && is_array($settings['run'])) {
70+
$config = array_merge($config, $settings['run']);
71+
}
72+
$settings['run'] = $config;
73+
return $settings;
74+
}
75+
76+
/**
77+
* Extract the base settings from the given settings array
78+
*
79+
* @param array<string, mixed> $settings
80+
* @return array<string, mixed>
81+
*/
82+
public static function extractBaseSettings(array $settings): array
83+
{
84+
$baseSettings = [
85+
Settings::ALLOW_FAILURE,
86+
Settings::BOOTSTRAP,
87+
Settings::COLORS,
88+
Settings::GIT_DIR,
89+
Settings::FAIL_ON_FIRST_ERROR,
90+
Settings::VERBOSITY,
91+
Settings::INCLUDES,
92+
Settings::INCLUDES_LEVEL,
93+
Settings::PHP_PATH,
94+
];
95+
96+
$config = [];
97+
foreach ($baseSettings as $setting) {
98+
if (isset($settings[$setting])) {
99+
$config[$setting] = $settings[$setting];
100+
}
101+
}
102+
return $config;
103+
}
104+
}

0 commit comments

Comments
 (0)