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\Input\InputArgument; |
10
|
|
|
use Bluzman\Input\InputException; |
11
|
|
|
use Bluzman\Generator; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ModelCommand |
17
|
|
|
* |
18
|
|
|
* @package Bluzman\Command\Generate |
19
|
|
|
*/ |
20
|
|
|
class GridCommand extends AbstractGenerateCommand |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Command configuration |
24
|
|
|
*/ |
25
|
14 |
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
|
|
// the name of the command (the part after "bin/bluzman") |
29
|
14 |
|
->setName('generate:grid') |
30
|
|
|
// the short description shown while running "php bin/bluzman list" |
31
|
14 |
|
->setDescription('Generate a GRID for model') |
32
|
|
|
// the full command description shown when running the command with |
33
|
|
|
// the "--help" option |
34
|
14 |
|
->setHelp('This command allows you to generate GRID files') |
35
|
|
|
; |
36
|
|
|
|
37
|
14 |
|
$this->addModelArgument(); |
38
|
14 |
|
$this->addModuleArgument(InputArgument::OPTIONAL); |
39
|
14 |
|
$this->addForceOption(); |
40
|
14 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param InputInterface $input |
44
|
|
|
* @param OutputInterface $output |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
* @throws \Bluzman\Generator\GeneratorException |
48
|
|
|
*/ |
49
|
2 |
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) : void |
|
|
|
|
50
|
|
|
{ |
51
|
2 |
|
$this->write('Running <info>generate:grid</info> command'); |
52
|
|
|
try { |
53
|
|
|
// validate |
54
|
2 |
|
$this->validateModelArgument(); |
55
|
1 |
|
$this->validateModuleArgument(); |
56
|
|
|
|
57
|
|
|
// generate directories and files |
58
|
1 |
|
$this->generate($input, $output); |
59
|
|
|
|
60
|
|
|
// verify files |
61
|
1 |
|
$this->verify($input, $output); |
62
|
1 |
|
} catch (InputException $e) { |
63
|
1 |
|
$this->error("ERROR: {$e->getMessage()}"); |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param InputInterface $input |
69
|
|
|
* @param OutputInterface $output |
70
|
|
|
* @return void |
71
|
|
|
* @throws InputException |
72
|
|
|
*/ |
73
|
1 |
|
protected function generate(InputInterface $input, OutputInterface $output) : void |
|
|
|
|
74
|
|
|
{ |
75
|
1 |
|
$model = ucfirst($input->getArgument('model')); |
76
|
1 |
|
$module = $input->getArgument('module'); |
77
|
|
|
|
78
|
|
|
// template data |
79
|
|
|
$data = [ |
80
|
1 |
|
'model' => $model, |
81
|
1 |
|
'module' => $module |
82
|
|
|
]; |
83
|
|
|
|
84
|
1 |
|
if ($module) { |
85
|
|
|
// controller and view generators required the `Model\Table` class |
86
|
|
|
// validator is present on previous step |
87
|
|
|
$data['columns'] = $this->getTableInstance($model)::getMeta(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// generate GRID class |
91
|
1 |
|
$this->write(" |> Generate Grid class <info>$model\\Grid</info>"); |
92
|
|
|
|
93
|
1 |
|
$gridFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php'; |
94
|
1 |
|
$this->generateFile('GridTemplate', $gridFile, $data); |
95
|
|
|
|
96
|
1 |
|
if ($module) { |
97
|
|
|
$this->write(" |> Generate Grid controller <info>$module/controllers/grid.php</info>"); |
98
|
|
|
|
99
|
|
|
$controllerFile = $this->getControllerPath($module, 'grid'); |
100
|
|
|
$this->generateFile('GridControllerTemplate', $controllerFile, $data); |
101
|
|
|
|
102
|
|
|
$this->write(" |> Generate Grid view <info>$module/views/grid.phtml</info>"); |
103
|
|
|
|
104
|
|
|
$viewFile = $this->getViewPath($module, 'grid'); |
105
|
|
|
$this->generateFile('GridViewTemplate', $viewFile, $data); |
106
|
|
|
} |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param InputInterface $input |
111
|
|
|
* @param OutputInterface $output |
112
|
|
|
* @return void |
113
|
|
|
* @throws \Bluzman\Generator\GeneratorException |
114
|
|
|
*/ |
115
|
1 |
View Code Duplication |
public function verify(InputInterface $input, OutputInterface $output) : void |
|
|
|
|
116
|
|
|
{ |
117
|
1 |
|
$model = $input->getArgument('model'); |
118
|
1 |
|
$module = $input->getArgument('module'); |
119
|
|
|
|
120
|
1 |
|
$modelPath = $this->getApplication()->getModelPath($model); |
121
|
|
|
|
122
|
|
|
$paths = [ |
123
|
1 |
|
$modelPath . DS . 'Grid.php', |
124
|
|
|
]; |
125
|
|
|
|
126
|
1 |
|
foreach ($paths as $path) { |
127
|
1 |
|
if (!$this->getFs()->exists($path)) { |
128
|
|
|
throw new Generator\GeneratorException("File `$path` is not exists"); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// notifications |
133
|
1 |
|
$this->write(" |> GRID for <info>{$model}</info> has been successfully created."); |
134
|
|
|
|
135
|
1 |
|
if ($module) { |
136
|
|
|
$this->write( |
137
|
|
|
" |> <options=bold>Open page <info>/acl</info> in your browser " . |
138
|
|
|
"and set permission <info>Management</info> for <info>{$module}</info> module</>" |
139
|
|
|
); |
140
|
|
|
} |
141
|
1 |
|
} |
142
|
|
|
} |
143
|
|
|
|
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.