@@ -69,11 +69,11 @@ public function fire()
6969 {
7070 $ this ->prepareVariablesForStubs ($ this ->argument ('name ' ));
7171
72- //create controller
72+ $ this -> createController ();
7373
74- //create transformer
74+ $ this -> createTransformer ();
7575
76- //create routes
76+ $ this -> addRoutes ();
7777 }
7878
7979 /**
@@ -91,10 +91,8 @@ protected function prepareVariablesForStubs($name)
9191
9292 $ this ->setModelData ($ name )
9393 ->setControllerData ()
94- ->setTransformerData ()
95- ->setEndpoint ();
96-
97- var_dump ($ this ->stubVariables );
94+ ->setRouteData ()
95+ ->setTransformerData ();
9896 }
9997
10098 /**
@@ -125,21 +123,39 @@ protected function setModelData($name)
125123 /**
126124 * Set the controller name and namespace.
127125 *
128- * @return string
126+ * @return $this
129127 */
130128 protected function setControllerData ()
131129 {
132- // $this->stubVariables['transformerName'] = $this->stubVariables['modelName'].'Transformer';
133- // $this->stubVariables['transformerNamespace'] = $this->appNamespace.$this->convertSlashes(config('laravel-api-generator.transformers_path'));
134- // $this->stubVariables['transformerFullName'] = trim($this->stubVariables['transformerNamespace'].'\\'.$this->stubVariables['transformerName'], '\\');
130+ $ this ->stubVariables ['controllerName ' ] = $ this ->stubVariables ['modelName ' ].'Controller ' ;
131+ $ this ->stubVariables ['controllerName ' ] = $ this ->stubVariables ['modelName ' ].'Controller ' ;
132+ $ this ->stubVariables ['controllerNamespace ' ] = $ this ->appNamespace .$ this ->convertSlashes (config ('laravel-api-generator.controllers_path ' ));
133+ $ this ->stubVariables ['controllerFullName ' ] = trim ($ this ->stubVariables ['controllerNamespace ' ].'\\' .$ this ->stubVariables ['controllerName ' ], '\\' );
134+
135+ return $ this ;
136+ }
137+
138+
139+ /**
140+ * Set route data for a given model.
141+ * "Profile\Payer" -> "profile_payers"
142+ *
143+ * @return $this
144+ */
145+ protected function setRouteData ()
146+ {
147+ $ name = str_replace ('\\' , '' , $ this ->stubVariables ['modelFullNameWithoutRoot ' ]);
148+ $ name = snake_case ($ name );
149+
150+ $ this ->stubVariables ['routeName ' ] = str_plural ($ name );
135151
136152 return $ this ;
137153 }
138154
139155 /**
140156 * Set the transformer name and namespace.
141157 *
142- * @return string
158+ * @return $this
143159 */
144160 protected function setTransformerData ()
145161 {
@@ -151,19 +167,114 @@ protected function setTransformerData()
151167 }
152168
153169 /**
154- * Set endpoint for a given model.
155- * "Profile\Payer" -> "profile_payers"
170+ * Create controller class file from a stub.
171+ */
172+ protected function createController ()
173+ {
174+ $ this ->createClass ('controller ' );
175+ }
176+
177+ /**
178+ * Create controller class file from a stub.
179+ */
180+ protected function createTransformer ()
181+ {
182+ $ this ->createClass ('transformer ' );
183+ }
184+
185+ /**
186+ * Add routes to routes file.
187+ */
188+ protected function addRoutes ()
189+ {
190+ $ stub = $ this ->constructStub (base_path (config ('laravel-api-generator.route_stub ' )));
191+
192+ $ routesFile = app_path (config ('laravel-api-generator.routes_file ' ));
193+
194+ // read file
195+ $ lines = file ($ routesFile );
196+ $ lastLine = $ lines [count ($ lines )-1 ];
197+
198+ // modify file
199+ if (strcmp ($ lastLine , "}); " ) === 0 ) {
200+ $ lines [count ($ lines )-1 ] = " " .$ stub ;
201+ $ lines [] = "\r\n}); " ;
202+ } else {
203+ $ lines [] = "\r\n" .$ stub ;
204+ }
205+
206+ // save file
207+ $ fp = fopen ($ routesFile , 'w ' );
208+ fwrite ($ fp , implode ('' , $ lines ));
209+ fclose ($ fp );
210+
211+ $ this ->info ('Routes added successfully. ' );
212+ }
213+
214+ /**
215+ * Create class with a given type.
216+ *
217+ * @param $type
218+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
219+ */
220+ protected function createClass ($ type )
221+ {
222+ $ path = $ this ->getPath ($ this ->stubVariables [$ type .'FullName ' ]);
223+ if ($ this ->files ->exists ($ path )) {
224+ $ this ->error (ucfirst ($ type ).' already exists! ' );
225+ return ;
226+ }
227+
228+ $ this ->makeDirectoryIfNeeded ($ path );
229+
230+ $ this ->files ->put ($ path , $ this ->constructStub (base_path (config ('laravel-api-generator. ' .$ type .'_stub ' ))));
231+
232+ $ this ->info (ucfirst ($ type ).' created successfully. ' );
233+ }
234+
235+ /**
236+ * Get the destination file path.
156237 *
238+ * @param string $name
157239 * @return string
158240 */
159- protected function setEndpoint ( )
241+ protected function getPath ( $ name )
160242 {
161- $ endpoint = str_replace ('\\' , '' , $ this ->stubVariables ['modelFullNameWithoutRoot ' ]);
162- $ endpoint = snake_case ($ endpoint );
243+ $ name = str_replace ($ this ->appNamespace , '' , $ name );
163244
164- $ this ->stubVariables ['endpoint ' ] = str_plural ($ endpoint );
245+ return $ this ->laravel ['path ' ].'/ ' .str_replace ('\\' , '/ ' , $ name ).'.php ' ;
246+ }
165247
166- return $ this ;
248+ /**
249+ * Build the directory for the class if needed.
250+ *
251+ * @param string $path
252+ * @return string
253+ */
254+ protected function makeDirectoryIfNeeded ($ path )
255+ {
256+ if ( ! $ this ->files ->isDirectory (dirname ($ path )))
257+ {
258+ $ this ->files ->makeDirectory (dirname ($ path ), 0777 , true , true );
259+ }
260+ }
261+
262+ /**
263+ * Get stub content and replace all stub placeholders
264+ * with data from $this->stubData
265+ *
266+ * @param string $path
267+ * @return string
268+ */
269+ protected function constructStub ($ path )
270+ {
271+ $ stub = $ this ->files ->get ($ path );
272+
273+ foreach ($ this ->stubVariables as $ var => $ value ) {
274+ $ stub = str_replace ('{{ ' .$ var .'}} ' , $ value , $ stub );
275+ }
276+
277+ return $ stub ;
167278 }
168279
169280 /**
@@ -182,12 +293,10 @@ protected function getArguments()
182293 * Convert "/" to "\".
183294 *
184295 * @param $string
185- *
186296 * @return string
187297 */
188298 protected function convertSlashes ($ string )
189299 {
190300 return str_replace ('/ ' , '\\' , $ string );
191-
192301 }
193302}
0 commit comments