Skip to content
This repository was archived by the owner on Jan 17, 2022. It is now read-only.

Commit cb8304a

Browse files
committed
Generator looks good now
1 parent cdb0230 commit cb8304a

File tree

5 files changed

+147
-37
lines changed

5 files changed

+147
-37
lines changed

src/Generator/ApiMakeCommand.php

Lines changed: 130 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<?php namespace App\Api\Controllers;
1+
<?php namespace {{controllerNamespace}};
22

3-
use App\User;
4-
use App\Api\Transformers\UserTransformer;
3+
use {{modelFullName}};
4+
use {{transformerFullName}};
55

6-
class UserController extends Controller
6+
class {{controllerName}} extends Controller
77
{
88
/**
99
* Eloquent model.
@@ -12,7 +12,7 @@ class UserController extends Controller
1212
*/
1313
protected function model()
1414
{
15-
return new User;
15+
return new {{modelName}};
1616
}
1717

1818
/**
@@ -22,6 +22,6 @@ class UserController extends Controller
2222
*/
2323
protected function transformer()
2424
{
25-
return new UserTransformer;
25+
return new {{transformerName}};
2626
}
2727
}

src/Generator/stubs/route.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Route::resource('users', 'UserController');
1+
Route::resource('{{routeName}}', '{{controllerName}}');

src/Generator/stubs/transformer.stub

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
<?php namespace App\Api\Transformers;
1+
<?php namespace {{transformerNamespace}};
22

3-
use App\User;
3+
use {{modelFullName}};
44
use League\Fractal\TransformerAbstract;
55

6-
class UserTransformer extends TransformerAbstract
6+
class {{transformerName}} extends TransformerAbstract
77
{
88
/**
99
* Turn this item object into a generic array.
1010
*
1111
* @param $item
1212
* @return array
1313
*/
14-
public function transform(User $item)
14+
public function transform({{modelName}} $item)
1515
{
1616
return [
1717
'id' => (int)$item->id,

src/config/config.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22

33
return [
4-
'routes_file' => 'Api/routes.php',
54
'controllers_path' => 'Api/Controllers',
65
'transformers_path' => 'Api/Transformers',
7-
'controller_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/controller.stub',
8-
'transformer_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/transformer.stub',
9-
'route_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/route.stub',
10-
'models_base_dir' => 'Models'
6+
'routes_file' => 'Api/routes.php',
7+
'models_base_dir' => 'Models',
8+
9+
'controller_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/stubs/controller.stub',
10+
'route_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/stubs/route.stub',
11+
'transformer_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/stubs/transformer.stub',
1112
];

0 commit comments

Comments
 (0)