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
|
|
|
class Controller extends Command |
18
|
|
|
{ |
19
|
|
|
public function configure() |
20
|
|
|
{ |
21
|
|
|
$this->setName('controller'); |
22
|
|
|
$this->setDescription('Automate create base controller class.'); |
23
|
|
|
$this->addArgument('name', InputArgument::REQUIRED); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$controllerPath = app()->getPath().'/src/Controller'; |
29
|
|
|
if (!file_exists($controllerPath)) { |
30
|
|
|
mkdir($controllerPath, 0755, true); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$name = ucfirst($input->getArgument('name')).'Controller'; |
34
|
|
|
$content = $this->createControllerTemplate($name); |
35
|
|
|
|
36
|
|
|
$controllerFile = $controllerPath.'/'.$name.'.php'; |
37
|
|
|
|
38
|
|
|
if (file_exists($controllerFile)) { |
39
|
|
|
throw new \LogicException(sprintf('Controller %s is already exists', $name)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
file_put_contents($controllerFile, $content); |
43
|
|
|
$output->writeln(sprintf('Controller %s created successful. path in %s', $name, $controllerPath)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function createControllerTemplate($name) |
47
|
|
|
{ |
48
|
|
|
return <<<CONTROLLER |
49
|
|
|
<?php |
50
|
|
|
|
51
|
|
|
namespace Controller; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
use FastD\Http\Response; |
55
|
|
|
use FastD\Http\ServerRequest; |
56
|
|
|
|
57
|
|
|
class {$name} |
58
|
|
|
{ |
59
|
|
|
public function create(ServerRequest \$request) |
60
|
|
|
{ |
61
|
|
|
return json([]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function patch(ServerRequest \$request) |
65
|
|
|
{ |
66
|
|
|
return json([]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function delete(ServerRequest \$request) |
70
|
|
|
{ |
71
|
|
|
return json([]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function find(ServerRequest \$request) |
75
|
|
|
{ |
76
|
|
|
return json([]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function select(ServerRequest \$request) |
80
|
|
|
{ |
81
|
|
|
return json([]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
CONTROLLER; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.