1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Prateekkarki\Laragen\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class Generate extends Command |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'laragen:make'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Generate code for your project'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new command instance. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command. |
35
|
|
|
* |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function handle() |
39
|
|
|
{ |
40
|
|
|
$config = config('laragen'); |
41
|
|
|
foreach ($config['modules'] as $model => $module) { |
42
|
|
|
$this->migration(ucfirst(str_singular($model)), $module); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function migration($model, $module) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$migrationTemplate = str_replace( |
49
|
|
|
[ |
50
|
|
|
'{{modelName}}', |
51
|
|
|
'{{modelNamePlural}}', |
52
|
|
|
'{{modelNamePluralLowerCase}}', |
53
|
|
|
'{{modelTableData}}', |
54
|
|
|
'{{modelTimestampType}}' |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
$model, |
58
|
|
|
str_plural($model), |
59
|
|
|
strtolower(str_plural($model)), |
60
|
|
|
'', |
61
|
|
|
'' |
62
|
|
|
], |
63
|
|
|
$this->getStub('Migration') |
64
|
|
|
); |
65
|
|
|
file_put_contents($this->laravel->databasePath(). "/migrations/" . date('Y_m_d_His') . "_create_" . strtolower(str_plural($model)) . "_table.php", $migrationTemplate); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function model($model) |
69
|
|
|
{ |
70
|
|
|
$modelTemplate = str_replace( |
71
|
|
|
['{{modelName}}'], |
72
|
|
|
[$model], |
73
|
|
|
$this->getStub('Model') |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
file_put_contents(app_path("/Models/{$model}.php"), $modelTemplate); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function controller($model) |
80
|
|
|
{ |
81
|
|
|
$controllerTemplate = str_replace( |
82
|
|
|
[ |
83
|
|
|
'{{modelName}}', |
84
|
|
|
'{{modelNamePluralLowerCase}}', |
85
|
|
|
'{{modelNameSingularLowerCase}}' |
86
|
|
|
], |
87
|
|
|
[ |
88
|
|
|
$model, |
89
|
|
|
strtolower(str_plural($model)), |
90
|
|
|
strtolower($model) |
91
|
|
|
], |
92
|
|
|
$this->getStub('Controller') |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
file_put_contents(app_path("/Http/Controllers/{$model}Controller.php"), $controllerTemplate); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getStub($type) |
99
|
|
|
{ |
100
|
|
|
return file_get_contents(__DIR__ . "/../resources/stubs/" . $type . ".stub"); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.