1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shamaseen\Repository\Generator\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\File; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use Shamaseen\Repository\Generator\Forms\formGenerator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RepositoryGenerator |
13
|
|
|
* @package Shamaseen\Repository\Generator\Commands |
14
|
|
|
*/ |
15
|
|
|
class Generator extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'make:repository |
23
|
|
|
{name : Class (singular) for example User} {--only-view}'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Create repository generator'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The repository name. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $repoName; |
38
|
|
|
/** |
39
|
|
|
* The repository name space. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $repoNamespace; |
44
|
|
|
private $formGenerator; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new command instance. |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
public function __construct() |
51
|
|
|
{ |
52
|
|
|
parent::__construct(); |
53
|
|
|
$this->formGenerator = new formGenerator(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Execute the console command. |
58
|
|
|
* |
59
|
|
|
* @throws \ReflectionException |
60
|
|
|
*/ |
61
|
|
|
public function handle() |
62
|
|
|
{ |
63
|
|
|
$file = preg_split( " (/|\\\\) ", (string)$this->argument('name')) ?? []; |
64
|
|
|
$this->repoName = $file[count($file) - 1]; |
|
|
|
|
65
|
|
|
|
66
|
|
|
unset($file[count($file) - 1]); |
67
|
|
|
$path = implode("\\", $file); |
|
|
|
|
68
|
|
|
|
69
|
|
|
if($this->option('only-view')) |
70
|
|
|
{ |
71
|
|
|
$this->makeViewsAndLanguage($path); |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->makeRepositoryPatternFiles($path); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function makeRepositoryPatternFiles($path) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$model= str_plural(\Config::get('repository.model')); |
81
|
|
|
$interface= str_plural(\Config::get('repository.interface')); |
82
|
|
|
$repository= str_plural(\Config::get('repository.repository')); |
83
|
|
|
|
84
|
|
|
$this->generate($path, \Config::get('repository.controllers_folder'), 'Controller'); |
85
|
|
|
$this->generate($path, $model, 'Entity'); |
86
|
|
|
$this->generate($path, \Config::get('repository.requests_folder'), 'Request'); |
87
|
|
|
$this->generate($path, $interface, 'Interface'); |
88
|
|
|
$this->generate($path, $repository, 'Repository'); |
89
|
|
|
|
90
|
|
|
File::append(\Config::get('repository.route_path') . '/web.php', "\n" . 'Route::resource(\'' . strtolower(str_plural($this->repoName)) . "', '".$path."\\".$this->repoName."Controller');"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $path |
95
|
|
|
* @throws \ReflectionException |
96
|
|
|
*/ |
97
|
|
|
function makeViewsAndLanguage($path) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$entity = $this->getEntity($path); |
100
|
|
|
|
101
|
|
|
$createHtml = ''; |
102
|
|
|
$editHtml = ''; |
103
|
|
|
if($entity instanceof Model) |
104
|
|
|
{ |
105
|
|
|
$createHtml = $this->formGenerator->generateForm($entity); |
106
|
|
|
$editHtml = $this->formGenerator->generateForm($entity,'put'); |
107
|
|
|
} |
108
|
|
|
else |
109
|
|
|
{ |
110
|
|
|
if(!$this->confirm('There is no entity for '.$this->repoName.", do you want to continue (this will disable form generator) ?")) |
111
|
|
|
{ |
112
|
|
|
echo "Dispatch .."; |
113
|
|
|
die; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach (\Config::get('repository.languages') as $lang) |
118
|
|
|
{ |
119
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.lang_path')."/{$lang}" , 'lang'); |
120
|
|
|
} |
121
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'create',$createHtml); |
122
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'edit',$editHtml); |
123
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'index'); |
124
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'show'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $path |
129
|
|
|
* @return bool|Model|object |
130
|
|
|
* @throws \ReflectionException |
131
|
|
|
*/ |
132
|
|
|
function getEntity($path) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$myClass = 'App\Entities\\'.$path."\\".$this->repoName; |
135
|
|
|
if(!class_exists($myClass)) |
136
|
|
|
return false; |
137
|
|
|
|
138
|
|
|
$refl = new ReflectionClass($myClass); |
139
|
|
|
|
140
|
|
|
return $refl->newInstance(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get stub content to generate needed files |
145
|
|
|
* |
146
|
|
|
* @param string $type determine which stub should choose to get content |
147
|
|
|
* @return false|string |
148
|
|
|
*/ |
149
|
|
|
protected function getStub($type) |
150
|
|
|
{ |
151
|
|
|
return file_get_contents(\Config::get('repository.stubs_path') . "/$type.stub"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $path Class path |
156
|
|
|
* @param string $folder default path to generate in |
157
|
|
|
* @param string $type define which kind of files should generate |
158
|
|
|
* @param string $form |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
protected function generate($path, $folder, $type,$form ='') |
162
|
|
|
{ |
163
|
|
|
$content = $this->getStub($type); |
164
|
|
|
|
165
|
|
|
if($content === false) |
166
|
|
|
{ |
167
|
|
|
echo 'file '.$type.".stub is not exist !"; |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$template = str_replace( |
172
|
|
|
[ |
173
|
|
|
'{{modelName}}', |
174
|
|
|
'{{lcPluralModelName}}', |
175
|
|
|
"{{folder}}", |
176
|
|
|
"{{path}}", |
177
|
|
|
"{{modelBaseFolderName}}", |
178
|
|
|
"{{interfaceBaseFolderName}}", |
179
|
|
|
"{{form}}", |
180
|
|
|
], |
181
|
|
|
[ |
182
|
|
|
$this->repoName, |
183
|
|
|
str_plural(lcfirst($this->repoName)), |
184
|
|
|
str_plural($folder), |
185
|
|
|
$path, |
186
|
|
|
str_plural(\Config::get('repository.model','Entity')), |
187
|
|
|
str_plural(\Config::get('repository.interface','Interface')), |
188
|
|
|
$form |
189
|
|
|
], |
190
|
|
|
$this->getStub($type) |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$folder = str_replace('\\','/',$folder); |
194
|
|
|
$path = str_replace('\\','/',$path); |
195
|
|
|
|
196
|
|
|
switch ($type) |
197
|
|
|
{ |
198
|
|
|
case 'Entity': |
199
|
|
|
$filePath = $this->getFolderOrCreate(\Config::get('repository.app_path') . "/{$folder}/{$path}"); |
200
|
|
|
$filePath = rtrim($filePath,'/'); |
201
|
|
|
$filePath .= "/"; |
202
|
|
|
file_put_contents($filePath . "{$this->repoName}.php", $template); |
203
|
|
|
break; |
204
|
|
|
case 'Controller': |
205
|
|
|
case 'Request': |
206
|
|
|
case 'Repository': |
207
|
|
|
case 'Interface': |
208
|
|
|
$filePath = $this->getFolderOrCreate(\Config::get('repository.app_path') . "/{$folder}/{$path}"); |
209
|
|
|
$filePath = rtrim($filePath,'/'); |
210
|
|
|
$filePath .= "/"; |
211
|
|
|
file_put_contents($filePath . "{$this->repoName}{$type}.php", $template); |
212
|
|
|
break; |
213
|
|
|
case 'create': |
214
|
|
|
case 'edit': |
215
|
|
|
case 'index': |
216
|
|
|
case 'show': |
217
|
|
|
$filePath = $this->getFolderOrCreate($folder."/".str_plural($path))."/"; |
218
|
|
|
$repoName = lcfirst($type); |
219
|
|
|
file_put_contents($filePath . $repoName.".blade.php", $template); |
220
|
|
|
break; |
221
|
|
|
default: |
222
|
|
|
$filePath = $this->getFolderOrCreate($folder)."/"; |
223
|
|
|
$repoName = lcfirst($this->repoName); |
224
|
|
|
file_put_contents($filePath . $repoName.".php", $template); |
225
|
|
|
} |
226
|
|
|
return true; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Check if folder exist |
231
|
|
|
* @param string $path class path |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public function getFolderOrCreate($path) |
235
|
|
|
{ |
236
|
|
|
if (!file_exists($path)) { |
237
|
|
|
mkdir($path, 0777, true); |
238
|
|
|
} |
239
|
|
|
return $path; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths