33namespace Davinet \ArtisanCommand \Commands ;
44
55use Illuminate \Console \Command ;
6+ use Illuminate \Support \Str ;
67
78class Repository extends Command
89{
@@ -89,6 +90,18 @@ protected function replaceModelName($name, $stub)
8990 return str_replace ('DummyModel ' , $ model , $ stub );
9091 }
9192
93+ /**
94+ * Replace the namespace of the namespace of the model.
95+ *
96+ * @param $namespace
97+ * @param $stub
98+ * @return mixed
99+ */
100+ protected function replaceModelNamespace ($ namespace , $ stub )
101+ {
102+ return str_replace ('DummyModelNamespace ' , ucfirst ($ namespace ), $ stub );
103+ }
104+
92105 /**
93106 * Rewrite actually the content in the file.
94107 *
@@ -102,6 +115,36 @@ protected function putInFile($filename, $content)
102115 file_put_contents ($ filename , $ content );
103116 }
104117
118+ /**
119+ * Set the right name and namespace.
120+ *
121+ * @param $model
122+ * @param $namespace
123+ * @return void
124+ */
125+ protected function setModelAndNamespace (&$ model , &$ namespace )
126+ {
127+ $ exploded = explode ('\\' , $ model );
128+ $ model = array_last ($ exploded );
129+ $ namespace = '' ;
130+
131+ for ($ i = 0 ; $ i < count ($ exploded ) - 1 ; $ i ++)
132+ $ namespace .= $ exploded [$ i ].'\\' ;
133+
134+ $ namespace = Str::replaceLast ('\\' ,'' , $ namespace );
135+ }
136+
137+ /**
138+ * Check if a model file exists.
139+ *
140+ * @param $model
141+ * @return bool
142+ */
143+ protected function modelFileExists ($ model )
144+ {
145+ return file_exists ( base_path (lcfirst ($ model ).'.php ' ));
146+ }
147+
105148 /**
106149 * Execute the console command.
107150 *
@@ -111,32 +154,45 @@ public function handle()
111154 {
112155 $ name = $ this ->argument ('name ' );
113156 $ model = $ this ->option ('model ' );
114-
157+ $ namespace = ' App ' ;
115158 if (empty ($ name )) {
116159 $ this ->error ('Please the name of the repository is expected. ' );
117160 } else {
118- if ((empty ($ model ) || !isset ($ model )) || (!file_exists (app_path ('/ ' .$ model .'.php ' )))) {
161+ $ content = null ;
162+
163+ if (is_null ($ model )) {
119164 $ content = $ this ->replaceClassName ($ name , $ this ->getEmptyStub ());
120165 } else {
121- $ content = $ this ->replaceClassName ($ name , $ this ->getStub ());
122- $ content = $ this ->replaceModelName ($ model , $ content );
123- $ content = $ this ->replacePropertyName ($ model , $ content );
166+ if (Str::contains ($ model , '\\' )) {
167+ $ this ->setModelAndNamespace ($ model , $ namespace );
168+ }
169+
170+ if ($ this ->modelFileExists ($ namespace .'\\' .$ model )) {
171+ $ content = $ this ->replaceModelNamespace ($ namespace , $ this ->getStub ());
172+ $ content = $ this ->replaceModelName ($ model , $ content );
173+ $ content = $ this ->replacePropertyName ($ model , $ content );
174+ $ content = $ this ->replaceClassName ($ name , $ content );
175+ } else {
176+ $ this ->output ->error ('The specified model " ' .$ this ->option ('model ' ).'" does not exist. ' );
177+ }
124178 }
125179
126- $ filename = app_path ('Repositories/ ' .ucfirst ($ name ).'.php ' );
180+ if (!is_null ($ content )) {
181+ $ filename = app_path ('Repositories/ ' .ucfirst ($ name ).'.php ' );
127182
128- if (file_exists ($ filename )) {
129- do {
130- $ input = $ this ->ask ("There is a repository with this name ( $ name) do you want to replace it ? [o/n] " );
131- } while (strtolower ($ input ) != 'o ' && strtolower ($ input ) != 'n ' );
183+ if (file_exists ($ filename )) {
184+ do {
185+ $ input = $ this ->ask ("There is a repository with this name ( $ name) do you want to replace it ? [o/n] " );
186+ } while (strtolower ($ input ) != 'o ' && strtolower ($ input ) != 'n ' );
132187
133- if ('o ' == strtolower ($ input )){
188+ if ('o ' == strtolower ($ input )){
189+ $ this ->putInFile ($ filename , $ content );
190+ $ this ->info ('Reporitory created successfully. ' );
191+ }
192+ } else {
134193 $ this ->putInFile ($ filename , $ content );
135194 $ this ->info ('Reporitory created successfully. ' );
136195 }
137- } else {
138- $ this ->putInFile ($ filename , $ content );
139- $ this ->info ('Reporitory created successfully. ' );
140196 }
141197 }
142198 }
0 commit comments