1717use Humbug \PhpScoper \Console \Configuration ;
1818use Humbug \PhpScoper \Handler \HandleAddPrefix ;
1919use Humbug \PhpScoper \Logger \ConsoleLogger ;
20- use InvalidArgumentException ;
21- use Symfony \Component \Console \Command \Command ;
2220use Symfony \Component \Console \Exception \RuntimeException ;
2321use Symfony \Component \Console \Input \InputArgument ;
2422use Symfony \Component \Console \Input \InputInterface ;
2523use Symfony \Component \Console \Input \InputOption ;
24+ use Symfony \Component \Console \Input \StringInput ;
2625use Symfony \Component \Console \Output \OutputInterface ;
2726use Symfony \Component \Console \Style \OutputStyle ;
2827use Symfony \Component \Console \Style \SymfonyStyle ;
2928use Symfony \Component \Filesystem \Filesystem ;
3029use Throwable ;
3130
32- final class AddPrefixCommand extends Command
31+ final class AddPrefixCommand extends BaseCommand
3332{
3433 private const PATH_ARG = 'paths ' ;
3534 private const PREFIX_OPT = 'prefix ' ;
@@ -39,7 +38,6 @@ final class AddPrefixCommand extends Command
3938 private const CONFIG_FILE_OPT = 'config ' ;
4039 private const CONFIG_FILE_DEFAULT = 'scoper.inc.php ' ;
4140 private const NO_CONFIG_OPT = 'no-config ' ;
42- private const WORKING_DIR_OPT = 'working-dir ' ;
4341
4442 private $ fileSystem ;
4543 private $ handle ;
@@ -60,6 +58,8 @@ public function __construct(Filesystem $fileSystem, HandleAddPrefix $handle)
6058 */
6159 protected function configure (): void
6260 {
61+ parent ::configure ();
62+
6363 $ this
6464 ->setName ('add-prefix ' )
6565 ->setDescription ('Goes through all the PHP files found in the given paths to apply the given prefix to namespaces & FQNs. ' )
@@ -113,13 +113,6 @@ protected function configure(): void
113113 ),
114114 null
115115 )
116- ->addOption (
117- self ::WORKING_DIR_OPT ,
118- 'd ' ,
119- InputOption::VALUE_REQUIRED ,
120- 'If specified, use the given directory as working directory. ' ,
121- null
122- )
123116 ;
124117 }
125118
@@ -137,7 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
137130 $ this ->validatePaths ($ input );
138131 $ this ->validateOutputDir ($ input , $ io );
139132
140- $ config = $ this ->retrieveConfig ($ input , $ io );
133+ $ config = $ this ->retrieveConfig ($ input , $ output , $ io );
141134
142135 $ logger = new ConsoleLogger (
143136 $ this ->getApplication (),
@@ -173,32 +166,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
173166 return 0 ;
174167 }
175168
176- private function changeWorkingDirectory (InputInterface $ input ): void
177- {
178- $ workingDir = $ input ->getOption (self ::WORKING_DIR_OPT );
179-
180- if (null !== $ workingDir ) {
181- if (false === file_exists ($ workingDir )) {
182- throw new InvalidArgumentException (
183- sprintf (
184- 'Could not change the working directory to "%s": directory does not exists. ' ,
185- $ workingDir
186- )
187- );
188- }
189-
190- if (false === chdir ($ workingDir )) {
191- throw new RuntimeException (
192- sprintf (
193- 'Failed to change the working directory to "%s" from "%s". ' ,
194- $ workingDir ,
195- getcwd ()
196- )
197- );
198- }
199- }
200- }
201-
202169 private function validatePrefix (InputInterface $ input ): void
203170 {
204171 $ prefix = $ input ->getOption (self ::PREFIX_OPT );
@@ -306,7 +273,7 @@ private function validateOutputDir(InputInterface $input, OutputStyle $io): void
306273 }
307274 }
308275
309- private function retrieveConfig (InputInterface $ input , OutputStyle $ io ): Configuration
276+ private function retrieveConfig (InputInterface $ input , OutputInterface $ output , OutputStyle $ io ): Configuration
310277 {
311278 if ($ input ->getOption (self ::NO_CONFIG_OPT )) {
312279 $ io ->writeln (
@@ -323,6 +290,13 @@ private function retrieveConfig(InputInterface $input, OutputStyle $io): Configu
323290 $ configFile = $ this ->makeAbsolutePath (self ::CONFIG_FILE_DEFAULT );
324291
325292 if (false === file_exists ($ configFile )) {
293+ $ initCommand = $ this ->getApplication ()->find ('init ' );
294+
295+ $ initInput = new StringInput ('' );
296+ $ initInput ->setInteractive ($ input ->isInteractive ());
297+
298+ $ initCommand ->run ($ initInput , $ output );
299+
326300 $ io ->writeln (
327301 sprintf (
328302 'Config file "<comment>%s</comment>" not found. Skipping. ' ,
0 commit comments