1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BaseInitCommand.php |
4
|
|
|
* |
5
|
|
|
* Description |
6
|
|
|
* |
7
|
|
|
* @author Milkmeowo <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Milkmeowo\Framework\Repository\Generators\Commands; |
11
|
|
|
|
12
|
|
|
use Illuminate\Console\Command; |
13
|
|
|
use Illuminate\Filesystem\Filesystem; |
14
|
|
|
use Milkmeowo\Framework\Repository\Generators\ControllerGenerator; |
15
|
|
|
use Milkmeowo\Framework\Repository\Generators\ModelGenerator; |
16
|
|
|
use Milkmeowo\Framework\Repository\Generators\PresenterGenerator; |
17
|
|
|
use Milkmeowo\Framework\Repository\Generators\RepositoryEloquentGenerator; |
18
|
|
|
use Milkmeowo\Framework\Repository\Generators\RepositoryInterfaceGenerator; |
19
|
|
|
use Milkmeowo\Framework\Repository\Generators\TransformerGenerator; |
20
|
|
|
|
21
|
|
|
class BaseInitCommand extends Command |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The name and signature of the console command. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'starter:base'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The console command description. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $description = 'Create starter base files'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Filesystem instance. |
40
|
|
|
* |
41
|
|
|
* @var Filesystem |
42
|
|
|
*/ |
43
|
|
|
protected $filesystem; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Filename of stub-file. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $stubs = [ |
51
|
|
|
'api.controller.laravel' => 'Api/LaravelBaseController', |
52
|
|
|
'api.controller.lumen' => 'Api/LumenBaseController', |
53
|
|
|
'http.controller.laravel' => 'Http/LaravelBaseController', |
54
|
|
|
'http.controller.lumen' => 'Http/LumenBaseController', |
55
|
|
|
'model' => 'Models/BaseModel', |
56
|
|
|
'presenter' => 'Presenters/BasePresenter', |
57
|
|
|
'repositories.eloquent' => 'Repositories/Eloquent/BaseRepository', |
58
|
|
|
'repositories.interfaces' => 'Repositories/Interfaces/BaseRepositoryInterface', |
59
|
|
|
'transformer' => 'Transformers/BaseTransformer', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* BaseInitCommand constructor. |
64
|
|
|
* |
65
|
|
|
* @param Filesystem $filesystem |
66
|
|
|
*/ |
67
|
|
|
public function __construct(Filesystem $filesystem) |
68
|
|
|
{ |
69
|
|
|
$this->filesystem = $filesystem; |
70
|
|
|
|
71
|
|
|
parent::__construct(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function handle() |
75
|
|
|
{ |
76
|
|
|
$this->generateApiController(); |
77
|
|
|
|
78
|
|
|
$this->generateHttpController(); |
79
|
|
|
|
80
|
|
|
$this->generateModel(); |
81
|
|
|
|
82
|
|
|
$this->generateRepositoriesEloquent(); |
83
|
|
|
|
84
|
|
|
$this->generateRepositoriesInterfaces(); |
85
|
|
|
|
86
|
|
|
$this->generatePresenters(); |
87
|
|
|
|
88
|
|
|
$this->generateTransformer(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function generateApiController() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$stub = is_lumen() ? $this->stubs['api.controller.lumen'] : $this->stubs['api.controller.laravel']; |
94
|
|
|
$stub = $this->getStub($stub); |
95
|
|
|
|
96
|
|
|
$controllerGenerator = new ControllerGenerator([ 'name' => 'Base' ]); |
97
|
|
|
$namespace = $controllerGenerator->getNamespace(); |
98
|
|
|
|
99
|
|
|
$path = $controllerGenerator->getBasePath().'/'.$controllerGenerator->getConfigGeneratorClassPath($controllerGenerator->getPathConfigNode(), |
100
|
|
|
true).'/BaseController.php'; |
101
|
|
|
|
102
|
|
|
$this->generateFile($path, $stub, $namespace); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function generateHttpController() |
106
|
|
|
{ |
107
|
|
|
$stub = is_lumen() ? $this->stubs['http.controller.lumen'] : $this->stubs['http.controller.laravel']; |
108
|
|
|
$stub = $this->getStub($stub); |
109
|
|
|
$path = app()->path().'/Http/Controllers/BaseController.php'; |
110
|
|
|
$namespace = 'namespace '.app()->getNamespace().'Http\Controllers;'; |
111
|
|
|
|
112
|
|
|
$this->generateFile($path, $stub, $namespace); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function generateModel() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$stub = $this->stubs['model']; |
118
|
|
|
$stub = $this->getStub($stub); |
119
|
|
|
$generator = new ModelGenerator([ 'name' => 'BaseModel' ]); |
120
|
|
|
$namespace = $generator->getNamespace(); |
121
|
|
|
$path = $generator->getPath(); |
122
|
|
|
|
123
|
|
|
$this->generateFile($path, $stub, $namespace); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function generatePresenters() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$stub = $this->stubs['presenter']; |
129
|
|
|
$stub = $this->getStub($stub); |
130
|
|
|
$generator = new PresenterGenerator([ 'name' => 'Base' ]); |
131
|
|
|
$namespace = $generator->getNamespace(); |
132
|
|
|
$path = $generator->getPath(); |
133
|
|
|
|
134
|
|
|
$this->generateFile($path, $stub, $namespace); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function generateRepositoriesEloquent() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$stub = $this->stubs['repositories.eloquent']; |
140
|
|
|
$stub = $this->getStub($stub); |
141
|
|
|
$generator = new RepositoryEloquentGenerator([ 'name' => 'Base' ]); |
142
|
|
|
$namespace = $generator->getNamespace(); |
143
|
|
|
$path = $generator->getBasePath().'/'.$generator->getConfigGeneratorClassPath($generator->getPathConfigNode(), |
144
|
|
|
true).'/BaseRepository.php'; |
145
|
|
|
|
146
|
|
|
$this->generateFile($path, $stub, $namespace); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
public function generateRepositoriesInterfaces() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$stub = $this->stubs['repositories.interfaces']; |
152
|
|
|
$stub = $this->getStub($stub); |
153
|
|
|
$generator = new RepositoryInterfaceGenerator([ 'name' => 'Base' ]); |
154
|
|
|
$namespace = $generator->getNamespace(); |
155
|
|
|
$path = $generator->getBasePath().'/'.$generator->getConfigGeneratorClassPath($generator->getPathConfigNode(), |
156
|
|
|
true).'/BaseRepositoryInterface.php'; |
157
|
|
|
|
158
|
|
|
$this->generateFile($path, $stub, $namespace); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
public function generateTransformer() |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$stub = $this->stubs['transformer']; |
164
|
|
|
$stub = $this->getStub($stub); |
165
|
|
|
$generator = new TransformerGenerator([ 'name' => 'Base' ]); |
166
|
|
|
$namespace = $generator->getNamespace(); |
167
|
|
|
$path = $generator->getPath(); |
168
|
|
|
|
169
|
|
|
$this->generateFile($path, $stub, $namespace); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function generateFile($path, $stub, $namespace) |
173
|
|
|
{ |
174
|
|
|
if ( ! $this->filesystem->exists($path) || $this->confirm($path.' already exists! Continue?')) { |
175
|
|
|
$content = str_replace('$NAMESPACE$', $namespace, $stub); |
176
|
|
|
|
177
|
|
View Code Duplication |
if (! $this->filesystem->isDirectory($dir = dirname($path))) { |
|
|
|
|
178
|
|
|
$this->filesystem->makeDirectory($dir, 0777, true, true); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->filesystem->put($path, $content); |
182
|
|
|
$this->line('---------------'); |
183
|
|
|
$this->info($path ." generated"); |
184
|
|
|
$this->line('---------------'); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getPath($stub) |
189
|
|
|
{ |
190
|
|
|
$defaultPath = dirname(__DIR__); |
191
|
|
|
$path = config('repository.generator.stubsOverridePath', $defaultPath); |
192
|
|
|
|
193
|
|
|
// rollback |
194
|
|
|
if ( ! file_exists($path.'/Stubs/base/'.$stub.'.stub')) { |
195
|
|
|
$path = $defaultPath; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $path.'/Stubs/base/'.$stub.'.stub'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getStub($stub) |
202
|
|
|
{ |
203
|
|
|
return $this->filesystem->get($this->getPath($stub)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.