@@ -23,69 +23,59 @@ public static function make($argv)
23
23
24
24
// split the controller name by '/'
25
25
$ controllerNameArray = explode ('/ ' , $ controllerNameArgv );
26
- $ controllerName = end ( $ controllerNameArray );
27
- $ pattern = ' /Controller$/ ' ;
28
- if (! preg_match ( $ pattern , $ controllerName )) {
29
- print_r ( ' not match ' );
30
- $ controllerName .= ' Controller ' ;
31
- }
26
+ $ controllerNameArray = array_map ( function ( $ item ) {
27
+ return ucfirst ( $ item ) ;
28
+ } , $ controllerNameArray );
29
+
30
+ $ controllerNameSpace = " LaraCore \\ App \\ Http \\ Controllers " ;
31
+ $ controllersBasePath = ' app/Http/Controllers ' ;
32
32
33
- // create file path
34
- $ filePath = null ;
35
- $ replaceNamespace = null ;
36
33
if (count ($ controllerNameArray ) > 1 ) {
37
- $ newArray = array_slice ($ controllerNameArray , 0 , -1 );
38
- $ replaceNamespace = '\\' . str_replace ('/ ' , '\\' , $ newArray );
39
- print_r (['$replaceNamespace ' => $ replaceNamespace ]);
40
- $ filePath = DS . implode (DS , $ controllerNameArray );
34
+ $ controllerName = end ($ controllerNameArray );
35
+ $ pattern = '/Controller$/ ' ;
36
+ if (!preg_match ($ pattern , $ controllerName )) {
37
+ $ controllerName .= 'Controller ' ;
38
+ }
39
+ $ controllerNameSpace .= '\\' . str_replace ('/ ' , '\\' , implode ('/ ' , array_slice ($ controllerNameArray , 0 , -1 )));
40
+ $ controllersBasePath .= '/ ' . implode ('/ ' , array_slice ($ controllerNameArray , 0 , -1 ));
41
+ } else {
42
+ $ controllerName = $ controllerNameArray [0 ];
43
+ $ pattern = '/Controller$/ ' ;
44
+ if (!preg_match ($ pattern , $ controllerName )) {
45
+ $ controllerName .= 'Controller ' ;
46
+ }
41
47
}
42
-
43
- $ controllerNameSpace = "LaraCore \\App \\Http \\Controllers {$ replaceNamespace }" ;
48
+ unset($ controllerNameArray [end ($ controllerNameArray )]);
44
49
45
50
// Check if the controller class already exists
46
- if (class_exists ($ controllerNameSpace )) {
51
+ $ class = $ controllerNameSpace . '\\' . $ controllerName ;
52
+ if (class_exists ($ class )) {
47
53
Log::error ("Controller ' $ controllerName' already exists. " );
48
54
exit (1 );
49
55
}
50
- $ path = ROOT . DS . 'app ' . DS . 'Http ' . DS . 'Controllers ' . $ filePath ;
56
+ // controller file path
57
+ $ controllerPath = check_or_make_dir ($ controllersBasePath );
58
+ $ controllerPath .= DS . $ controllerName . '.php ' ;
51
59
52
- // Check if the Migrations directory exists, and create it if not
53
- if (!is_dir ($ path )) {
54
- mkdir ($ path );
55
- }
56
-
57
- $ path = $ path . DS . $ controllerName . '.php ' ;
60
+ // get controller stub
61
+ $ controllerStubPath = base_path ('framework/Stub/Controller.stub ' );
62
+ $ controllerStub = file_get_contents ($ controllerStubPath );
58
63
59
- $ controllerStub = file_get_contents (ROOT . DS . 'framework ' . DS . 'Stub ' . DS . 'Controller.stub ' );
60
- print_r ([
61
- $ path ,
62
- $ replaceNamespace ,
63
- ]);
64
- exit ;
65
- Log::info ("Controller file created: $ path " );
64
+ Log::info ("Controller file created...: $ controllerPath " );
66
65
66
+ // replace stub
67
67
$ controllerContent = str_replace (
68
68
['{{Namespace}} ' , '{{ControllerName}} ' ],
69
69
[$ controllerNameSpace , $ controllerName ],
70
70
$ controllerStub
71
71
);
72
72
73
- $ createFile = file_put_contents ($ path , $ controllerContent );
73
+ $ createFile = file_put_contents ($ controllerPath , $ controllerContent );
74
74
if (!$ createFile ) {
75
- Log::error ("Controller $ controllerName not created. " );
75
+ Log::error ("Controller ' $ controllerName' not created. " );
76
76
exit (1 );
77
77
}
78
- Log::success ("Controller $ controllerName created successfully. " );
79
- }
80
-
81
- /**
82
- * Summary of createMigrationFile
83
- * @param mixed $migrationName
84
- * @return void
85
- */
86
- private static function createMigrationFile ($ migrationName )
87
- {
88
-
78
+ Log::success ("Controller ' $ controllerName' created successfully. " );
89
79
}
90
80
91
81
}
0 commit comments