Beispiel #1
0
 /**
  * Create an action method
  *
  * @return ConsoleModel
  */
 public function methodAction()
 {
     // check for help mode
     if ($this->requestOptions->getFlagHelp()) {
         return $this->methodHelp();
     }
     // output header
     $this->consoleHeader('Creating new action');
     // get needed options to shorten code
     $path = $this->requestOptions->getPath();
     $moduleName = $this->requestOptions->getModuleName();
     $controllerName = $this->requestOptions->getControllerName();
     $controllerPath = $this->requestOptions->getControllerPath();
     $controllerClass = $this->requestOptions->getControllerClass();
     $controllerFile = $this->requestOptions->getControllerFile();
     $controllerViewPath = $this->requestOptions->getControllerViewPath();
     $actionName = $this->requestOptions->getActionName();
     $actionViewFile = $this->requestOptions->getActionViewFile();
     // check for module path and application config
     if (!file_exists($path . '/module') || !file_exists($path . '/config/application.config.php')) {
         return $this->sendError(array(array(Color::NORMAL => 'The path '), array(Color::RED => $path), array(Color::NORMAL => ' doesn\'t contain a ZF2 application.')));
     }
     // check if action name provided
     if (!$actionName) {
         return $this->sendError(array(array(Color::NORMAL => 'Please provide the action name as parameter.')));
     }
     // check if controller name provided
     if (!$controllerName) {
         return $this->sendError(array(array(Color::NORMAL => 'Please provide the controller name as parameter.')));
     }
     // check if module name provided
     if (!$moduleName) {
         return $this->sendError(array(array(Color::NORMAL => 'Please provide the module name as parameter.')));
     }
     // check if controller exists already in module
     if (!file_exists($controllerPath . $controllerFile)) {
         return $this->sendError(array(array(Color::NORMAL => 'The controller '), array(Color::RED => $controllerName), array(Color::NORMAL => ' does not exist in module '), array(Color::RED => $moduleName), array(Color::NORMAL => '. I cannot create an action here.')));
     }
     // write start message
     $this->console->write('       => Adding action ');
     $this->console->write($actionName, Color::GREEN);
     $this->console->write(' to controller ');
     $this->console->write($controllerName, Color::GREEN);
     $this->console->write(' in module ');
     $this->console->writeLine($moduleName, Color::GREEN);
     // update controller class
     try {
         $controllerFlag = $this->moduleGenerator->updateController();
     } catch (GeneratorException $e) {
         $this->console->writeLine();
         return $this->sendError(array(array(Color::NORMAL => 'The action '), array(Color::RED => $actionName), array(Color::NORMAL => ' already exists in controller '), array(Color::RED => $controllerName), array(Color::NORMAL => ' of module '), array(Color::RED => $moduleName), array(Color::NORMAL => '.')));
     }
     // write start message
     $this->console->write('       => Creating view script ');
     $this->console->write($actionViewFile, Color::GREEN);
     $this->console->write(' in ');
     $this->console->writeLine($controllerViewPath, Color::GREEN);
     // create view script
     $viewScriptFlag = $this->moduleGenerator->createViewScript();
     $this->console->writeLine();
     $this->console->write(' Done ', Color::NORMAL, Color::CYAN);
     $this->console->write(' ');
     // write message
     $this->console->write('The action ');
     $this->console->write($actionName, Color::GREEN);
     $this->console->write(' has been created in controller ');
     $this->console->write($controllerName, Color::GREEN);
     $this->console->write(' of module ');
     $this->console->writeLine($moduleName, Color::GREEN);
     // output footer
     $this->consoleFooter('action was successfully created');
 }