@@ -51,37 +51,63 @@ class CrudModelBackpackCommand extends GeneratorCommand
5151 */
5252 public function handle ()
5353 {
54- $ name = $ this ->qualifyClass ($ this ->getNameInput ());
54+ $ name = $ this ->getNameInput ();
55+ $ namespaceApp = $ this ->qualifyClass ($ this ->getNameInput ());
56+ $ namespaceModels = $ this ->qualifyClass ('/Models/ ' .$ this ->getNameInput ());
5557
58+ // Check if exists on app or models
59+ $ existsOnApp = $ this ->alreadyExists ($ namespaceApp );
60+ $ existsOnModels = $ this ->alreadyExists ($ namespaceModels );
61+
62+ // If no model was found, we will generate the path to the location where this class file
63+ // should be written. Then, we will build the class and make the proper replacements on
64+ // the stub files so that it gets the correctly formatted namespace and class name.
65+ if (! $ existsOnApp && ! $ existsOnModels ) {
66+ $ this ->makeDirectory ($ namespaceModels );
67+
68+ $ this ->files ->put ($ this ->getPath ($ namespaceModels ), $ this ->sortImports ($ this ->buildClass ($ namespaceModels )));
69+
70+ $ this ->info ($ this ->type .' created successfully. ' );
71+
72+ return ;
73+ }
74+
75+ // If it was found on both namespaces, we'll ask user to pick one of them
76+ if ($ existsOnApp && $ existsOnModels ) {
77+ $ result = $ this ->choice ('Multiple models with this name were found, which one do you want to use? ' , [
78+ 1 => "Use $ namespaceApp " ,
79+ 2 => "Use $ namespaceModels " ,
80+ ]);
81+
82+ // Disable the namespace not selected
83+ $ existsOnApp = $ result === 1 ;
84+ $ existsOnModels = $ result === 2 ;
85+ }
86+
87+ $ name = $ existsOnApp ? $ namespaceApp : $ namespaceModels ;
5688 $ path = $ this ->getPath ($ name );
5789
58- // First we will check to see if the class already exists. If it does, we don't want
59- // to create the class and overwrite the user's code. We just make sure it uses CrudTrait
60- // We add that one line. Otherwise, we will continue generating this class' files.
61- if ((! $ this ->hasOption ('force ' ) ||
62- ! $ this ->option ('force ' )) &&
63- $ this ->alreadyExists ($ this ->getNameInput ())) {
90+ // As the class already exists, we don't want to create the class and overwrite the
91+ // user's code. We just make sure it uses CrudTrait. We add that one line.
92+ if (! $ this ->hasOption ('force ' ) || ! $ this ->option ('force ' )) {
6493 $ file = $ this ->files ->get ($ path );
65- $ file_array = preg_split ('/(\r\n)|\r|\n/ ' , $ file );
94+ $ lines = preg_split ('/(\r\n)|\r|\n/ ' , $ file );
6695
6796 // check if it already uses CrudTrait
6897 // if it does, do nothing
69- if (Str::contains ($ file , [ $ this ->crudTrait ] )) {
70- $ this ->info ('Model already exists and uses CrudTrait. ' );
98+ if (Str::contains ($ file , $ this ->crudTrait )) {
99+ $ this ->comment ('Model already used CrudTrait. ' );
71100
72- return false ;
101+ return ;
73102 }
74103
75104 // if it does not have CrudTrait, add the trait on the Model
76-
77- $ classDefinition = 'class ' .$ this ->getNameInput ().' extends ' ;
78-
79- foreach ($ file_array as $ key => $ line ) {
80- if (Str::contains ($ line , $ classDefinition )) {
105+ foreach ($ lines as $ key => $ line ) {
106+ if (Str::contains ($ line , "class {$ this ->getNameInput ()} extends " )) {
81107 if (Str::endsWith ($ line , '{ ' )) {
82108 // add the trait on the next
83109 $ position = $ key + 1 ;
84- } elseif ($ file_array [$ key + 1 ] == '{ ' ) {
110+ } elseif ($ lines [$ key + 1 ] == '{ ' ) {
85111 // add the trait on the next next line
86112 $ position = $ key + 2 ;
87113 }
@@ -91,31 +117,21 @@ public function handle()
91117 // IDEs start counting from 1
92118
93119 // add CrudTrait
94- array_splice ($ file_array , $ position , 0 , ' use \\' . $ this ->crudTrait . ' ; ' );
120+ array_splice ($ lines , $ position , 0 , " use \\{ $ this ->crudTrait } ; " );
95121
96122 // save the file
97- $ this ->files ->put ($ path , implode (PHP_EOL , $ file_array ));
123+ $ this ->files ->put ($ path , implode (PHP_EOL , $ lines ));
98124
99125 // let the user know what we've done
100- $ this ->info ('Model already exists! We just added CrudTrait on it. ' );
126+ $ this ->info ('Model already existed. Added CrudTrait to it. ' );
101127
102- return false ;
128+ return ;
103129 }
104130 }
105131
106- $ this ->error ('Model already exists! Could not add CrudTrait - please add manually. ' );
107-
108- return false ;
132+ // In case we couldn't add the CrudTrait
133+ $ this ->error ("Model already existed on ' $ name' and we couldn't add CrudTrait. Please add it manually. " );
109134 }
110-
111- // Next, we will generate the path to the location where this class' file should get
112- // written. Then, we will build the class and make the proper replacements on the
113- // stub files so that it gets the correctly formatted namespace and class name.
114- $ this ->makeDirectory ($ path );
115-
116- $ this ->files ->put ($ path , $ this ->sortImports ($ this ->buildClass ($ name )));
117-
118- $ this ->info ($ this ->type .' created successfully. ' );
119135 }
120136
121137 /**
@@ -128,18 +144,6 @@ protected function getStub()
128144 return __DIR__ .'/../stubs/crud-model.stub ' ;
129145 }
130146
131- /**
132- * Get the default namespace for the class.
133- *
134- * @param string $rootNamespace
135- *
136- * @return string
137- */
138- protected function getDefaultNamespace ($ rootNamespace )
139- {
140- return $ rootNamespace .'\Models ' ;
141- }
142-
143147 /**
144148 * Replace the table name for the given stub.
145149 *
0 commit comments