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
|
|
|
public static function generateCSS(Event $event) |
31
|
|
|
{ |
32
|
|
|
$extra = $event->getComposer()->getPackage()->getExtra(); |
33
|
|
|
static::validateConfiguration($extra); |
34
|
|
|
|
35
|
|
|
$processor = new Processor($event->getIO()); |
36
|
|
|
$currentDirectory = getcwd(); |
37
|
|
|
|
38
|
|
|
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) { |
39
|
|
|
foreach ($config[static::OPTION_KEY_INPUT] as $value) { |
40
|
|
|
$processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER; |
44
|
|
|
|
45
|
|
|
$processor->processFiles($formatter); |
46
|
|
|
} |
47
|
|
|
$processor->saveOutput(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $config |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
protected static function validateConfiguration(array $config) |
57
|
|
|
{ |
58
|
|
|
if (empty($config[static::CONFIG_MAIN_KEY])) { |
59
|
|
|
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!is_array($config[static::CONFIG_MAIN_KEY])) { |
63
|
|
|
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return static::validateOptions($config); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $config |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
* @throws \InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
protected static function validateOptions(array $config) |
76
|
|
|
{ |
77
|
|
|
foreach ($config[static::CONFIG_MAIN_KEY] as $index => $option) { |
78
|
|
|
if (!is_array($option)) { |
79
|
|
|
throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
static::validateMandatoryOptions($option); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $config |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
* @throws \InvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
protected static function validateMandatoryOptions(array $config) |
96
|
|
|
{ |
97
|
|
|
foreach (static::$mandatoryOptions as $option) { |
98
|
|
|
if (empty($config[$option])) { |
99
|
|
|
throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!"); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
static::validateInputOption($config); |
103
|
|
|
static::validateOutputOption($config); |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
/** |
108
|
|
|
* @param array $config |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
protected static function validateInputOption(array $config) |
113
|
|
|
{ |
114
|
|
|
if (!is_array($config[static::OPTION_KEY_INPUT])) { |
115
|
|
|
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $config |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
protected static function validateOutputOption(array $config) |
127
|
|
|
{ |
128
|
|
|
if (!is_string($config[static::OPTION_KEY_OUTPUT])) { |
129
|
|
|
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|