|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Milkmeowo\Framework\Repository\Generators\Migrations\SchemaParser; |
|
6
|
|
|
|
|
7
|
|
|
class ModelGenerator extends Generator |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Get stub name. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $stub = 'model'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get root namespace. |
|
18
|
|
|
* |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getRootNamespace() |
|
22
|
|
|
{ |
|
23
|
|
|
return parent::getRootNamespace().parent::getConfigGeneratorClassPath($this->getPathConfigNode()); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get generator path config node. |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getPathConfigNode() |
|
32
|
|
|
{ |
|
33
|
|
|
return 'models'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get destination path for generated file. |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getPath() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->getBasePath().'/'.parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true).'/'.$this->getName().'.php'; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get base path of destination file. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getBasePath() |
|
52
|
|
|
{ |
|
53
|
|
|
return config('repository.generator.basePath', app()->path()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get array replacements. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getReplacements() |
|
62
|
|
|
{ |
|
63
|
|
|
return array_merge(parent::getReplacements(), [ |
|
64
|
|
|
'fillable' => $this->getFillable(), |
|
65
|
|
|
'use_base_model' => 'use '.$this->getRootNamespace().'\BaseModel;', |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the fillable attributes. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getFillable() |
|
75
|
|
|
{ |
|
76
|
|
|
if (! $this->fillable) { |
|
|
|
|
|
|
77
|
|
|
return '[]'; |
|
78
|
|
|
} |
|
79
|
|
|
$results = '['.PHP_EOL; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($this->getSchemaParser()->toArray() as $column => $value) { |
|
82
|
|
|
$results .= "\t\t'{$column}',".PHP_EOL; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $results."\t".']'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get schema parser. |
|
90
|
|
|
* |
|
91
|
|
|
* @return SchemaParser |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getSchemaParser() |
|
94
|
|
|
{ |
|
95
|
|
|
return new SchemaParser($this->fillable); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.