Skip to content

Commit f6981bc

Browse files
hjuarez20enzolutions
authored andcommitted
[generate:controller] Validate class file is already exist (#4066)
1 parent df8b95b commit f6981bc

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Command/Generate/ControllerCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
137137

138138
$module = $this->validateModule($input->getOption('module'));
139139
$class = $this->validator->validateControllerName($input->getOption('class'));
140+
$this->validator->validateControllerClassExists($class, $module);
140141
$routes = $input->getOption('routes');
141142
$test = $input->getOption('test');
142143
$services = $input->getOption('services');
@@ -176,8 +177,9 @@ protected function interact(InputInterface $input, OutputInterface $output)
176177
$class = $this->getIo()->ask(
177178
$this->trans('commands.generate.controller.questions.class'),
178179
'DefaultController',
179-
function ($class) {
180-
return $this->validator->validateControllerName($class);
180+
function ($class) use($module) {
181+
$class = $this->validator->validateControllerName($class);
182+
return $this->validator->validateControllerClassExists($class, $module);
181183
}
182184
);
183185
$input->setOption('class', $class);

src/Utils/Validator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ public function validateControllerName($class_name)
132132
}
133133
}
134134

135+
public function validateControllerClassExists($class_name, $module_name)
136+
{
137+
$class_exists = $this->extensionManager->getModule($module_name)->getControllerPath() . '/' . $class_name . '.php';
138+
139+
if (!$class_exists) {
140+
return $class_name;
141+
} else {
142+
throw new \InvalidArgumentException(
143+
sprintf(
144+
'Controller file with the name "%s.php" is already exist. Enter a different controller class name',
145+
$class_name,
146+
$module_name
147+
)
148+
);
149+
}
150+
}
151+
135152
public function validateMachineName($machine_name)
136153
{
137154
if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {

0 commit comments

Comments
 (0)