1
|
|
|
<?php |
2
|
|
|
namespace Prateekkarki\Laragen\Generators\Common; |
3
|
|
|
|
4
|
|
|
use Prateekkarki\Laragen\Generators\BaseGenerator; |
5
|
|
|
use Prateekkarki\Laragen\Generators\GeneratorInterface; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class Model extends BaseGenerator implements GeneratorInterface |
9
|
|
|
{ |
10
|
|
|
public function generate() |
11
|
|
|
{ |
12
|
|
|
$generatedFiles = []; |
13
|
|
|
$modelTemplate = $this->buildTemplate('common/Models/Model', [ |
14
|
|
|
'{{modelName}}' => $this->module->getModelName(), |
15
|
|
|
'{{massAssignables}}' => implode("', '", $this->module->getColumns(true, true)), |
16
|
|
|
'{{usedModels}}' => $this->getUsedModels(), |
17
|
|
|
'{{foreignMethods}}' => $this->getForeignMethods() |
18
|
|
|
]); |
19
|
|
|
|
20
|
|
|
$fullFilePath = $this->getPath("app/Models/").$this->module->getModelName().".php"; |
21
|
|
|
file_put_contents($fullFilePath, $modelTemplate); |
22
|
|
|
$generatedFiles[] = $fullFilePath; |
23
|
|
|
|
24
|
|
|
foreach($this->module->getFilteredColumns('hasPivot') as $type){ |
25
|
|
|
$typeTemplate = $this->buildTemplate('common/Models/Pivot', [ |
26
|
|
|
'{{pivotName}}' => $type->getPivot(), |
27
|
|
|
'{{massAssignables}}' => implode("', '", $type->getTypeColumns()), |
28
|
|
|
'{{foreignMethods}}' => $this->getTypeForeignMethods($type), |
29
|
|
|
]); |
30
|
|
|
$fullFilePath = $this->getPath("app/Models/").$type->getPivot().".php"; |
31
|
|
|
file_put_contents($fullFilePath, $typeTemplate); |
32
|
|
|
$generatedFiles[] = $fullFilePath; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach($this->module->getFilteredColumns(['hasModel', 'hasOptions']) as $type){ |
36
|
|
|
$pivotModel = Str::singular($type->getPivot()); |
37
|
|
|
$typeTemplate = $this->buildTemplate('common/Models/Model', [ |
38
|
|
|
'{{modelName}}' => $pivotModel, |
39
|
|
|
'{{massAssignables}}' => implode("', '", $type->getTypeColumns()), |
40
|
|
|
'{{usedModels}}' => $this->getUsedModels($pivotModel), |
41
|
|
|
'{{foreignMethods}}' => $this->getTypeForeignMethods($type), |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$fullFilePath = $this->getPath("app/Models/").$pivotModel.".php"; |
45
|
|
|
file_put_contents($fullFilePath, $typeTemplate); |
46
|
|
|
$generatedFiles[] = $fullFilePath; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $generatedFiles; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getTypeForeignMethods($type) |
53
|
|
|
{ |
54
|
|
|
$foreignMethods = ""; |
55
|
|
|
$stub = $type->getStub('modelMethod') ?: 'common/Models/fragments/belongsTo'; |
56
|
|
|
$foreignMethods .= $this->buildTemplate($stub, [ |
57
|
|
|
'{{columnName}}' => $type->getColumn(), |
58
|
|
|
'{{parent}}' => $type->getParentModelLowercase(), |
59
|
|
|
'{{parentModel}}' => $type->getParentModel(), |
60
|
|
|
'{{relatedModel}}' => $type->getRelatedModel(), |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
return $foreignMethods; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getUsedModels($pivotModel = false) { |
67
|
|
|
$usedModels = ""; |
68
|
|
|
$classes = []; |
69
|
|
|
foreach($this->module->getFilteredColumns(['hasSingleRelation', 'hasPivot', 'hasModel']) as $type){ |
70
|
|
|
$model = $type->getRelatedModel(); |
71
|
|
|
$class = ($model == 'User') ? config('laragen.options.user_model') : "App\\Models\\".$model; |
72
|
|
|
if(in_array($class, $classes) || $model == $this->module->getModelName() || $model == $pivotModel){ |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
$classes[] = $class; |
76
|
|
|
$usedModels .= PHP_EOL."use ".$class.";"; |
77
|
|
|
} |
78
|
|
|
return $usedModels; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getForeignMethods() |
82
|
|
|
{ |
83
|
|
|
$foreignMethods = ""; |
84
|
|
|
foreach($this->module->getFilteredColumns(['hasPivot', 'hasSingleRelation', 'hasModel']) as $type){ |
85
|
|
|
$stub = $type->getStub('foreignMethod') ?: 'common/Models/fragments/hasOne'; |
86
|
|
|
$foreignMethods .= $this->buildTemplate($stub, [ |
87
|
|
|
'{{columnName}}' => $type->getColumn(), |
88
|
|
|
'{{parent}}' => $type->getParentModelLowercase(), |
89
|
|
|
'{{relatedModel}}' => $type->getRelatedModel(), |
90
|
|
|
'{{table}}' => $type->getPivotTable(), |
91
|
|
|
'{{parentModel}}' => $type->getParentModel(), |
92
|
|
|
'{{parentId}}' => $type->getParentModelLowercase() . "_id", |
93
|
|
|
'{{childId}}' => $type->getChildKey(), |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
return $foreignMethods; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|