1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Bluz PHP Team |
4
|
|
|
* @link https://github.com/bluzphp/bluzman |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Bluzman\Command\Generate; |
8
|
|
|
|
9
|
|
|
use Bluzman\Generator; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ControllerCommand |
15
|
|
|
* |
16
|
|
|
* @package Bluzman\Command\Generate |
17
|
|
|
*/ |
18
|
|
|
class ControllerCommand extends AbstractGenerateCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Command configuration |
22
|
|
|
*/ |
23
|
14 |
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this |
26
|
|
|
// the name of the command (the part after "bin/bluzman") |
27
|
14 |
|
->setName('generate:controller') |
28
|
|
|
// the short description shown while running "php bin/bluzman list" |
29
|
14 |
|
->setDescription('Generate a new controller') |
30
|
|
|
// the full command description shown when running the command with |
31
|
|
|
// the "--help" option |
32
|
14 |
|
->setHelp('This command allows you to generate controller files') |
33
|
|
|
; |
34
|
|
|
|
35
|
14 |
|
$this->addModuleArgument(); |
36
|
14 |
|
$this->addControllerArgument(); |
37
|
14 |
|
$this->addForceOption(); |
38
|
14 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param InputInterface $input |
42
|
|
|
* @param OutputInterface $output |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
1 |
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) : void |
|
|
|
|
46
|
|
|
{ |
47
|
1 |
|
$this->write('Running <info>generate:controller</info> command'); |
48
|
|
|
try { |
49
|
|
|
// validate |
50
|
1 |
|
$this->validateModuleArgument(); |
51
|
1 |
|
$this->validateControllerArgument(); |
52
|
|
|
|
53
|
|
|
// generate directories and files |
54
|
1 |
|
$this->generate($input, $output); |
55
|
|
|
|
56
|
|
|
// verify it |
57
|
1 |
|
$this->verify($input, $output); |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
$this->error("ERROR: {$e->getMessage()}"); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param InputInterface $input |
65
|
|
|
* @param OutputInterface $output |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
1 |
|
protected function generate(InputInterface $input, OutputInterface $output) : void |
|
|
|
|
69
|
|
|
{ |
70
|
1 |
|
$module = $input->getArgument('module'); |
71
|
1 |
|
$controller = $input->getArgument('controller'); |
72
|
|
|
|
73
|
1 |
|
$controllerFile = $this->getControllerPath($module, $controller); |
74
|
1 |
|
$this->generateFile('ControllerTemplate', $controllerFile); |
75
|
|
|
|
76
|
1 |
|
$viewFile = $this->getViewPath($module, $controller); |
77
|
1 |
|
$this->generateFile('ViewTemplate', $viewFile, ['name' => $controller]); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param InputInterface $input |
82
|
|
|
* @param OutputInterface $output |
83
|
|
|
* @return void |
84
|
|
|
* @throws \Bluzman\Generator\GeneratorException |
85
|
|
|
*/ |
86
|
1 |
|
public function verify(InputInterface $input, OutputInterface $output) : void |
87
|
|
|
{ |
88
|
1 |
|
$module = $input->getArgument('module'); |
89
|
1 |
|
$controller = $input->getArgument('controller'); |
90
|
|
|
|
91
|
1 |
|
$modulePath = $this->getApplication()->getModulePath($module); |
92
|
|
|
|
93
|
|
|
$paths = [ |
94
|
1 |
|
$modulePath, |
95
|
1 |
|
$modulePath . DS . 'controllers', |
96
|
1 |
|
$modulePath . DS . 'controllers' . DS . $controller . '.php', |
97
|
1 |
|
$modulePath . DS . 'views', |
98
|
1 |
|
$modulePath . DS . 'views' . DS . $controller . '.phtml', |
99
|
|
|
|
100
|
|
|
]; |
101
|
|
|
|
102
|
1 |
|
foreach ($paths as $path) { |
103
|
1 |
|
if (!$this->getFs()->exists($path)) { |
104
|
|
|
throw new Generator\GeneratorException("File or directory `$path` is not exists"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$this->write( |
109
|
1 |
|
" |> Controller <info>{$controller}</info> has been successfully created " . |
110
|
1 |
|
"in the module <info>{$module}</info>." |
111
|
|
|
); |
112
|
1 |
|
} |
113
|
|
|
} |
114
|
|
|
|
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.