From f6981bcfa6fb80909d66ea33b2d1e4d99f1283ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harold=20Ju=C3=A1rez?= Date: Wed, 29 May 2019 12:31:49 -0600 Subject: [PATCH] [generate:controller] Validate class file is already exist (#4066) --- src/Command/Generate/ControllerCommand.php | 6 ++++-- src/Utils/Validator.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Command/Generate/ControllerCommand.php b/src/Command/Generate/ControllerCommand.php index 36730f995..4607febc7 100644 --- a/src/Command/Generate/ControllerCommand.php +++ b/src/Command/Generate/ControllerCommand.php @@ -137,6 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $module = $this->validateModule($input->getOption('module')); $class = $this->validator->validateControllerName($input->getOption('class')); + $this->validator->validateControllerClassExists($class, $module); $routes = $input->getOption('routes'); $test = $input->getOption('test'); $services = $input->getOption('services'); @@ -176,8 +177,9 @@ protected function interact(InputInterface $input, OutputInterface $output) $class = $this->getIo()->ask( $this->trans('commands.generate.controller.questions.class'), 'DefaultController', - function ($class) { - return $this->validator->validateControllerName($class); + function ($class) use($module) { + $class = $this->validator->validateControllerName($class); + return $this->validator->validateControllerClassExists($class, $module); } ); $input->setOption('class', $class); diff --git a/src/Utils/Validator.php b/src/Utils/Validator.php index 63a143b33..e7e185b57 100644 --- a/src/Utils/Validator.php +++ b/src/Utils/Validator.php @@ -132,6 +132,23 @@ public function validateControllerName($class_name) } } + public function validateControllerClassExists($class_name, $module_name) + { + $class_exists = $this->extensionManager->getModule($module_name)->getControllerPath() . '/' . $class_name . '.php'; + + if (!$class_exists) { + return $class_name; + } else { + throw new \InvalidArgumentException( + sprintf( + 'Controller file with the name "%s.php" is already exist. Enter a different controller class name', + $class_name, + $module_name + ) + ); + } + } + public function validateMachineName($machine_name) { if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {