/** * Show actions for a controller in a module */ public function actionsAction() { // check for help mode if ($this->requestOptions->getFlagHelp()) { return $this->actionsHelp(); } // output header $this->consoleHeader('Fetching requested information'); // get needed options to shorten code $path = $this->requestOptions->getPath(); $moduleName = $this->requestOptions->getModuleName(); $modulePath = $this->requestOptions->getModulePath(); $controllerKey = $this->requestOptions->getControllerKey(); $controllerName = $this->requestOptions->getControllerName(); $controllerPath = $this->requestOptions->getControllerPath(); $controllerFile = $this->requestOptions->getControllerFile(); // 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 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 module exists if (!file_exists($modulePath)) { return $this->sendError(array(array(Color::NORMAL => 'The module '), array(Color::RED => $moduleName), array(Color::NORMAL => ' does not exist.'))); } // 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 => '.'))); } // get actions $actions = $this->getActionsForController($controllerPath . $controllerFile, $controllerKey); // check actions if (empty($actions)) { return $this->sendError(array(array(Color::NORMAL => 'No actions available for controller '), array(Color::RED => $controllerName), array(Color::NORMAL => ' in module '), array(Color::RED => $moduleName), array(Color::NORMAL => '.'))); } // start output $this->console->write(' Done ', Color::NORMAL, Color::CYAN); $this->console->write(' '); $this->console->write('Actions available in controller '); $this->console->write($controllerName, Color::GREEN); $this->console->write(' in module '); $this->console->write($moduleName, Color::GREEN); $this->console->writeLine(PHP_EOL); // output actions foreach ($actions as $actionMethod) { $this->console->writeLine(' => ' . $actionMethod . '()', Color::GREEN); } // output footer $this->consoleFooter('requested info was successfully displayed'); }
/** * Update controller class with new action * * @return bool * @throws \Zend\Code\Generator\Exception */ public function updateController() { // get needed options to shorten code $moduleName = $this->requestOptions->getModuleName(); $controllerKey = $this->requestOptions->getControllerKey(); $controllerPath = $this->requestOptions->getControllerPath(); $controllerFile = $this->requestOptions->getControllerFile(); $actionMethod = $this->requestOptions->getActionMethod(); $controllerFilePath = $controllerPath . $controllerFile; // get file and class reflection $fileReflection = new FileReflection($controllerFilePath, true); $classReflection = $fileReflection->getClass($controllerKey . 'Controller'); // setup class generator with reflected class $code = ClassGenerator::fromReflection($classReflection); // check for action method if ($code->hasMethod($actionMethod)) { throw new GeneratorException('New action already exists within controller'); } // fix namespace usage $code->addUse('Zend\\Mvc\\Controller\\AbstractActionController'); $code->addUse('Zend\\View\\Model\\ViewModel'); $code->setExtendedClass('AbstractActionController'); $code->addMethodFromGenerator($this->generateActionMethod($actionMethod)); // 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($controllerFilePath, $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'); }