2
2
3
3
namespace BlameButton \LaravelDockerBuilder \Commands ;
4
4
5
- use BlameButton \LaravelDockerBuilder \Commands \Choices \ ArtisanOptimize ;
6
- use BlameButton \LaravelDockerBuilder \Commands \Choices \NodeBuildTool ;
7
- use BlameButton \LaravelDockerBuilder \Commands \Choices \NodePackageManager ;
8
- use BlameButton \LaravelDockerBuilder \Commands \Choices \PhpExtensions ;
9
- use BlameButton \LaravelDockerBuilder \Commands \Choices \PhpVersion ;
10
- use BlameButton \LaravelDockerBuilder \Detector \ NodeBuildToolDetector ;
11
- use BlameButton \LaravelDockerBuilder \Detector \ NodePackageManagerDetector ;
12
- use BlameButton \LaravelDockerBuilder \Detector \ PhpExtensionsDetector ;
13
- use BlameButton \LaravelDockerBuilder \Detector \ PhpVersionDetector ;
5
+ use BlameButton \LaravelDockerBuilder \Commands \GenerateQuestions \ ArtisanOptimizeQuestion ;
6
+ use BlameButton \LaravelDockerBuilder \Commands \GenerateQuestions \ Choices \NodeBuildTool ;
7
+ use BlameButton \LaravelDockerBuilder \Commands \GenerateQuestions \ Choices \NodePackageManager ;
8
+ use BlameButton \LaravelDockerBuilder \Commands \GenerateQuestions \ Choices \PhpExtensions ;
9
+ use BlameButton \LaravelDockerBuilder \Commands \GenerateQuestions \ Choices \PhpVersion ;
10
+ use BlameButton \LaravelDockerBuilder \Commands \ GenerateQuestions \ NodeBuildToolQuestion ;
11
+ use BlameButton \LaravelDockerBuilder \Commands \ GenerateQuestions \ NodePackageManagerQuestion ;
12
+ use BlameButton \LaravelDockerBuilder \Commands \ GenerateQuestions \ PhpExtensionsQuestion ;
13
+ use BlameButton \LaravelDockerBuilder \Commands \ GenerateQuestions \ PhpVersionQuestion ;
14
14
use BlameButton \LaravelDockerBuilder \Exceptions \InvalidOptionValueException ;
15
15
use BlameButton \LaravelDockerBuilder \Traits \InteractsWithTwig ;
16
16
use Symfony \Component \Console \Input \InputOption ;
@@ -26,11 +26,11 @@ class DockerGenerateCommand extends BaseCommand
26
26
public function handle (): int
27
27
{
28
28
try {
29
- $ phpVersion = $ this -> getPhpVersion ( );
30
- $ phpExtensions = $ this -> getPhpExtensions ( $ phpVersion );
31
- $ artisanOptimize = $ this -> getArtisanOptimize ( );
32
- $ nodePackageManager = $ this -> getNodePackageManager ( );
33
- $ nodeBuildTool = $ nodePackageManager ? $ this -> getNodeBuildTool ( ) : false ;
29
+ $ phpVersion = app (PhpVersionQuestion::class)-> getAnswer ( $ this );
30
+ $ phpExtensions = app (PhpExtensionsQuestion::class)-> getAnswer ( $ this , $ phpVersion );
31
+ $ artisanOptimize = app (ArtisanOptimizeQuestion::class)-> getAnswer ( $ this );
32
+ $ nodePackageManager = app (NodePackageManagerQuestion::class)-> getAnswer ( $ this );
33
+ $ nodeBuildTool = $ nodePackageManager ? app (NodeBuildToolQuestion::class)-> getAnswer ( $ this ) : false ;
34
34
} catch (InvalidOptionValueException $ exception ) {
35
35
$ this ->error ($ exception ->getMessage ());
36
36
@@ -53,21 +53,17 @@ public function handle(): int
53
53
return self ::SUCCESS ;
54
54
}
55
55
56
- $ context = [
56
+ $ this -> saveDockerfiles ( [
57
57
'php_version ' => $ phpVersion ,
58
- 'php_extensions ' => $ phpExtensions ,
58
+ 'php_extensions ' => implode ( ' ' , $ phpExtensions) ,
59
59
'artisan_optimize ' => $ artisanOptimize ,
60
60
'node_package_manager ' => $ nodePackageManager ,
61
61
'node_build_tool ' => $ nodeBuildTool ,
62
- ];
63
-
64
- $ this ->saveDockerfiles ($ context );
62
+ ]);
65
63
$ this ->newLine ();
66
64
67
65
$ command = array_filter ([
68
- 'php ' ,
69
- 'artisan ' ,
70
- 'docker:generate ' ,
66
+ 'php ' , 'artisan ' , 'docker:generate ' ,
71
67
'-n ' , // --no-interaction
72
68
'-p ' .$ phpVersion , // --php-version
73
69
'-e ' .implode (', ' , $ phpExtensions ), // --php-extensions
@@ -82,155 +78,6 @@ public function handle(): int
82
78
return self ::SUCCESS ;
83
79
}
84
80
85
- /**
86
- * Get the PHP version, either by detecting it from the "composer.json",
87
- * from the "php-version" option, or asking the user.
88
- *
89
- * @return string
90
- *
91
- * @throws InvalidOptionValueException when an unsupported PHP version is passed
92
- */
93
- private function getPhpVersion (): string
94
- {
95
- if ($ option = $ this ->option ('php-version ' )) {
96
- return in_array ($ option , PhpVersion::values ())
97
- ? $ option
98
- : throw new InvalidOptionValueException ("Invalid value [ $ option] for option [php-version] " );
99
- }
100
-
101
- $ detected = app (PhpVersionDetector::class)->detect ();
102
-
103
- if ($ this ->option ('detect ' )) {
104
- return $ detected ;
105
- }
106
-
107
- return $ this ->choice (
108
- question: 'PHP version ' ,
109
- choices: PhpVersion::values (),
110
- default: $ detected ?: PhpVersion::v8_2,
111
- );
112
- }
113
-
114
- /**
115
- * Get the PHP extensions, either by detecting them from the application's configuration,
116
- * from the "php-extensions" option, or asking the user.
117
- *
118
- * @param string $phpVersion
119
- * @return array
120
- *
121
- * @throws InvalidOptionValueException when an unsupported extension is passed
122
- */
123
- private function getPhpExtensions (string $ phpVersion ): array
124
- {
125
- $ supportedExtensions = PhpExtensions::values ($ phpVersion );
126
-
127
- if ($ option = $ this ->option ('php-extensions ' )) {
128
- $ extensions = explode (', ' , $ option );
129
-
130
- foreach ($ extensions as $ extension ) {
131
- if (in_array ($ extension , $ supportedExtensions )) {
132
- continue ;
133
- }
134
-
135
- throw new InvalidOptionValueException ("Extension [ $ extension] is not supported. " );
136
- }
137
-
138
- return array_intersect ($ extensions , $ supportedExtensions );
139
- }
140
-
141
- $ detected = app (PhpExtensionsDetector::class, ['supportedExtensions ' => $ supportedExtensions ])->detect ();
142
-
143
- if ($ this ->option ('detect ' )) {
144
- $ detected = explode (', ' , $ detected );
145
-
146
- foreach ($ detected as $ key => $ value ) {
147
- $ detected [$ key ] = $ supportedExtensions [$ value ];
148
- }
149
-
150
- return $ detected ;
151
- }
152
-
153
- return $ this ->choice (
154
- question: 'PHP extensions ' ,
155
- choices: $ supportedExtensions ,
156
- default: $ detected ,
157
- multiple: true ,
158
- );
159
- }
160
-
161
- public function getArtisanOptimize (): bool
162
- {
163
- if ($ this ->option ('optimize ' ) || $ this ->option ('detect ' )) {
164
- return true ;
165
- }
166
-
167
- $ choice = $ this ->choice (
168
- question: 'Do you want to run "php artisan optimize" when the image boots? ' ,
169
- choices: ArtisanOptimize::values (),
170
- default: ArtisanOptimize::YES ,
171
- );
172
-
173
- return ArtisanOptimize::YES === $ choice ;
174
- }
175
-
176
- /**
177
- * Get the Node Package Manager, either by detecting it from files present (package-lock.json, yarn.lock),
178
- * from the "node-package-manager" option, or asking the user.
179
- *
180
- * @return string|false
181
- *
182
- * @throws InvalidOptionValueException
183
- */
184
- private function getNodePackageManager (): string |false
185
- {
186
- if ($ option = $ this ->option ('node-package-manager ' )) {
187
- return in_array ($ option , NodePackageManager::values ())
188
- ? $ option
189
- : throw new InvalidOptionValueException ("Invalid value [ $ option] for option [node-package-manager] " );
190
- }
191
-
192
- $ detected = app (NodePackageManagerDetector::class)->detect ();
193
-
194
- if ($ this ->option ('detect ' )) {
195
- return $ detected ;
196
- }
197
-
198
- return $ this ->optionalChoice (
199
- question: 'Which Node package manager do you use? ' ,
200
- choices: NodePackageManager::values (),
201
- default: $ detected ?: NodePackageManager::NPM ,
202
- );
203
- }
204
-
205
- /**
206
- * Get the Node Build Tool, either by detecting it from files present (vite.config.js, webpack.mix.js),
207
- * from the "node-build-tool" option, or asking the user.
208
- *
209
- * @return string
210
- *
211
- * @throws InvalidOptionValueException
212
- */
213
- private function getNodeBuildTool (): string
214
- {
215
- if ($ option = $ this ->option ('node-build-tool ' )) {
216
- return in_array ($ option , NodeBuildTool::values ())
217
- ? $ option
218
- : throw new InvalidOptionValueException ("Invalid value [ $ option] for option [node-build-tool] " );
219
- }
220
-
221
- $ detected = app (NodeBuildToolDetector::class)->detect ();
222
-
223
- if ($ this ->option ('detect ' )) {
224
- return $ detected ;
225
- }
226
-
227
- return $ this ->choice (
228
- question: 'Which Node build tool do you use? ' ,
229
- choices: NodeBuildTool::values (),
230
- default: $ detected ?: NodeBuildTool::VITE ,
231
- );
232
- }
233
-
234
81
private function saveDockerfiles (array $ context ): void
235
82
{
236
83
if (! is_dir ($ dir = base_path ('.docker ' ))) {
@@ -281,9 +128,21 @@ protected function getOptions(): array
281
128
new InputOption (
282
129
name: 'optimize ' ,
283
130
shortcut: 'o ' ,
284
- mode: InputOption::VALUE_NONE ,
131
+ mode: InputOption::VALUE_NEGATABLE ,
285
132
description: 'Add "php artisan optimize" to entrypoint ' ,
286
133
),
134
+ new InputOption (
135
+ name: 'opcache ' ,
136
+ mode: InputOption::VALUE_NEGATABLE ,
137
+ description: 'Add "opcache" extension and configure it ' ,
138
+ default: true ,
139
+ ),
140
+ new InputOption (
141
+ name: 'alpine ' ,
142
+ mode: InputOption::VALUE_NEGATABLE ,
143
+ description: 'Use Alpine Linux based images ' ,
144
+ default: true ,
145
+ ),
287
146
new InputOption (
288
147
name: 'node-package-manager ' ,
289
148
shortcut: 'm ' ,
0 commit comments