2929
3030final class AddPrefixCommand extends Command
3131{
32- /** @internal */
33- const PREFIX_ARG = 'prefix ' ;
3432 /** @internal */
3533 const PATH_ARG = 'paths ' ;
3634 /** @internal */
37- const OUTPUT_DIR = 'output-dir ' ;
35+ const PREFIX_OPT = 'prefix ' ;
36+ /** @internal */
37+ const OUTPUT_DIR_OPT = 'output-dir ' ;
38+ /** @internal */
39+ const FORCE_OPT = 'force ' ;
3840
3941 private $ fileSystem ;
4042 private $ handle ;
@@ -58,23 +60,30 @@ protected function configure()
5860 $ this
5961 ->setName ('add-prefix ' )
6062 ->setDescription ('Goes through all the PHP files found in the given paths to apply the given prefix to namespaces & FQNs. ' )
61- ->addArgument (
62- self ::PREFIX_ARG ,
63- InputArgument::REQUIRED ,
64- 'The namespace prefix to add '
65- )
6663 ->addArgument (
6764 self ::PATH_ARG ,
68- InputArgument::REQUIRED | InputArgument:: IS_ARRAY ,
65+ InputArgument::IS_ARRAY ,
6966 'The path(s) to process. '
7067 )
7168 ->addOption (
72- self ::OUTPUT_DIR ,
69+ self ::PREFIX_OPT ,
70+ 'p ' ,
71+ InputOption::VALUE_REQUIRED ,
72+ 'The namespace prefix to add '
73+ )
74+ ->addOption (
75+ self ::OUTPUT_DIR_OPT ,
7376 'o ' ,
7477 InputOption::VALUE_REQUIRED ,
7578 'The output directory in which the prefixed code will be dumped. ' ,
7679 'build '
7780 )
81+ ->addOption (
82+ self ::FORCE_OPT ,
83+ 'f ' ,
84+ InputOption::VALUE_NONE ,
85+ 'Deletes any existing content in the output directory without any warning '
86+ )
7887 ;
7988 }
8089
@@ -95,15 +104,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
95104 );
96105
97106 $ logger ->outputScopingStart (
98- $ input ->getArgument (self ::PREFIX_ARG ),
107+ $ input ->getOption (self ::PREFIX_OPT ),
99108 $ input ->getArgument (self ::PATH_ARG )
100109 );
101110
102111 try {
103112 $ this ->handle ->__invoke (
104- $ input ->getArgument (self ::PREFIX_ARG ),
113+ $ input ->getOption (self ::PREFIX_OPT ),
105114 $ input ->getArgument (self ::PATH_ARG ),
106- $ input ->getOption (self ::OUTPUT_DIR ),
115+ $ input ->getOption (self ::OUTPUT_DIR_OPT ),
107116 $ logger
108117 );
109118 } catch (Throwable $ throwable ) {
@@ -117,7 +126,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
117126
118127 private function validatePrefix (InputInterface $ input )
119128 {
120- $ prefix = trim ($ input ->getArgument (self ::PREFIX_ARG ));
129+ $ prefix = $ input ->getOption (self ::PREFIX_OPT );
130+
131+ if (null === $ prefix ) {
132+ $ prefix = uniqid ('PhpScoper ' );
133+ } else {
134+ $ prefix = trim ($ prefix );
135+ }
121136
122137 if (1 === preg_match ('/(?<prefix>.*?) \\\\*$/ ' , $ prefix , $ matches )) {
123138 $ prefix = $ matches ['prefix ' ];
@@ -127,12 +142,12 @@ private function validatePrefix(InputInterface $input)
127142 throw new RuntimeException (
128143 sprintf (
129144 'Expected "%s" argument to be a non empty string. ' ,
130- self ::PREFIX_ARG
145+ self ::PREFIX_OPT
131146 )
132147 );
133148 }
134149
135- $ input ->setArgument (self ::PREFIX_ARG , $ prefix );
150+ $ input ->setOption (self ::PREFIX_OPT , $ prefix );
136151 }
137152
138153 private function validatePaths (InputInterface $ input )
@@ -151,28 +166,47 @@ function (string $path) use ($cwd, $fileSystem) {
151166 $ input ->getArgument (self ::PATH_ARG )
152167 );
153168
169+ if (0 === count ($ paths )) {
170+ $ paths [] = $ cwd ;
171+ }
172+
154173 $ input ->setArgument (self ::PATH_ARG , $ paths );
155174 }
156175
157176 private function validateOutputDir (InputInterface $ input , OutputStyle $ io )
158177 {
159- $ outputDir = $ input ->getOption (self ::OUTPUT_DIR );
178+ $ outputDir = $ input ->getOption (self ::OUTPUT_DIR_OPT );
160179
161180 if (false === $ this ->fileSystem ->isAbsolutePath ($ outputDir )) {
162181 $ outputDir = getcwd ().DIRECTORY_SEPARATOR .$ outputDir ;
163182 }
164183
165- $ input ->setOption (self ::OUTPUT_DIR , $ outputDir );
184+ $ input ->setOption (self ::OUTPUT_DIR_OPT , $ outputDir );
166185
167186 if (false === $ this ->fileSystem ->exists ($ outputDir )) {
168187 return ;
169188 }
170189
190+ if (false === is_writable ($ outputDir )) {
191+ throw new RuntimeException (
192+ sprintf (
193+ 'Expected "<comment>%s</comment>" to be writeable. ' ,
194+ $ outputDir
195+ )
196+ );
197+ }
198+
199+ if ($ input ->getOption (self ::FORCE_OPT )) {
200+ $ this ->fileSystem ->remove ($ outputDir );
201+
202+ return ;
203+ }
204+
171205 if (false === is_dir ($ outputDir )) {
172206 $ canDeleteFile = $ io ->confirm (
173207 sprintf (
174- 'Expected "%s " to be a directory but found a file instead. It will be removed, do you wish '
175- .'to proceed? ' ,
208+ 'Expected "<comment>%s</comment> " to be a directory but found a file instead. It will be '
209+ .'removed, do you wish to proceed? ' ,
176210 $ outputDir
177211 ),
178212 false
@@ -183,15 +217,21 @@ private function validateOutputDir(InputInterface $input, OutputStyle $io)
183217 }
184218
185219 $ this ->fileSystem ->remove ($ outputDir );
186- }
187-
188- if (false === is_writable ($ outputDir )) {
189- throw new RuntimeException (
220+ } else {
221+ $ canDeleteFile = $ io ->confirm (
190222 sprintf (
191- 'Expected "%s" to be writeable. ' ,
223+ 'The output directory "<comment>%s</comment>" already exists. Continuing will erase its '
224+ .' content, do you wish to proceed? ' ,
192225 $ outputDir
193- )
226+ ),
227+ false
194228 );
229+
230+ if (false === $ canDeleteFile ) {
231+ return ;
232+ }
233+
234+ $ this ->fileSystem ->remove ($ outputDir );
195235 }
196236 }
197237}
0 commit comments