22
33namespace Incenteev \ParameterHandler ;
44
5- use Composer \IO \IOInterface ;
65use Composer \Script \Event ;
7- use Symfony \Component \Yaml \Inline ;
8- use Symfony \Component \Yaml \Parser ;
9- use Symfony \Component \Yaml \Yaml ;
106
117class ScriptHandler
128{
@@ -28,169 +24,14 @@ public static function buildParameters(Event $event)
2824 $ configs = array ($ configs );
2925 }
3026
27+ $ processor = new Processor ($ event ->getIO ());
28+
3129 foreach ($ configs as $ config ) {
3230 if (!is_array ($ config )) {
3331 throw new \InvalidArgumentException ('The extra.incenteev-parameters setting must be an array of configuration objects. ' );
3432 }
3533
36- self ::processFile ($ config , $ event ->getIO ());
37- }
38- }
39-
40- private static function processFile (array $ config , IOInterface $ io )
41- {
42- $ config = self ::processConfig ($ config );
43-
44- $ realFile = $ config ['file ' ];
45- $ parameterKey = $ config ['parameter-key ' ];
46-
47- $ exists = is_file ($ realFile );
48-
49- $ yamlParser = new Parser ();
50-
51- $ action = $ exists ? 'Updating ' : 'Creating ' ;
52- $ io ->write (sprintf ('<info>%s the "%s" file</info> ' , $ action , $ realFile ));
53-
54- // Find the expected params
55- $ expectedValues = $ yamlParser ->parse (file_get_contents ($ config ['dist-file ' ]));
56- if (!isset ($ expectedValues [$ parameterKey ])) {
57- throw new \InvalidArgumentException ('The dist file seems invalid. ' );
58- }
59- $ expectedParams = (array ) $ expectedValues [$ parameterKey ];
60-
61- // find the actual params
62- $ actualValues = array ($ parameterKey => array ());
63- if ($ exists ) {
64- $ existingValues = $ yamlParser ->parse (file_get_contents ($ realFile ));
65- if ($ existingValues === null ) {
66- $ existingValues = array ();
67- }
68- if (!is_array ($ existingValues )) {
69- throw new \InvalidArgumentException (sprintf ('The existing "%s" file does not contain an array ' , $ realFile ));
70- }
71- $ actualValues = array_merge ($ actualValues , $ existingValues );
72- }
73-
74- $ actualValues [$ parameterKey ] = self ::processParams ($ config , $ io , $ expectedParams , (array ) $ actualValues [$ parameterKey ]);
75-
76- // Preserve other top-level keys than `$parameterKey` in the file
77- foreach ($ expectedValues as $ key => $ setting ) {
78- if (!array_key_exists ($ key , $ actualValues )) {
79- $ actualValues [$ key ] = $ setting ;
80- }
81- }
82-
83- if (!is_dir ($ dir = dirname ($ realFile ))) {
84- mkdir ($ dir , 0755 , true );
85- }
86-
87- file_put_contents ($ realFile , "# This file is auto-generated during the composer install \n" . Yaml::dump ($ actualValues , 99 ));
88- }
89-
90- private static function processConfig (array $ config )
91- {
92- if (empty ($ config ['file ' ])) {
93- throw new \InvalidArgumentException ('The extra.incenteev-parameters.file setting is required to use this script handler. ' );
94- }
95-
96- if (empty ($ config ['dist-file ' ])) {
97- $ config ['dist-file ' ] = $ config ['file ' ].'.dist ' ;
34+ $ processor ->processFile ($ config );
9835 }
99-
100- if (!is_file ($ config ['dist-file ' ])) {
101- throw new \InvalidArgumentException (sprintf ('The dist file "%s" does not exist. Check your dist-file config or create it. ' , $ config ['dist-file ' ]));
102- }
103-
104- if (empty ($ config ['parameter-key ' ])) {
105- $ config ['parameter-key ' ] = 'parameters ' ;
106- }
107-
108- return $ config ;
109- }
110-
111- private static function processParams (array $ config , IOInterface $ io , $ expectedParams , $ actualParams )
112- {
113- // Grab values for parameters that were renamed
114- $ renameMap = empty ($ config ['rename-map ' ]) ? array () : (array ) $ config ['rename-map ' ];
115- $ actualParams = array_replace ($ actualParams , self ::processRenamedValues ($ renameMap , $ actualParams ));
116-
117- $ keepOutdatedParams = false ;
118- if (isset ($ config ['keep-outdated ' ])) {
119- $ keepOutdatedParams = (boolean ) $ config ['keep-outdated ' ];
120- }
121-
122- if (!$ keepOutdatedParams ) {
123- // Remove the outdated params
124- foreach ($ actualParams as $ key => $ value ) {
125- if (!array_key_exists ($ key , $ expectedParams )) {
126- unset($ actualParams [$ key ]);
127- }
128- }
129- }
130-
131- $ envMap = empty ($ config ['env-map ' ]) ? array () : (array ) $ config ['env-map ' ];
132-
133- // Add the params coming from the environment values
134- $ actualParams = array_replace ($ actualParams , self ::getEnvValues ($ envMap ));
135-
136- return self ::getParams ($ io , $ expectedParams , $ actualParams );
137- }
138-
139- private static function getEnvValues (array $ envMap )
140- {
141- $ params = array ();
142- foreach ($ envMap as $ param => $ env ) {
143- $ value = getenv ($ env );
144- if ($ value ) {
145- $ params [$ param ] = Inline::parse ($ value );
146- }
147- }
148-
149- return $ params ;
150- }
151-
152- private static function processRenamedValues (array $ renameMap , array $ actualParams )
153- {
154- foreach ($ renameMap as $ param => $ oldParam ) {
155- if (array_key_exists ($ param , $ actualParams )) {
156- continue ;
157- }
158-
159- if (!array_key_exists ($ oldParam , $ actualParams )) {
160- continue ;
161- }
162-
163- $ actualParams [$ param ] = $ actualParams [$ oldParam ];
164- }
165-
166- return $ actualParams ;
167- }
168-
169- private static function getParams (IOInterface $ io , array $ expectedParams , array $ actualParams )
170- {
171- // Simply use the expectedParams value as default for the missing params.
172- if (!$ io ->isInteractive ()) {
173- return array_replace ($ expectedParams , $ actualParams );
174- }
175-
176- $ isStarted = false ;
177-
178- foreach ($ expectedParams as $ key => $ message ) {
179- if (array_key_exists ($ key , $ actualParams )) {
180- continue ;
181- }
182-
183- if (!$ isStarted ) {
184- $ isStarted = true ;
185- $ io ->write ('<comment>Some parameters are missing. Please provide them.</comment> ' );
186- }
187-
188- $ default = Inline::dump ($ message );
189- $ value = $ io ->ask (sprintf ('<question>%s</question> (<comment>%s</comment>): ' , $ key , $ default ), $ default );
190-
191- $ actualParams [$ key ] = Inline::parse ($ value );
192- }
193-
194- return $ actualParams ;
19536 }
19637}
0 commit comments