/** * Create controller factory * * @return bool */ public function createControllerFactory() { // get needed options to shorten code $moduleName = $this->requestOptions->getModuleName(); $controllerClass = $this->requestOptions->getControllerClass(); $controllerPath = $this->requestOptions->getControllerPath(); $factoryClass = $controllerClass . 'Factory'; $factoryFile = $factoryClass . '.php'; $factoryFilePath = $controllerPath . $factoryFile; // check for factory class if (file_exists($factoryFilePath)) { throw new GeneratorException('The factory for this controller exists already.'); } // create controller class with class generator $code = new ClassGenerator(); $code->setNamespaceName($moduleName . '\\Controller'); $code->addUse('Zend\\ServiceManager\\FactoryInterface'); $code->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $code->setName($factoryClass); $code->setImplementedInterfaces(array('FactoryInterface')); $code->addMethodFromGenerator($this->generateCreateServiceMethod($controllerClass, 'controllerLoader', 'controller')); // add optional doc block if ($this->flagCreateApiDocs) { $code->setDocBlock(new DocBlockGenerator('Factory for ' . $controllerClass, 'Please add a proper description for the ' . $controllerClass . ' factory', array($this->generatePackageTag($moduleName)))); } // create file with file generator $file = new FileGenerator(); $file->setClass($code); // add optional doc block if ($this->flagCreateApiDocs) { $file->setDocBlock(new DocBlockGenerator('This file was generated by FrilleZFTool.', null, array($this->generatePackageTag($moduleName), $this->generateSeeTag()))); } // write controller class if (!file_put_contents($factoryFilePath, $file->generate())) { return false; } return true; }
/** * 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'); }