22
33namespace Kernel243 \Artisan \Commands ;
44
5- use Illuminate \Console \Command ;
65use Illuminate \Support \Str ;
76
8- class File extends Command
7+ class File extends BaseCommand
98{
109 /**
1110 * The name and signature of the console command.
@@ -21,37 +20,27 @@ class File extends Command
2120 */
2221 protected $ description = 'Create a new file ' ;
2322
24- /**
25- * Create a new command instance.
26- *
27- * @return void
28- */
29- public function __construct ()
30- {
31- parent ::__construct ();
32- }
33-
3423 /**
3524 * Execute the console command.
3625 *
37- * @return void
26+ * @return int
3827 */
39- public function handle ()
28+ public function handle (): int
4029 {
4130 if ($ this ->isCorrectFilename ($ this ->argument ('filename ' ))) {
4231 $ extension = $ this ->getExtension ();
4332 $ path = base_path (str_replace ('. ' , '/ ' , $ this ->argument ('filename ' )).'. ' .$ extension );
4433
45- if ($ this ->replaceExistingFile ($ path , 'There is already a file with this name do you want to replace it ? [y/n] ' )) {
34+ if ($ this ->shouldReplaceFile ($ path , 'There is already a file with this name do you want to replace it ? [y/n] ' )) {
4635 $ filename = explode ('. ' , $ this ->argument ('filename ' ));
47-
48- $ this ->createFoldersIfNecessary ($ filename );
49-
36+ $ this ->createFoldersIfNecessary ($ filename , base_path ());
5037 file_put_contents ($ path , '' );
5138 $ this ->info ('File created successfully ' );
5239 }
53- } else
54- $ this ->error ('The filename is not correct. ' );
40+ return self ::SUCCESS ;
41+ }
42+ $ this ->error ('The filename is not correct. ' );
43+ return self ::FAILURE ;
5544 }
5645
5746 /**
@@ -70,23 +59,6 @@ protected function getExtension()
7059 return 'php ' ;
7160 }
7261
73- /**
74- * Create a set of folders if necessary.
75- *
76- * @param $filename
77- * @return void
78- */
79- protected function createFoldersIfNecessary ($ filename )
80- {
81- $ folder = base_path ('' );
82- for ($ i = 0 ; $ i < count ($ filename ) - 1 ; $ i ++) {
83- if (!is_dir ($ folder . '/ ' . $ filename [$ i ])) {
84- mkdir ($ folder . '/ ' . $ filename [$ i ]);
85- }
86- $ folder .= '/ ' . $ filename [$ i ];
87- }
88- }
89-
9062 /**
9163 * Check if the filename is correct.
9264 *
@@ -97,25 +69,4 @@ protected function isCorrectFilename($name)
9769 {
9870 return (bool ) preg_match ('#^[a-zA-Z][a-zA-Z0-9._\-]+$# ' , $ name );
9971 }
100-
101- /**
102- * Check if the filename exists and if it could be replaced.
103- *
104- * @param $filename
105- * @param $question
106- * @return bool
107- */
108- protected function replaceExistingFile ($ filename , $ question )
109- {
110- $ replaceExistingFile = true ;
111- if (file_exists ($ filename )) {
112- do {
113- $ input = $ this ->ask ($ question );
114- } while (strtolower ($ input ) != 'y ' && strtolower ($ input ) != 'n ' );
115-
116- if (strtolower ($ input ) == 'n ' )
117- $ replaceExistingFile = false ;
118- }
119- return $ replaceExistingFile ;
120- }
12172}
0 commit comments