-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsk.php
194 lines (165 loc) · 4.99 KB
/
Ask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Solire\Install;
use Composer\IO\IOInterface;
use Composer\Script\Event;
use Exception;
use Solire\Lib\Path;
/**
* Prompts questions. Save answers. Write yml files.
*
* @author thansen <thansen@solire.fr>
* @license CC by-nc http://creativecommons.org/licenses/by-nc/3.0/fr/
*/
class Ask
{
private static $alias = [];
/**
* Initialisation.
*
* @param Event $event The composer event
*
* @return void
*/
public static function init(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();
$parameters = [];
if (isset($extra['solire']['parameters'])) {
$parameters = $extra['solire']['parameters'];
}
foreach ($parameters as $path => $data) {
$defaultPath = pathinfo($path, PATHINFO_DIRNAME)
. Path::DS
. pathinfo($path, PATHINFO_FILENAME)
. '.default.'
. pathinfo($path, PATHINFO_EXTENSION)
;
if (!file_exists($defaultPath)) {
$defaultPath = $path;
}
$parameters[$path] = array_merge(
yaml_parse_file($defaultPath),
$data
);
}
$extra['solire']['parameters'] = $parameters;
$event->getComposer()->getPackage()->setExtra($extra);
}
/**
* A composer installation script to define the different configuration
* parameters.
*
* @param Event $event The composer event
*
* @return void
*/
public static function parameters(Event $event)
{
$io = $event->getIO();
$extra = $event->getComposer()->getPackage()->getExtra();
$parameters = [];
if (isset($extra['solire']['parameters'])) {
$parameters = $extra['solire']['parameters'];
}
$configs = yaml_parse_file($extra['solire']['ask']);
foreach ($configs as $path => $questions) {
if (!isset($parameters[$path])) {
$parameters[$path] = [];
}
foreach ($questions as $section => $sectionQuestions) {
if (!isset($parameters[$path][$section])) {
$parameters[$path][$section] = [];
}
foreach ($sectionQuestions as $key => $question) {
$default = null;
if (isset($parameters[$path][$section][$key])) {
$default = $parameters[$path][$section][$key];
}
$parameters[$path][$section][$key] = self::askQuestion(
$io,
$question,
$default
);
}
}
}
$extra['solire']['parameters'] = $parameters;
$event->getComposer()->getPackage()->setExtra($extra);
}
/**
* Pose une question.
*
* @param IOInterface $io The input output interface
* @param array $question The question config
* @param string $default The default value
*
* @return string
*/
private static function askQuestion(
IOInterface $io,
array $question,
$default = null
) {
if (!empty($question['copy'])) {
return self::copyFromAlias($question['copy']);
}
if ($default === null && isset($question['default'])) {
$default = $question['default'];
}
$text = sprintf(
'<question>%s</question> (<comment>%s</comment>): ',
$question['text'],
$default
);
if (!empty($question['hide'])) {
$answer = $io->askAndHideAnswer($text, $default);
} else {
$answer = $io->ask($text, $default);
}
if (!empty($question['alias'])) {
self::$alias[$question['alias']] = $answer;
}
return $answer;
}
private static function saveAlias($alias, $answer)
{
self::$alias[$alias] = $answer;
}
/**
* Copie une valeurs avec un alias.
*
* @param string $alias Alias
*
* @return string
*
* @throws Exception Si un alias n'existe pas
*/
private static function copyFromAlias($alias)
{
if (!isset(self::$alias[$alias])) {
throw new Exception(
sprintf(
'parameters alias [%s] does not exist',
$alias
)
);
}
return self::$alias[$alias];
}
/**
* Write config.
*
* @param Event $event The composer event
*
* @return void
*/
public static function write(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();
$parameters = $extra['solire']['parameters'];
unset($parameters['temp']);
foreach ($parameters as $path => $data) {
yaml_emit_file($path, $data);
}
}
}