Skip to content

Commit d8e9c28

Browse files
author
Eugene Matvejev
authored
Merge pull request #11 from learn-symfony/master
sync with main repository
2 parents fc7cfa3 + 29fb7d3 commit d8e9c28

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

ScriptHandler.php

+36-18
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,56 @@
77

88
class ScriptHandler
99
{
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+
1016
public static function compileCSS(Event $event)
1117
{
12-
$extras = $event->getComposer()->getPackage()->getExtra();
18+
$extra = $event->getComposer()->getPackage()->getExtra();
19+
20+
static::validateConfiguration($extra);
1321

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);
1634
}
1735

18-
$configs = $extras['css-compiler'];
36+
$processor->saveOutput();
37+
}
1938

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.');
2243
}
2344

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.');
2647
}
2748

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)) {
3251
throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
3352
}
3453

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!');
3759
}
38-
39-
$processor->processFiles(isset($config['format']) ? $config['format'] : 'compact');
4060
}
41-
42-
$processor->saveOutput();
4361
}
4462
}

0 commit comments

Comments
 (0)