1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EM\CssCompiler; |
4
|
|
|
|
5
|
|
|
use Composer\Script\Event; |
6
|
|
|
use EM\CssCompiler\Processor\Processor; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @see ScriptHandlerTest |
10
|
|
|
* |
11
|
|
|
* @since 0.1 |
12
|
|
|
*/ |
13
|
|
|
class ScriptHandler |
14
|
|
|
{ |
15
|
|
|
const CONFIG_MAIN_KEY = 'css-compiler'; |
16
|
|
|
const OPTION_KEY_INPUT = 'input'; |
17
|
|
|
const OPTION_KEY_OUTPUT = 'output'; |
18
|
|
|
const OPTION_KEY_FORMATTER = 'format'; |
19
|
|
|
const DEFAULT_OPTION_FORMATTER = 'compact'; |
20
|
|
|
protected static $mandatoryOptions = [ |
21
|
|
|
self::OPTION_KEY_INPUT, |
22
|
|
|
self::OPTION_KEY_OUTPUT |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Event $event |
27
|
|
|
* |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
*/ |
30
|
1 |
|
public static function generateCSS(Event $event) |
31
|
|
|
{ |
32
|
1 |
|
$extra = $event->getComposer()->getPackage()->getExtra(); |
33
|
1 |
|
static::validateConfiguration($extra); |
34
|
|
|
|
35
|
1 |
|
$processor = new Processor($event->getIO()); |
36
|
|
|
|
37
|
1 |
|
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) { |
38
|
1 |
|
foreach ($config[static::OPTION_KEY_INPUT] as $inputSource) { |
39
|
1 |
|
$processor->attachFiles( |
40
|
1 |
|
static::resolvePath($inputSource, getcwd()), |
41
|
1 |
|
static::resolvePath($config[static::OPTION_KEY_OUTPUT], getcwd()) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER; |
46
|
1 |
|
$processor->processFiles($formatter); |
47
|
|
|
} |
48
|
1 |
|
$processor->saveOutput(); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $path |
53
|
|
|
* @param string $prefix |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
1 |
|
protected static function resolvePath($path, $prefix) |
58
|
|
|
{ |
59
|
1 |
|
return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $config |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
*/ |
68
|
6 |
|
protected static function validateConfiguration(array $config) |
69
|
|
|
{ |
70
|
6 |
|
if (empty($config[static::CONFIG_MAIN_KEY])) { |
71
|
2 |
|
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting'); |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
if (!is_array($config[static::CONFIG_MAIN_KEY])) { |
75
|
1 |
|
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects'); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
foreach ($config[static::CONFIG_MAIN_KEY] as $options) { |
79
|
3 |
|
if (!is_array($options)) { |
80
|
1 |
|
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array'); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
static::validateMandatoryOptions($options); |
84
|
|
|
} |
85
|
2 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $options |
89
|
|
|
* |
90
|
|
|
* @throws \InvalidArgumentException |
91
|
|
|
*/ |
92
|
7 |
|
protected static function validateMandatoryOptions(array $options) |
93
|
|
|
{ |
94
|
7 |
|
foreach (static::$mandatoryOptions as $optionIndex) { |
95
|
7 |
|
if (!isset($options[$optionIndex])) { |
96
|
1 |
|
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} is required!"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
switch ($optionIndex) { |
100
|
6 |
View Code Duplication |
case static::OPTION_KEY_INPUT: |
|
|
|
|
101
|
6 |
|
if (!is_array($options[$optionIndex])) { |
102
|
2 |
|
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} should be array!"); |
103
|
|
|
} |
104
|
4 |
|
break; |
105
|
4 |
View Code Duplication |
case static::OPTION_KEY_OUTPUT: |
|
|
|
|
106
|
4 |
|
if (!is_string($options[$optionIndex])) { |
107
|
1 |
|
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} should string!"); |
108
|
|
|
} |
109
|
4 |
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
3 |
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.