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 CrudCommand 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:crud') |
31
|
|
|
// the short description shown while running "php bin/bluzman list" |
32
|
14 |
|
->setDescription('Generate a CRUD 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 CRUD files'); |
36
|
|
|
|
37
|
|
|
$this |
38
|
14 |
|
->addModelArgument() |
39
|
14 |
|
->addModuleArgument(InputArgument::OPTIONAL) |
40
|
|
|
; |
41
|
14 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param InputInterface $input |
45
|
|
|
* @param OutputInterface $output |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
3 |
|
$this->write("Running <info>generate:crud</info> command"); |
52
|
|
|
|
53
|
3 |
|
$model = $input->getArgument('model'); |
54
|
3 |
|
$this->getDefinition()->getArgument('model')->validate($model); |
|
|
|
|
55
|
|
|
|
56
|
1 |
|
if (!$this->getApplication()->isModelExists($model)) { |
57
|
|
|
throw new InputException( |
58
|
|
|
"Model $model is not exist, " . |
59
|
|
|
"run command <question>bluzman generate:model $model</question> before" |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
if ($module = $input->getArgument('module')) { |
64
|
|
|
$this->getDefinition()->getArgument('module')->validate($module); |
|
|
|
|
65
|
|
|
|
66
|
|
|
if (!$this->getApplication()->isModuleExists($module)) { |
67
|
|
|
throw new InputException( |
68
|
|
|
"Module $module is not exist, " . |
69
|
|
|
"run command <question>bluzman generate:module $module</question> before" |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// generate directories and files |
75
|
1 |
|
$this->generate($input, $output); |
76
|
|
|
|
77
|
|
|
// verify it |
78
|
1 |
|
$this->verify($input, $output); |
79
|
|
|
|
80
|
1 |
|
$this->write("CRUD for <info>{$model}</info> has been successfully created."); |
81
|
1 |
|
if ($module = $input->getArgument('module')) { |
82
|
|
|
$this->write( |
83
|
1 |
|
"Open page <info>/acl</info> in your browser and set permissions for <info>{$module}</info>" |
84
|
|
|
); |
85
|
|
|
} |
86
|
2 |
|
} catch (InputException $e) { |
87
|
2 |
|
$this->error("ERROR: {$e->getMessage()}"); |
88
|
|
|
} |
89
|
3 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param InputInterface $input |
93
|
|
|
* @param OutputInterface $output |
94
|
|
|
* @return void |
95
|
|
|
* @throws InputException |
96
|
|
|
*/ |
97
|
1 |
|
protected function generate(InputInterface $input, OutputInterface $output) |
|
|
|
|
98
|
|
|
{ |
99
|
1 |
|
$model = ucfirst($input->getArgument('model')); |
100
|
|
|
|
101
|
|
|
// generate CRUD |
102
|
1 |
|
$crudFile = $this->getApplication()->getModelPath($model) . DS . 'Crud.php'; |
103
|
|
|
|
104
|
1 |
|
if (file_exists($crudFile)) { |
105
|
|
|
$this->comment("Crud file <info>$model/Crud.php</info> already exists"); |
106
|
|
|
} else { |
107
|
1 |
|
$template = $this->getTemplate('CrudTemplate'); |
108
|
1 |
|
$template->setFilePath($crudFile); |
109
|
1 |
|
$template->setTemplateData( |
110
|
|
|
[ |
111
|
1 |
|
'model' => $model |
112
|
|
|
] |
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/crud.php</info>"); |
121
|
|
|
|
122
|
|
|
$controllerFile = $this->getControllerPath($module, 'crud'); |
123
|
|
|
if (file_exists($controllerFile)) { |
124
|
|
|
$this->comment("Controller file <info>$module/crud</info> already exists"); |
125
|
|
|
} else { |
126
|
|
|
$template = new Generator\Template\CrudControllerTemplate(); |
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/crud.phtml</info>"); |
135
|
|
|
|
136
|
|
|
$viewFile = $this->getViewPath($module, 'crud'); |
137
|
|
|
if (file_exists($viewFile)) { |
138
|
|
|
$this->comment("View file <info>$module/crud</info> already exists"); |
139
|
|
|
} else { |
140
|
|
|
$template = new Generator\Template\CrudViewTemplate(); |
141
|
|
|
$template->setFilePath($viewFile); |
142
|
|
|
$template->setTemplateData( |
143
|
|
|
[ |
144
|
|
|
'model' => $model, |
145
|
|
|
'module' => $module |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$generator = new Generator\Generator($template); |
150
|
|
|
$generator->make(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param InputInterface $input |
157
|
|
|
* @param OutputInterface $output |
158
|
|
|
* @return void |
159
|
|
|
* @throws \Bluzman\Generator\GeneratorException |
160
|
|
|
*/ |
161
|
1 |
|
public function verify(InputInterface $input, OutputInterface $output) |
162
|
|
|
{ |
163
|
1 |
|
$modelPath = $this->getApplication()->getModelPath($input->getArgument('model')); |
164
|
|
|
|
165
|
|
|
$paths = [ |
166
|
1 |
|
$modelPath . DS . 'Crud.php', |
167
|
|
|
]; |
168
|
|
|
|
169
|
1 |
|
foreach ($paths as $path) { |
170
|
1 |
|
if (!$this->getFs()->exists($path)) { |
171
|
1 |
|
throw new Generator\GeneratorException("File `$path` is not exists"); |
172
|
|
|
} |
173
|
|
|
} |
174
|
1 |
|
} |
175
|
|
|
} |
176
|
|
|
|
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: