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

Commit cdb0230

Browse files
committed
Work on the generator
1 parent 9bed4cc commit cdb0230

File tree

5 files changed

+210
-15
lines changed

5 files changed

+210
-15
lines changed

src/Generator/ApiMakeCommand.php

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<?php
2+
3+
namespace Arrilot\Api\Generator;
4+
5+
use Illuminate\Console\AppNamespaceDetectorTrait;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
10+
class ApiMakeCommand extends Command
11+
{
12+
use AppNamespaceDetectorTrait;
13+
14+
/**
15+
* The filesystem instance.
16+
*
17+
* @var \Illuminate\Filesystem\Filesystem
18+
*/
19+
protected $files;
20+
21+
/**
22+
* The console command name.
23+
*
24+
* @var string
25+
*/
26+
protected $name = 'make:api';
27+
28+
/**
29+
* The console command description.
30+
*
31+
* @var string
32+
*/
33+
protected $description = 'Create api controller, transformer and api routes for a given model (arrilot/laravel-api-generator)';
34+
35+
/**
36+
* The application namespace.
37+
*
38+
* @var string
39+
*/
40+
protected $appNamespace;
41+
42+
/**
43+
* The array of variables available in stubs.
44+
*
45+
* @var array
46+
*/
47+
protected $stubVariables = [];
48+
49+
protected $modelsBaseNamespace;
50+
51+
/**
52+
* Create a new controller creator command instance.
53+
*
54+
* @param \Illuminate\Filesystem\Filesystem $files
55+
*/
56+
public function __construct(Filesystem $files)
57+
{
58+
parent::__construct();
59+
60+
$this->files = $files;
61+
}
62+
63+
/**
64+
* Execute the console command.
65+
*
66+
* @return void
67+
*/
68+
public function fire()
69+
{
70+
$this->prepareVariablesForStubs($this->argument('name'));
71+
72+
//create controller
73+
74+
//create transformer
75+
76+
//create routes
77+
}
78+
79+
/**
80+
* Prepare names, paths and namespaces for stubs.
81+
*
82+
* @param $name
83+
*/
84+
protected function prepareVariablesForStubs($name)
85+
{
86+
$this->appNamespace = $this->getAppNamespace();
87+
88+
$baseDir = config('laravel-api-generator.models_base_dir');
89+
90+
$this->modelsBaseNamespace = $baseDir ? trim($baseDir, '\\').'\\' : '';
91+
92+
$this->setModelData($name)
93+
->setControllerData()
94+
->setTransformerData()
95+
->setEndpoint();
96+
97+
var_dump($this->stubVariables);
98+
}
99+
100+
/**
101+
* Set the model name and namespace.
102+
*
103+
* @return $this
104+
*/
105+
protected function setModelData($name)
106+
{
107+
if (str_contains($name, '/'))
108+
{
109+
$name = $this->convertSlashes($name);
110+
}
111+
112+
$name = trim($name, '\\');
113+
114+
$this->stubVariables['modelFullNameWithoutRoot'] = $name;
115+
$this->stubVariables['modelFullName'] = $this->appNamespace.$this->modelsBaseNamespace.$name;
116+
117+
$exploded = explode('\\', $this->stubVariables['modelFullName']);
118+
119+
$this->stubVariables['modelName'] = array_pop($exploded);
120+
$this->stubVariables['modelNamespace'] = implode('\\', $exploded);
121+
122+
return $this;
123+
}
124+
125+
/**
126+
* Set the controller name and namespace.
127+
*
128+
* @return string
129+
*/
130+
protected function setControllerData()
131+
{
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'], '\\');
135+
136+
return $this;
137+
}
138+
139+
/**
140+
* Set the transformer name and namespace.
141+
*
142+
* @return string
143+
*/
144+
protected function setTransformerData()
145+
{
146+
$this->stubVariables['transformerName'] = $this->stubVariables['modelName'].'Transformer';
147+
$this->stubVariables['transformerNamespace'] = $this->appNamespace.$this->convertSlashes(config('laravel-api-generator.transformers_path'));
148+
$this->stubVariables['transformerFullName'] = trim($this->stubVariables['transformerNamespace'].'\\'.$this->stubVariables['transformerName'], '\\');
149+
150+
return $this;
151+
}
152+
153+
/**
154+
* Set endpoint for a given model.
155+
* "Profile\Payer" -> "profile_payers"
156+
*
157+
* @return string
158+
*/
159+
protected function setEndpoint()
160+
{
161+
$endpoint = str_replace('\\', '', $this->stubVariables['modelFullNameWithoutRoot']);
162+
$endpoint = snake_case($endpoint);
163+
164+
$this->stubVariables['endpoint'] = str_plural($endpoint);
165+
166+
return $this;
167+
}
168+
169+
/**
170+
* Get the console command arguments.
171+
*
172+
* @return array
173+
*/
174+
protected function getArguments()
175+
{
176+
return [
177+
['name', InputArgument::REQUIRED, 'The name of the model'],
178+
];
179+
}
180+
181+
/**
182+
* Convert "/" to "\".
183+
*
184+
* @param $string
185+
*
186+
* @return string
187+
*/
188+
protected function convertSlashes($string)
189+
{
190+
return str_replace('/', '\\', $string);
191+
192+
}
193+
}

src/Generator/stubs/route.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Route::resource('users', 'UserController');

src/Generator/stubs/transformer.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use League\Fractal\TransformerAbstract;
66
class UserTransformer extends TransformerAbstract
77
{
88
/**
9-
* Turn this item object into a generic array
9+
* Turn this item object into a generic array.
10+
*
1011
* @param $item
1112
* @return array
1213
*/
1314
public function transform(User $item)
1415
{
1516
return [
1617
'id' => (int)$item->id,
17-
'email' => (string)$item->email,
1818
'created_at' => (string)$item->created_at,
1919
'updated_at' => (string)$item->updated_at,
2020
];

src/ServiceProvider.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace Arrilot\Api;
44

5-
use Illuminate\Console\AppNamespaceDetectorTrait;
5+
use Arrilot\Api\Generator\ApiMakeCommand;
66

77
class ServiceProvider extends \Illuminate\Support\ServiceProvider
88
{
9-
use AppNamespaceDetectorTrait;
109

1110
/**
1211
* Register the service provider.
@@ -18,12 +17,12 @@ public function register()
1817
$this->mergeConfigFrom(
1918
__DIR__.'/config/config.php', 'laravel-api-generator'
2019
);
21-
//
22-
// $this->app->singleton('command.api.make', function ($app) {
23-
// return new ApiMakeCommand($app['files']);
24-
// });
25-
//
26-
// $this->commands('command.api.make');
20+
21+
$this->app->singleton('command.api.make', function ($app) {
22+
return new ApiMakeCommand($app['files']);
23+
});
24+
25+
$this->commands('command.api.make');
2726
}
2827

2928
/**
@@ -35,6 +34,6 @@ public function boot()
3534
__DIR__.'/config/config.php' => config_path('laravel-api-generator.php'),
3635
]);
3736

38-
require base_path(config('laravel-api-generator.routes_file'));
37+
require app_path(config('laravel-api-generator.routes_file'));
3938
}
4039
}

src/config/config.php

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

33
return [
4-
'routes_file' => 'app/Api/routes.php',
5-
'controllers_path' => 'app/Api/Controllers',
6-
'transformers_path' => 'app/Api/Transformers',
4+
'routes_file' => 'Api/routes.php',
5+
'controllers_path' => 'Api/Controllers',
6+
'transformers_path' => 'Api/Transformers',
77
'controller_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/controller.stub',
8-
'transformer_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/transformer.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'
911
];

0 commit comments

Comments
 (0)