@@ -18,11 +18,13 @@ class ScriptHandler
18
18
const OPTION_KEY_FORMATTER = 'format ' ;
19
19
const DEFAULT_OPTION_FORMATTER = 'compact ' ;
20
20
protected static $ mandatoryOptions = [
21
- self ::OPTION_KEY_INPUT ,
22
- self ::OPTION_KEY_OUTPUT
21
+ self ::OPTION_KEY_INPUT => ' array ' ,
22
+ self ::OPTION_KEY_OUTPUT => ' string '
23
23
];
24
24
25
25
/**
26
+ * @api
27
+ *
26
28
* @param Event $event
27
29
*
28
30
* @throws \InvalidArgumentException
@@ -34,20 +36,26 @@ public static function generateCSS(Event $event)
34
36
35
37
$ processor = new Processor ($ event ->getIO ());
36
38
37
- foreach ($ extra [static ::CONFIG_MAIN_KEY ] as $ config ) {
38
- foreach ($ config [static ::OPTION_KEY_INPUT ] as $ inputSource ) {
39
+ foreach ($ extra [static ::CONFIG_MAIN_KEY ] as $ options ) {
40
+ foreach ($ options [static ::OPTION_KEY_INPUT ] as $ inputSource ) {
39
41
$ processor ->attachFiles (
40
42
static ::resolvePath ($ inputSource , getcwd ()),
41
- static ::resolvePath ($ config [static ::OPTION_KEY_OUTPUT ], getcwd ())
43
+ static ::resolvePath ($ options [static ::OPTION_KEY_OUTPUT ], getcwd ())
42
44
);
43
45
}
44
46
45
- $ formatter = isset ( $ config [ static ::OPTION_KEY_FORMATTER ] ) ? $ config [static ::OPTION_KEY_FORMATTER ] : static ::DEFAULT_OPTION_FORMATTER ;
47
+ $ formatter = array_key_exists ( static ::OPTION_KEY_FORMATTER , $ options ) ? $ options [static ::OPTION_KEY_FORMATTER ] : static ::DEFAULT_OPTION_FORMATTER ;
46
48
$ processor ->processFiles ($ formatter );
47
49
}
48
50
$ processor ->saveOutput ();
49
51
}
50
52
53
+ /**
54
+ * @param string $path
55
+ * @param string $prefix
56
+ *
57
+ * @return string
58
+ */
51
59
protected static function resolvePath ($ path , $ prefix )
52
60
{
53
61
return '/ ' === substr ($ path , 0 , 1 ) ? $ path : "{$ prefix }/ {$ path }" ;
@@ -56,92 +64,44 @@ protected static function resolvePath($path, $prefix)
56
64
/**
57
65
* @param array $config
58
66
*
59
- * @return bool
60
67
* @throws \InvalidArgumentException
61
68
*/
62
69
protected static function validateConfiguration (array $ config )
63
70
{
64
- if (empty ( $ config [ static ::CONFIG_MAIN_KEY ] )) {
71
+ if (! array_key_exists ( static ::CONFIG_MAIN_KEY , $ config )) {
65
72
throw new \InvalidArgumentException ('compiler should needs to be configured through the extra.css-compiler setting ' );
66
73
}
67
74
68
75
if (!is_array ($ config [static ::CONFIG_MAIN_KEY ])) {
69
- throw new \InvalidArgumentException ('the extra.css-compiler setting must be an array of objects ' );
76
+ throw new \InvalidArgumentException ('the extra. ' . static :: CONFIG_MAIN_KEY . ' setting must be an array of objects ' );
70
77
}
71
78
72
- return static ::validateOptions ($ config [static ::CONFIG_MAIN_KEY ]);
73
- }
74
-
75
- /**
76
- * @param array $config
77
- *
78
- * @return bool
79
- * @throws \InvalidArgumentException
80
- */
81
- protected static function validateOptions (array $ config )
82
- {
83
- foreach ($ config as $ option ) {
84
- if (!is_array ($ option )) {
85
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. " . static ::OPTION_KEY_INPUT . ' array ' );
79
+ foreach ($ config [static ::CONFIG_MAIN_KEY ] as $ index => $ options ) {
80
+ if (!is_array ($ options )) {
81
+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index] should be an array " );
86
82
}
87
83
88
- static ::validateMandatoryOptions ($ option );
84
+ static ::validateMandatoryOptions ($ options , $ index );
89
85
}
90
-
91
- return true ;
92
86
}
93
87
94
88
/**
95
- * @param array $config
89
+ * @param array $options
90
+ * @param int $index
96
91
*
97
- * @return bool
98
92
* @throws \InvalidArgumentException
99
93
*/
100
- protected static function validateMandatoryOptions (array $ config )
94
+ protected static function validateMandatoryOptions (array $ options , $ index )
101
95
{
102
- foreach (static ::$ mandatoryOptions as $ option ) {
103
- if (empty ( $ config [ $ option ] )) {
104
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. {$ option } is required! " );
96
+ foreach (static ::$ mandatoryOptions as $ optionIndex => $ type ) {
97
+ if (! array_key_exists ( $ optionIndex , $ options )) {
98
+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index ]. {$ optionIndex } is required! " );
105
99
}
106
100
107
- switch ($ option ) {
108
- case static ::OPTION_KEY_INPUT :
109
- static ::validateIsArray ($ config [$ option ]);
110
- break ;
111
- case static ::OPTION_KEY_OUTPUT :
112
- static ::validateIsString ($ config [$ option ]);
113
- break ;
101
+ $ callable = "is_ {$ type }" ;
102
+ if (!$ callable ($ options [$ optionIndex ])) {
103
+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index]. {$ optionIndex } should be {$ type }! " );
114
104
}
115
105
}
116
-
117
- return true ;
118
- }
119
-
120
- /**
121
- * @param array $option
122
- *
123
- * @return bool
124
- */
125
- protected static function validateIsArray ($ option )
126
- {
127
- if (!is_array ($ option )) {
128
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . '[] ' . static ::OPTION_KEY_INPUT . ' should be array! ' );
129
- }
130
-
131
- return true ;
132
- }
133
-
134
- /**
135
- * @param string $option
136
- *
137
- * @return bool
138
- */
139
- protected static function validateIsString ($ option )
140
- {
141
- if (!is_string ($ option )) {
142
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . '[] ' . static ::OPTION_KEY_OUTPUT . ' should string! ' );
143
- }
144
-
145
- return true ;
146
106
}
147
107
}
0 commit comments