@@ -51,37 +51,61 @@ 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+ return ;
72+ }
73+
74+ // If it was found on both namespaces, we'll ask user to pick one of them
75+ if ($ existsOnApp && $ existsOnModels ) {
76+ $ result = $ this ->choice ('Multiple models with this name were found, which one do you want to use? ' , [
77+ 1 => "Use $ namespaceApp " ,
78+ 2 => "Use $ namespaceModels " ,
79+ ]);
80+
81+ // Disable the namespace not selected
82+ $ existsOnApp = $ result === 1 ;
83+ $ existsOnModels = $ result === 2 ;
84+ }
85+
86+ $ name = $ existsOnApp ? $ namespaceApp : $ namespaceModels ;
5687 $ path = $ this ->getPath ($ name );
5788
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 ())) {
89+ // As the class already exists, we don't want to create the class and overwrite the
90+ // user's code. We just make sure it uses CrudTrait. We add that one line.
91+ if (!$ this ->hasOption ('force ' ) || !$ this ->option ('force ' )) {
6492 $ file = $ this ->files ->get ($ path );
65- $ file_array = preg_split ('/(\r\n)|\r|\n/ ' , $ file );
93+ $ lines = preg_split ('/(\r\n)|\r|\n/ ' , $ file );
6694
6795 // check if it already uses CrudTrait
6896 // if it does, do nothing
69- if (Str::contains ($ file , [$ this ->crudTrait ])) {
70- $ this ->info ('Model already exists and uses CrudTrait. ' );
71-
72- return false ;
97+ if (Str::contains ($ file , $ this ->crudTrait )) {
98+ $ this ->error ('Model already uses CrudTrait. ' );
99+ return ;
73100 }
74101
75102 // 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 )) {
103+ foreach ($ lines as $ key => $ line ) {
104+ if (Str::contains ($ line , "class {$ this ->getNameInput ()} extends " )) {
81105 if (Str::endsWith ($ line , '{ ' )) {
82106 // add the trait on the next
83107 $ position = $ key + 1 ;
84- } elseif ($ file_array [$ key + 1 ] == '{ ' ) {
108+ } elseif ($ lines [$ key + 1 ] == '{ ' ) {
85109 // add the trait on the next next line
86110 $ position = $ key + 2 ;
87111 }
@@ -91,31 +115,21 @@ public function handle()
91115 // IDEs start counting from 1
92116
93117 // add CrudTrait
94- array_splice ($ file_array , $ position , 0 , ' use \\' . $ this ->crudTrait . ' ; ' );
118+ array_splice ($ lines , $ position , 0 , " use \\{ $ this ->crudTrait } ; " );
95119
96120 // save the file
97- $ this ->files ->put ($ path , implode (PHP_EOL , $ file_array ));
121+ $ this ->files ->put ($ path , implode (PHP_EOL , $ lines ));
98122
99123 // let the user know what we've done
100- $ this ->info ('Model already exists! We just added CrudTrait on it. ' );
101-
102- return false ;
124+ $ this ->error ('Model already exists! ' );
125+ $ this -> info ( ' We \' ve added CrudTrait on the Model. ' );
126+ return ;
103127 }
104128 }
105129
106- $ this ->error ('Model already exists! Could not add CrudTrait - please add manually. ' );
107-
108- return false ;
130+ // In case we couldn't add the CrudTrait
131+ $ this ->error ("Model already exists on ' $ name' and we couldn't add CrudTrait. Please add it manually. " );
109132 }
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. ' );
119133 }
120134
121135 /**
@@ -128,18 +142,6 @@ protected function getStub()
128142 return __DIR__ .'/../stubs/crud-model.stub ' ;
129143 }
130144
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-
143145 /**
144146 * Replace the table name for the given stub.
145147 *
0 commit comments