|
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 Bluz\Validator\Validator as v; |
|
10
|
|
|
use Bluzman\Input\InputArgument; |
|
11
|
|
|
use Bluzman\Input\InputException; |
|
12
|
|
|
use Bluzman\Generator; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ModelCommand |
|
18
|
|
|
* |
|
19
|
|
|
* @package Bluzman\Command\Generate |
|
20
|
|
|
*/ |
|
21
|
|
|
class GridCommand extends AbstractGenerateCommand |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Command configuration |
|
25
|
|
|
*/ |
|
26
|
14 |
|
protected function configure() |
|
27
|
|
|
{ |
|
28
|
|
|
$this |
|
29
|
|
|
// the name of the command (the part after "bin/bluzman") |
|
30
|
14 |
|
->setName('generate:grid') |
|
31
|
|
|
// the short description shown while running "php bin/bluzman list" |
|
32
|
14 |
|
->setDescription('Generate a GRID for model') |
|
33
|
|
|
// the full command description shown when running the command with |
|
34
|
|
|
// the "--help" option |
|
35
|
14 |
|
->setHelp('This command allows you to generate GRID files') |
|
36
|
|
|
; |
|
37
|
|
|
|
|
38
|
|
|
$this |
|
39
|
14 |
|
->addModelArgument() |
|
40
|
14 |
|
->addModuleArgument(InputArgument::OPTIONAL) |
|
41
|
|
|
; |
|
42
|
14 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param InputInterface $input |
|
46
|
|
|
* @param OutputInterface $output |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
2 |
|
$this->write("Running <info>generate:grid</info> command"); |
|
53
|
|
|
|
|
54
|
2 |
|
$model = $input->getArgument('model'); |
|
55
|
2 |
|
$this->getDefinition()->getArgument('model')->validate($model); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
1 |
|
if (!$this->getApplication()->isModelExists($model)) { |
|
58
|
|
|
throw new InputException( |
|
59
|
|
|
"Model $model is not exist, " . |
|
60
|
|
|
"run command <question>bluzman generate:model $model</question> before" |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
if ($module = $input->getArgument('module')) { |
|
65
|
|
|
$this->getDefinition()->getArgument('module')->validate($module); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
if (!$this->getApplication()->isModuleExists($module)) { |
|
68
|
|
|
throw new InputException( |
|
69
|
|
|
"Module $module is not exist, " . |
|
70
|
|
|
"run command <question>bluzman generate:module $module</question> before" |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// generate directories and files |
|
76
|
1 |
|
$this->generate($input, $output); |
|
77
|
|
|
|
|
78
|
|
|
// verify it |
|
79
|
1 |
|
$this->verify($input, $output); |
|
80
|
|
|
|
|
81
|
1 |
|
$this->write("GRID for <info>{$model}</info> has been successfully created."); |
|
82
|
|
|
|
|
83
|
1 |
|
if ($module = $input->getArgument('module')) { |
|
84
|
|
|
$this->write( |
|
85
|
1 |
|
"Open page <info>/acl</info> in your browser and set permissions for <info>{$module}</info>" |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
1 |
|
} catch (InputException $e) { |
|
89
|
1 |
|
$this->error("ERROR: {$e->getMessage()}"); |
|
90
|
|
|
} |
|
91
|
2 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param InputInterface $input |
|
95
|
|
|
* @param OutputInterface $output |
|
96
|
|
|
* @return void |
|
97
|
|
|
* @throws InputException |
|
98
|
|
|
*/ |
|
99
|
1 |
|
protected function generate(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
1 |
|
$model = ucfirst($input->getArgument('model')); |
|
102
|
|
|
|
|
103
|
|
|
// generate CRUD |
|
104
|
1 |
|
$crudFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php'; |
|
105
|
|
|
|
|
106
|
1 |
|
if (file_exists($crudFile)) { |
|
107
|
|
|
$this->comment("Crud file <info>$model/Grid.php</info> already exists"); |
|
108
|
|
|
} else { |
|
109
|
1 |
|
$template = $this->getTemplate('GridTemplate'); |
|
110
|
1 |
|
$template->setFilePath($crudFile); |
|
111
|
1 |
|
$template->setTemplateData([ |
|
112
|
1 |
|
'model' => $model |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
1 |
|
$generator = new Generator\Generator($template); |
|
116
|
1 |
|
$generator->make(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
if ($module = $input->getArgument('module')) { |
|
120
|
|
|
$this->write("Generate <info>$module/controllers/grid.php</info>"); |
|
121
|
|
|
|
|
122
|
|
|
$controllerFile = $this->getControllerPath($module, 'grid'); |
|
123
|
|
|
if (file_exists($controllerFile)) { |
|
124
|
|
|
$this->comment("Controller file <info>$module/grid</info> already exists"); |
|
125
|
|
|
} else { |
|
126
|
|
|
$template = new Generator\Template\GridControllerTemplate(); |
|
127
|
|
|
$template->setFilePath($controllerFile); |
|
128
|
|
|
$template->setTemplateData(['model' => $model]); |
|
129
|
|
|
|
|
130
|
|
|
$generator = new Generator\Generator($template); |
|
131
|
|
|
$generator->make(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->write("Generate <info>$module/views/grid.phtml</info>"); |
|
135
|
|
|
|
|
136
|
|
|
$viewFile = $this->getViewPath($module, 'grid'); |
|
137
|
|
|
if (file_exists($viewFile)) { |
|
138
|
|
|
$this->comment("View file <info>$module/grid</info> already exists"); |
|
139
|
|
|
} else { |
|
140
|
|
|
$template = new Generator\Template\GridViewTemplate(); |
|
141
|
|
|
$template->setFilePath($viewFile); |
|
142
|
|
|
$template->setTemplateData(['model' => $model]); |
|
143
|
|
|
|
|
144
|
|
|
$generator = new Generator\Generator($template); |
|
145
|
|
|
$generator->make(); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param InputInterface $input |
|
152
|
|
|
* @param OutputInterface $output |
|
153
|
|
|
* @return void |
|
154
|
|
|
* @throws \Bluzman\Generator\GeneratorException |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function verify(InputInterface $input, OutputInterface $output) |
|
157
|
|
|
{ |
|
158
|
1 |
|
$modelPath = $this->getApplication()->getModelPath($input->getArgument('model')); |
|
159
|
|
|
|
|
160
|
|
|
$paths = [ |
|
161
|
1 |
|
$modelPath . DS . 'Grid.php', |
|
162
|
|
|
]; |
|
163
|
|
|
|
|
164
|
1 |
|
foreach ($paths as $path) { |
|
165
|
1 |
|
if (!$this->getFs()->exists($path)) { |
|
166
|
1 |
|
throw new Generator\GeneratorException("File `$path` is not exists"); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
1 |
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: