1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author jan huang <[email protected]> |
4
|
|
|
* @copyright 2016 |
5
|
|
|
* |
6
|
|
|
* @link https://www.github.com/janhuang |
7
|
|
|
* @link http://www.fast-d.cn/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FastD\Console; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ModelCreate |
20
|
|
|
* @package FastD\Console |
21
|
|
|
*/ |
22
|
|
|
class ModelCreate extends Command |
23
|
|
|
{ |
24
|
|
|
public function configure() |
25
|
|
|
{ |
26
|
|
|
$this->setName('model:create'); |
27
|
|
|
$this->addArgument('name', InputArgument::REQUIRED); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$modelPath = app()->getPath() . '/src/Model'; |
33
|
|
|
if (!file_exists($modelPath)) { |
34
|
|
|
mkdir($modelPath, 0755, true); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$name = ucfirst($input->getArgument('name')) . 'Model'; |
38
|
|
|
$content = $this->createModelTemplate($name); |
39
|
|
|
|
40
|
|
|
$modelFile = $modelPath . '/' . $name . '.php'; |
41
|
|
|
|
42
|
|
|
if (file_exists($modelFile)) { |
43
|
|
|
throw new \LogicException(sprintf('Model %s is already exists', $name)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
file_put_contents($modelFile, $content); |
47
|
|
|
$output->writeln(sprintf('Model %s created successful. path in %s', $name, $modelPath)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function createModelTemplate($name) |
51
|
|
|
{ |
52
|
|
|
$table = strtolower(str_replace('Model', '', $name)); |
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
|
|
|
'OR' => [ |
87
|
|
|
'id' => \$id, |
88
|
|
|
] |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
return \$this->find(\$id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function create(array \$data) |
95
|
|
|
{ |
96
|
|
|
\$id = \$this->db->insert(static::TABLE, \$data); |
97
|
|
|
|
98
|
|
|
return \$this->find(\$id); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function delete(\$id) |
102
|
|
|
{ |
103
|
|
|
return \$this->db->delete(static::TABLE, [ |
104
|
|
|
'id' => \$id |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
MODEL; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
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.