1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Question\Question; |
10
|
|
|
use App\Common\Helper; |
11
|
|
|
use App\Console\Traits\CodeGenerate; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* GenerateModelCommand |
15
|
|
|
*/ |
16
|
|
|
class GenerateModelCommand extends Command |
17
|
|
|
{ |
18
|
|
|
use CodeGenerate; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Configuration of command |
22
|
|
|
*/ |
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this |
26
|
|
|
->setName('generate:model') |
27
|
|
|
->setDescription('Command for generate model') |
28
|
|
|
; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute method of command |
33
|
|
|
* |
34
|
|
|
* @param InputInterface $input |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
$output->writeln(['<comment>Welcome to the model generator</comment>']); |
43
|
|
|
|
44
|
|
|
$helper = $this->getHelper('question'); |
45
|
|
|
$question = new Question('<info>Please enter table name: </info>'); |
46
|
|
|
$tableName = $helper->ask($input, $output, $question); |
47
|
|
|
|
48
|
|
|
$table = Capsule::schema()->getColumnListing($tableName); |
49
|
|
|
if (count($table) === 0) { |
50
|
|
|
$output->writeln([sprintf('<comment>Not found table `%s`</comment>', $tableName)]); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$columns = []; |
55
|
|
View Code Duplication |
foreach ($table as $columnName) { |
|
|
|
|
56
|
|
|
$columnType = Capsule::schema()->getColumnType($tableName, $columnName); |
57
|
|
|
$columns[] = [ |
58
|
|
|
'name' => $columnName, |
59
|
|
|
'type' => $columnType !== 'datetime' ? $columnType : '\Carbon\Carbon', |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$modelName = substr($tableName, 0, -1); |
64
|
|
|
$className = Helper::underscoreToCamelCase($modelName, true); |
65
|
|
|
$baseName = $className.'.php'; |
66
|
|
|
$path = $this->getPath($baseName, MODELS_PATH); |
67
|
|
|
|
68
|
|
|
$placeHolders = [ |
69
|
|
|
'<class>', |
70
|
|
|
'<tableName>', |
71
|
|
|
'<phpdoc>', |
72
|
|
|
'<fillable>', |
73
|
|
|
]; |
74
|
|
|
$replacements = [ |
75
|
|
|
$className, |
76
|
|
|
strtolower($tableName), |
77
|
|
|
$this->generatePhpDoc($columns), |
78
|
|
|
$this->generateFillable($columns), |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$this->generateCode($placeHolders, $replacements, 'ModelTemplate.tpl', $path); |
82
|
|
|
|
83
|
|
|
$output->writeln(sprintf('Generated new model class to "<info>%s</info>"', realpath($path))); |
84
|
|
|
|
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $columns |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
private function generatePhpDoc($columns) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$phpdoc = []; |
95
|
|
|
foreach ($columns as $column) { |
96
|
|
|
$phpdoc[] = sprintf(" * @property %s $%s", $column['type'], $column['name']); |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
return implode("\n", $phpdoc); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $columns |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
private function generateFillable($columns) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$fillable = []; |
109
|
|
|
foreach ($columns as $column) { |
110
|
|
|
$fillable[] = sprintf(" '%s',", $column['name']); |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
return implode("\n", $fillable); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.