1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EM\CssCompiler; |
4
|
|
|
|
5
|
|
|
use Composer\Script\Event; |
6
|
|
|
use EM\CssCompiler\Processor\Processor; |
7
|
|
|
|
8
|
|
|
class ScriptHandler |
9
|
|
|
{ |
10
|
|
|
const CONFIG_MAIN_KEY = 'css-compiler'; |
11
|
|
|
const OPTION_KEY_INPUT = 'input'; |
12
|
|
|
const OPTION_KEY_OUTPUT = 'output'; |
13
|
|
|
const OPTION_KEY_FORMATTER = 'format'; |
14
|
|
|
const DEFAULT_OPTION_FORMATTER = 'compact'; |
15
|
|
|
protected static $mandatoryOptions = [ |
16
|
|
|
self::OPTION_KEY_INPUT, |
17
|
|
|
self::OPTION_KEY_OUTPUT |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Event $event |
22
|
|
|
* |
23
|
|
|
* @throws \InvalidArgumentException |
24
|
|
|
*/ |
25
|
|
|
public static function generateCSS(Event $event) |
26
|
|
|
{ |
27
|
|
|
$extra = $event->getComposer()->getPackage()->getExtra(); |
28
|
|
|
static::validateConfiguration($extra); |
29
|
|
|
|
30
|
|
|
$processor = new Processor($event->getIO()); |
31
|
|
|
$currentDirectory = getcwd(); |
32
|
|
|
|
33
|
|
|
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) { |
34
|
|
|
foreach ($config[static::OPTION_KEY_INPUT] as $value) { |
35
|
|
|
$processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER; |
39
|
|
|
|
40
|
|
|
$processor->processFiles($formatter); |
41
|
|
|
} |
42
|
|
|
$processor->saveOutput(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $config |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
protected static function validateConfiguration(array $config) |
52
|
|
|
{ |
53
|
|
|
if (empty($config[static::CONFIG_MAIN_KEY])) { |
54
|
|
|
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!is_array($config[static::CONFIG_MAIN_KEY])) { |
58
|
|
|
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($config[static::CONFIG_MAIN_KEY] as $index => $el) { |
62
|
|
|
if (!is_array($el)) { |
63
|
|
|
throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
static::validateOptions($el); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $config |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
* @throws \InvalidArgumentException |
77
|
|
|
*/ |
78
|
|
|
protected static function validateOptions(array $config) |
79
|
|
|
{ |
80
|
|
|
foreach (static::$mandatoryOptions as $option) { |
81
|
|
|
if (empty($config[$option])) { |
82
|
|
|
throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!"); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
if (!is_array($config[static::OPTION_KEY_INPUT])) { |
86
|
|
|
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!'); |
87
|
|
|
} |
88
|
|
|
if (!is_string($config[static::OPTION_KEY_OUTPUT])) { |
89
|
|
|
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|