|
7 | 7 |
|
8 | 8 | class ScriptHandler |
9 | 9 | { |
| 10 | + const CONFIG_MAIN_KEY = 'css-compiler'; |
| 11 | + const CONFIG_INPUT_KEY = 'input'; |
| 12 | + const CONFIG_OUTPUT_KEY = 'output'; |
| 13 | + const CONFIG_FORMATTER_KEY = 'format'; |
| 14 | + const CONFIG_DEFAULT_FORMATTER = 'compact'; |
| 15 | + |
10 | 16 | public static function compileCSS(Event $event) |
11 | 17 | { |
12 | | - $extras = $event->getComposer()->getPackage()->getExtra(); |
| 18 | + $extra = $event->getComposer()->getPackage()->getExtra(); |
| 19 | + |
| 20 | + static::validateConfiguration($extra); |
13 | 21 |
|
14 | | - if (!isset($extras['css-compiler'])) { |
15 | | - throw new \InvalidArgumentException('The parameter handler needs to be configured through the extra.css-compiler setting.'); |
| 22 | + $processor = new Processor($event->getIO()); |
| 23 | + $currentDirectory = getcwd(); |
| 24 | + foreach ($extra[static::CONFIG_MAIN_KEY] as $config) { |
| 25 | + foreach ($config[static::CONFIG_INPUT_KEY] as $item => $value) { |
| 26 | + $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::CONFIG_OUTPUT_KEY]}"); |
| 27 | + } |
| 28 | + |
| 29 | + $formatter = isset($config[static::CONFIG_FORMATTER_KEY]) |
| 30 | + ? $config[static::CONFIG_FORMATTER_KEY] |
| 31 | + : static::CONFIG_DEFAULT_FORMATTER; |
| 32 | + |
| 33 | + $processor->processFiles($formatter); |
16 | 34 | } |
17 | 35 |
|
18 | | - $configs = $extras['css-compiler']; |
| 36 | + $processor->saveOutput(); |
| 37 | + } |
19 | 38 |
|
20 | | - if (!is_array($configs)) { |
21 | | - throw new \InvalidArgumentException('The extra.css-compiler setting must be an array of a configuration objects.'); |
| 39 | + protected static function validateConfiguration(array $config) |
| 40 | + { |
| 41 | + if (!isset($config[static::CONFIG_MAIN_KEY])) { |
| 42 | + throw new \InvalidArgumentException('the parameter handler needs to be configured through the extra.css-compiler setting.'); |
22 | 43 | } |
23 | 44 |
|
24 | | - if (array_keys($configs) !== range(0, count($configs) - 1)) { |
25 | | - $configs = [$configs]; |
| 45 | + if (!is_array($config[static::CONFIG_MAIN_KEY])) { |
| 46 | + throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of a configuration objects.'); |
26 | 47 | } |
27 | 48 |
|
28 | | - $processor = new Processor($event->getIO()); |
29 | | - $prefix = getcwd(); |
30 | | - foreach ($configs as $config) { |
31 | | - if (!is_array($config)) { |
| 49 | + foreach ($config[static::CONFIG_MAIN_KEY] as $el) { |
| 50 | + if (!is_array($el)) { |
32 | 51 | throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.'); |
33 | 52 | } |
34 | 53 |
|
35 | | - foreach ($config['input'] as $item => $value) { |
36 | | - $processor->attachFiles("{$prefix}/{$value}", "{$prefix}/{$config['output']}"); |
| 54 | + if (!isset($el[static::CONFIG_INPUT_KEY])) { |
| 55 | + throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_INPUT_KEY . ' is required!'); |
| 56 | + } |
| 57 | + if (!isset($el[static::CONFIG_OUTPUT_KEY])) { |
| 58 | + throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_OUTPUT_KEY . ' is required!'); |
37 | 59 | } |
38 | | - |
39 | | - $processor->processFiles(isset($config['format']) ? $config['format'] : 'compact'); |
40 | 60 | } |
41 | | - |
42 | | - $processor->saveOutput(); |
43 | 61 | } |
44 | 62 | } |
0 commit comments