1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shamaseen\Repository\Generator\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RepositoryGenerator |
10
|
|
|
* @package Shamaseen\Repository\Generator\Commands |
11
|
|
|
*/ |
12
|
|
|
class Generator extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'make:repository |
20
|
|
|
{name : Class (singular) for example User} {--view}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Create repository generator'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The repository name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $repoName; |
35
|
|
|
/** |
36
|
|
|
* The repository name space. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $repoNamespace; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new command instance. |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Execute the console command. |
53
|
|
|
* |
54
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public function handle() |
57
|
|
|
{ |
58
|
|
|
$file = preg_split( " (/|\\\\) ", (string)$this->argument('name')) ?? []; |
59
|
|
|
$this->repoName = $file[count($file) - 1]; |
|
|
|
|
60
|
|
|
if($this->option('only-view')) |
61
|
|
|
{ |
62
|
|
|
$this->makeViewsAndLanguage(); |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->makeRepositoryPatternFiles($file); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function makeRepositoryPatternFiles($file) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
unset($file[count($file) - 1]); |
72
|
|
|
$path = implode("\\", $file); |
73
|
|
|
|
74
|
|
|
$model= str_plural(\Config::get('repository.model')); |
75
|
|
|
$interface= str_plural(\Config::get('repository.interface')); |
76
|
|
|
$repository= str_plural(\Config::get('repository.repository')); |
77
|
|
|
|
78
|
|
|
$this->generate($path, \Config::get('repository.controllers_folder'), 'Controller'); |
79
|
|
|
$this->generate($path, $model, 'Entity'); |
80
|
|
|
$this->generate($path, \Config::get('repository.requests_folder'), 'Request'); |
81
|
|
|
$this->generate($path, $interface, 'Interface'); |
82
|
|
|
$this->generate($path, $repository, 'Repository'); |
83
|
|
|
|
84
|
|
|
File::append(\Config::get('repository.route_path') . '/web.php', "\n" . 'Route::resource(\'' . strtolower(str_plural($this->repoName)) . "', '".$path."\\".$this->repoName."Controller');"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
function makeViewsAndLanguage() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
foreach (\Config::get('repository.languages') as $lang) |
90
|
|
|
{ |
91
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.lang_path')."/{$lang}" , 'lang'); |
92
|
|
|
} |
93
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'create'); |
94
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'edit'); |
95
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'index'); |
96
|
|
|
$this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'show'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get stub content to generate needed files |
101
|
|
|
* |
102
|
|
|
* @param string $type determine which stub should choose to get content |
103
|
|
|
* @return false|string |
104
|
|
|
*/ |
105
|
|
|
protected function getStub($type) |
106
|
|
|
{ |
107
|
|
|
return file_get_contents(\Config::get('repository.stubs_path') . "/$type.stub"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $path Class path |
112
|
|
|
* @param string $folder default path to generate in |
113
|
|
|
* @param string $type define which kind of files should generate |
114
|
|
|
* @return false |
115
|
|
|
*/ |
116
|
|
|
protected function generate($path, $folder, $type) |
117
|
|
|
{ |
118
|
|
|
$content = $this->getStub($type); |
119
|
|
|
|
120
|
|
|
if($content === false) |
121
|
|
|
{ |
122
|
|
|
echo 'file '.$type.".stub is not exist !"; |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$template = str_replace( |
127
|
|
|
[ |
128
|
|
|
'{{modelName}}', |
129
|
|
|
"{{folder}}", |
130
|
|
|
"{{path}}", |
131
|
|
|
"{{modelBaseFolderName}}", |
132
|
|
|
"{{interfaceBaseFolderName}}", |
133
|
|
|
], |
134
|
|
|
[ |
135
|
|
|
$this->repoName, |
136
|
|
|
str_plural($folder), |
137
|
|
|
$path, |
138
|
|
|
str_plural(\Config::get('repository.model','Entity')), |
139
|
|
|
str_plural(\Config::get('repository.interface','Interface')), |
140
|
|
|
], |
141
|
|
|
$this->getStub($type) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$folder = str_replace('\\','/',$folder); |
145
|
|
|
$path = str_replace('\\','/',$path); |
146
|
|
|
|
147
|
|
|
switch ($type) |
148
|
|
|
{ |
149
|
|
|
case 'Entity': |
150
|
|
|
$filePath = $this->getFolderOrCreate(\Config::get('repository.app_path') . "/{$folder}/{$path}"); |
151
|
|
|
$filePath = rtrim($filePath,'/'); |
152
|
|
|
$filePath .= "/"; |
153
|
|
|
file_put_contents($filePath . "{$this->repoName}.php", $template); |
154
|
|
|
break; |
155
|
|
|
case 'Controller': |
156
|
|
|
case 'Request': |
157
|
|
|
case 'Repository': |
158
|
|
|
case 'Interface': |
159
|
|
|
$filePath = $this->getFolderOrCreate(\Config::get('repository.app_path') . "/{$folder}/{$path}"); |
160
|
|
|
$filePath = rtrim($filePath,'/'); |
161
|
|
|
$filePath .= "/"; |
162
|
|
|
file_put_contents($filePath . "{$this->repoName}{$type}.php", $template); |
163
|
|
|
break; |
164
|
|
|
case 'create': |
165
|
|
|
case 'edit': |
166
|
|
|
case 'index': |
167
|
|
|
case 'show': |
168
|
|
|
$filePath = $this->getFolderOrCreate($folder."/".str_plural($path))."/"; |
169
|
|
|
$repoName = lcfirst($type); |
170
|
|
|
file_put_contents($filePath . $repoName.".blade.php", $template); |
171
|
|
|
break; |
172
|
|
|
default: |
173
|
|
|
$filePath = $this->getFolderOrCreate($folder)."/"; |
174
|
|
|
$repoName = lcfirst($this->repoName); |
175
|
|
|
file_put_contents($filePath . $repoName.".php", $template); |
176
|
|
|
} |
177
|
|
|
return true; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Check if folder exist |
182
|
|
|
* @param string $path class path |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getFolderOrCreate($path) |
186
|
|
|
{ |
187
|
|
|
if (!file_exists($path)) { |
188
|
|
|
mkdir($path, 0777, true); |
189
|
|
|
} |
190
|
|
|
return $path; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|