|
11 | 11 |
|
12 | 12 | namespace CaptainHook\App; |
13 | 13 |
|
| 14 | +use CaptainHook\App\Config\Helper; |
14 | 15 | use CaptainHook\App\Config\Run; |
15 | 16 | use CaptainHook\App\Storage\File; |
16 | 17 | use InvalidArgumentException; |
@@ -92,93 +93,80 @@ class Config |
92 | 93 | */ |
93 | 94 | public function __construct(string $path, bool $fileExists = false, array $settings = []) |
94 | 95 | { |
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); |
98 | 101 |
|
| 102 | + // remember that the path given was absolute, important for 'install' handling |
99 | 103 | if (Check::isAbsolutePath($path)) { |
100 | 104 | $this->pathProvidedIsAbsolute = true; |
101 | 105 | } |
102 | | - |
103 | 106 | $this->path = File::makePathAbsolute($path); |
104 | 107 | $this->fileExists = $fileExists; |
105 | | - $this->settings = $settings; |
| 108 | + } |
106 | 109 |
|
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); |
110 | 121 | } |
111 | 122 |
|
112 | 123 | /** |
113 | 124 | * Extract custom settings from Captain Hook ones |
114 | 125 | * |
115 | 126 | * @param array<string, mixed> $settings |
116 | | - * @return array<string, mixed> |
| 127 | + * @return void |
117 | 128 | */ |
118 | | - private function setupCustom(array $settings): array |
| 129 | + private function setupCustom(array $settings): void |
119 | 130 | { |
120 | 131 | /* @var array<string, mixed> $custom */ |
121 | 132 | $this->custom = $settings['custom'] ?? []; |
122 | | - unset($settings['custom']); |
123 | | - |
124 | | - return $settings; |
125 | 133 | } |
126 | 134 |
|
127 | 135 | /** |
128 | 136 | * Setup all configured plugins |
129 | 137 | * |
130 | 138 | * @param array<string, mixed> $settings |
131 | | - * @return array<string, mixed> |
| 139 | + * @return void |
132 | 140 | */ |
133 | | - private function setupPlugins(array $settings): array |
| 141 | + private function setupPlugins(array $settings): void |
134 | 142 | { |
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); |
147 | 144 | } |
148 | 145 |
|
149 | 146 | /** |
150 | | - * Extract all running related settings into a run configuration |
| 147 | + * Extract all run-related settings into a run configuration |
151 | 148 | * |
152 | 149 | * @param array<string, mixed> $settings |
153 | | - * @return array<string, mixed> |
| 150 | + * @return void |
154 | 151 | */ |
155 | | - private function setupRunConfig(array $settings): array |
| 152 | + private function setupRunConfig(array $settings): void |
156 | 153 | { |
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); |
175 | 165 | } |
176 | | - $this->runConfig = new Run($config); |
177 | | - return $settings; |
178 | 166 | } |
179 | 167 |
|
180 | 168 | /** |
181 | | - * Is configuration loaded from file |
| 169 | + * Is the configuration loaded from a file? |
182 | 170 | * |
183 | 171 | * @return bool |
184 | 172 | */ |
|
0 commit comments