1 | <?php |
||
12 | class Builder |
||
13 | { |
||
14 | /** @var array */ |
||
15 | protected $questions = []; |
||
16 | |||
17 | /** @var array */ |
||
18 | protected $config = []; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $envFile; |
||
22 | |||
23 | /** @var EnvLoader */ |
||
24 | protected $envLoader; |
||
25 | |||
26 | /** @var EnvWriter */ |
||
27 | protected $envWriter; |
||
28 | |||
29 | /** @var IO */ |
||
30 | protected $io; |
||
31 | |||
32 | /** @var array */ |
||
33 | protected $answers = []; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Application constructor. |
||
38 | * |
||
39 | * This builds with the CLImateInterface as a default, but this |
||
40 | * can be overridden by the setIOHandler() method. |
||
41 | * |
||
42 | * @throws ConfigurationException |
||
43 | * @param string $envFile |
||
44 | * @param array $config |
||
45 | */ |
||
46 | 34 | public function __construct(string $envFile, array $config = []) |
|
61 | |||
62 | /** |
||
63 | * Set some of the optional config elements. |
||
64 | * |
||
65 | * @throws ConfigurationException |
||
66 | * @param array $config |
||
67 | */ |
||
68 | 34 | public function setConfig(array $config) |
|
69 | { |
||
70 | 34 | foreach ($config as $key => $value) { |
|
71 | switch ($key) { |
||
72 | 20 | case 'verbose': |
|
73 | 18 | case 'loadEnv': |
|
74 | 18 | if (!is_bool($value)) { |
|
75 | 2 | throw new ConfigurationException($key, 'This should be a boolean value.'); |
|
76 | } |
||
77 | |||
78 | 16 | $this->config[$key] = $value; |
|
79 | 16 | break; |
|
80 | |||
81 | default: |
||
82 | 18 | throw new ConfigurationException($key, 'This is an unknown configuration option.'); |
|
83 | } |
||
84 | } |
||
85 | 34 | } |
|
86 | |||
87 | /** |
||
88 | * Get the Builder's configuration options. |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | 2 | public function getConfig() |
|
96 | |||
97 | /** |
||
98 | * Set the EnvLoader. |
||
99 | * |
||
100 | * @param EnvLoader $envLoader |
||
101 | */ |
||
102 | 34 | public function setEnvLoader(EnvLoader $envLoader) |
|
106 | |||
107 | /** |
||
108 | * Add in an IOHanderInterface object. |
||
109 | * |
||
110 | * @param IOHandlerInterface $ioHandler |
||
111 | */ |
||
112 | 14 | public function setIOHandler(IOHandlerInterface $ioHandler) |
|
116 | |||
117 | /** |
||
118 | * Set the IO. |
||
119 | * |
||
120 | * @param IO $io |
||
121 | */ |
||
122 | 34 | public function setIO(IO $io) |
|
126 | |||
127 | /** |
||
128 | * Override the EnvWriter. |
||
129 | * |
||
130 | * @param EnvWriter $envWriter |
||
131 | */ |
||
132 | 34 | public function setEnvWriter(EnvWriter $envWriter) |
|
136 | |||
137 | /** |
||
138 | * Run the builder |
||
139 | * |
||
140 | * @throws AskException |
||
141 | * @return array |
||
142 | */ |
||
143 | 12 | public function run() |
|
160 | |||
161 | /** |
||
162 | * Write the answers to a file. |
||
163 | * |
||
164 | * @return bool The status of the write operation. |
||
165 | */ |
||
166 | 4 | public function write() |
|
177 | |||
178 | /** |
||
179 | * Set a question to ask the user. |
||
180 | * |
||
181 | * @param string $name The name of the env variable to read/use |
||
182 | * @param string $prompt The prompt you want to give the user |
||
183 | * @param string $default A default answer, if any. |
||
184 | * @param bool $required Is a response required? |
||
185 | */ |
||
186 | 10 | public function ask($name, $prompt, $default = '', $required = false) |
|
194 | |||
195 | /** |
||
196 | * Return all of the questions. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 2 | public function getQuestions() |
|
204 | } |
||
205 |