1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author jan huang <[email protected]> |
4
|
|
|
* @copyright 2016 |
5
|
|
|
* |
6
|
|
|
* @see https://www.github.com/janhuang |
7
|
|
|
* @see https://fastdlabs.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FastD\Console; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ModelCreate. |
19
|
|
|
*/ |
20
|
|
|
class Model extends Command |
21
|
|
|
{ |
22
|
|
|
public function configure() |
23
|
|
|
{ |
24
|
|
|
$this->setName('model'); |
25
|
|
|
$this->setDescription('Automate create base CURD model class.'); |
26
|
|
|
$this->addArgument('name', InputArgument::REQUIRED); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$modelPath = app()->getPath().'/src/Model'; |
32
|
|
|
if (!file_exists($modelPath)) { |
33
|
|
|
mkdir($modelPath, 0755, true); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$name = ucfirst($input->getArgument('name')).'Model'; |
37
|
|
|
$content = $this->createModelTemplate($name); |
38
|
|
|
|
39
|
|
|
$modelFile = $modelPath.'/'.$name.'.php'; |
40
|
|
|
|
41
|
|
|
if (file_exists($modelFile)) { |
42
|
|
|
throw new \LogicException(sprintf('Model %s is already exists', $name)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
file_put_contents($modelFile, $content); |
46
|
|
|
$output->writeln(sprintf('Model %s created successful. path in %s', $name, $modelPath)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function createModelTemplate($name) |
50
|
|
|
{ |
51
|
|
|
$table = strtolower(str_replace('Model', '', $name)); |
52
|
|
|
|
53
|
|
|
return <<<MODEL |
54
|
|
|
<?php |
55
|
|
|
|
56
|
|
|
namespace Model; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
use FastD\Model\Model; |
60
|
|
|
|
61
|
|
|
class {$name} extends Model |
62
|
|
|
{ |
63
|
|
|
const TABLE = '{$table}'; |
64
|
|
|
const LIMIT = '15'; |
65
|
|
|
|
66
|
|
|
public function select(\$page = 1) |
67
|
|
|
{ |
68
|
|
|
\$offset = (\$page - 1) * static::LIMIT; |
69
|
|
|
return \$this->db->select(static::TABLE, '*', [ |
70
|
|
|
'LIMIT' => [\$offset, static::LIMIT] |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function find(\$id) |
75
|
|
|
{ |
76
|
|
|
return \$this->db->get(static::TABLE, '*', [ |
77
|
|
|
'OR' => [ |
78
|
|
|
'id' => \$id, |
79
|
|
|
] |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function patch(\$id, array \$data) |
84
|
|
|
{ |
85
|
|
|
\$affected = \$this->db->update(static::TABLE, \$data, [ |
86
|
|
|
'id' => \$id, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
return \$this->find(\$id); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function create(array \$data) |
93
|
|
|
{ |
94
|
|
|
\$this->db->insert(static::TABLE, \$data); |
95
|
|
|
|
96
|
|
|
return \$this->find(\$this->db->id()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function delete(\$id) |
100
|
|
|
{ |
101
|
|
|
return \$this->db->delete(static::TABLE, [ |
102
|
|
|
'id' => \$id |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
MODEL; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.