55use BlameButton \LaravelDockerBuilder \Commands \Choices \ArtisanOptimize ;
66use BlameButton \LaravelDockerBuilder \Commands \Choices \NodeBuildTool ;
77use BlameButton \LaravelDockerBuilder \Commands \Choices \NodePackageManager ;
8+ use BlameButton \LaravelDockerBuilder \Commands \Choices \PhpExtensions ;
89use 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 ;
914use BlameButton \LaravelDockerBuilder \Traits \InteractsWithTwig ;
15+ use Illuminate \Support \Collection ;
1016use Symfony \Component \Console \Input \InputOption ;
1117
1218class DockerGenerateCommand extends BaseCommand
@@ -20,14 +26,27 @@ class DockerGenerateCommand extends BaseCommand
2026 public function handle (): int
2127 {
2228 $ phpVersion = $ this ->getPhpVersion ();
29+ $ phpExtensions = $ this ->getPhpExtensions ($ phpVersion );
2330 $ artisanOptimize = $ this ->getArtisanOptimize ();
24-
2531 $ nodePackageManager = $ this ->getNodePackageManager ();
2632 $ nodeBuildTool = $ nodePackageManager ? $ this ->getNodeBuildTool () : false ;
2733
34+ if ($ this ->option ('detect ' )) {
35+ $ this ->info ('Detected Configuration ' );
36+ }
37+
38+ $ this ->table (['Key ' , 'Value ' ], [
39+ ['PHP version ' , "<comment> $ phpVersion</comment> " ],
40+ ['PHP extensions ' , implode (', ' , $ phpExtensions )],
41+ ['Artisan Optimize ' , '<comment> ' .json_encode ($ artisanOptimize ).'</comment> ' ],
42+ ['Node Package Manager ' , NodePackageManager::name ($ nodePackageManager )],
43+ ['Node Build Tool ' , $ nodePackageManager ? NodeBuildTool::name ($ nodeBuildTool ) : 'None ' ],
44+ ]);
45+
2846 $ dockerfiles = collect ([
2947 'php.dockerfile ' => $ this ->render ('php.dockerfile.twig ' , [
3048 'php_version ' => $ phpVersion ,
49+ 'php_extensions ' => $ phpExtensions ,
3150 'artisan_optimize ' => $ artisanOptimize ,
3251 'node_package_manager ' => $ nodePackageManager ,
3352 'node_build_tool ' => $ nodeBuildTool ,
@@ -65,16 +84,52 @@ private function getPhpVersion(): string
6584 : throw new \InvalidArgumentException ("Invalid value [ $ option] for option [php-version] " );
6685 }
6786
87+ $ detected = app (PhpVersionDetector::class)->detect ();
88+
89+ if ($ this ->option ('detect ' )) {
90+ return $ detected ;
91+ }
92+
6893 return $ this ->choice (
6994 question: 'PHP version ' ,
7095 choices: PhpVersion::values (),
71- default: PhpVersion::v8_2,
96+ default: $ detected ?: PhpVersion::v8_2,
97+ );
98+ }
99+
100+ private function getPhpExtensions (string $ phpVersion ): array
101+ {
102+ $ supportedExtensions = PhpExtensions::values ($ phpVersion );
103+
104+ if ($ option = $ this ->option ('php-extensions ' )) {
105+ return Collection::make (explode (', ' , $ option ))
106+ ->intersect ($ supportedExtensions )
107+ ->toArray ();
108+ }
109+
110+ $ detected = app (PhpExtensionsDetector::class, ['supportedExtensions ' => $ supportedExtensions ])->detect ();
111+
112+ if ($ this ->option ('detect ' )) {
113+ $ detected = explode (', ' , $ detected );
114+
115+ foreach ($ detected as $ key => $ value ) {
116+ $ detected [$ key ] = $ supportedExtensions [$ value ];
117+ }
118+
119+ return $ detected ;
120+ }
121+
122+ return $ this ->choice (
123+ question: 'PHP extensions ' ,
124+ choices: $ supportedExtensions ,
125+ default: $ detected ,
126+ multiple: true ,
72127 );
73128 }
74129
75130 public function getArtisanOptimize (): bool
76131 {
77- if ($ this ->option ('optimize ' )) {
132+ if ($ this ->option ('optimize ' ) || $ this -> option ( ' detect ' ) ) {
78133 return true ;
79134 }
80135
@@ -95,10 +150,16 @@ private function getNodePackageManager(): string|false
95150 : throw new \InvalidArgumentException ("Invalid value [ $ option] for option [node-package-manager] " );
96151 }
97152
153+ $ detected = app (NodePackageManagerDetector::class)->detect ();
154+
155+ if ($ this ->option ('detect ' )) {
156+ return $ detected ;
157+ }
158+
98159 return $ this ->optionalChoice (
99160 question: 'Which Node package manager do you use? ' ,
100161 choices: NodePackageManager::values (),
101- default: NodePackageManager::NPM ,
162+ default: $ detected ?: NodePackageManager::NPM ,
102163 );
103164 }
104165
@@ -110,22 +171,40 @@ private function getNodeBuildTool(): string
110171 : throw new \InvalidArgumentException ("Invalid value [ $ option] for option [node-build-tool] " );
111172 }
112173
174+ $ detected = app (NodeBuildToolDetector::class)->detect ();
175+
176+ if ($ this ->option ('detect ' )) {
177+ return $ detected ;
178+ }
179+
113180 return $ this ->choice (
114181 question: 'Which Node build tool do you use? ' ,
115182 choices: NodeBuildTool::values (),
116- default: NodeBuildTool::VITE ,
183+ default: $ detected ?: NodeBuildTool::VITE ,
117184 );
118185 }
119186
120187 protected function getOptions (): array
121188 {
122189 return [
190+ new InputOption (
191+ name: 'detect ' ,
192+ shortcut: 'd ' ,
193+ mode: InputOption::VALUE_NONE ,
194+ description: 'Detect project requirements '
195+ ),
123196 new InputOption (
124197 name: 'php-version ' ,
125198 shortcut: 'p ' ,
126199 mode: InputOption::VALUE_REQUIRED ,
127200 description: sprintf ('PHP version (supported: %s) ' , implode (', ' , PhpVersion::values ())),
128201 ),
202+ new InputOption (
203+ name: 'php-extensions ' ,
204+ shortcut: 'e ' ,
205+ mode: InputOption::VALUE_REQUIRED ,
206+ description: sprintf ('PHP extensions (supported: %s) ' , implode (', ' , PhpExtensions::values ())),
207+ ),
129208 new InputOption (
130209 name: 'optimize ' ,
131210 shortcut: 'o ' ,
0 commit comments