/** * 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; }
/** * Add configuration for the routing of a module * * @return bool|mixed */ public function addRouterConfig() { // get needed options to shorten code $flagSingleRoute = $this->requestOptions->getFlagSingleRoute(); $moduleName = $this->requestOptions->getModuleName(); $modulePath = $this->requestOptions->getModulePath(); $moduleRoute = $this->requestOptions->getModuleRoute(); // Read module configuration $moduleConfigOld = (require $modulePath . '/config/module.config.php'); $moduleConfigNew = $moduleConfigOld; // check if controller exists if (!isset($moduleConfigNew['controllers']) || count($moduleConfigNew['controllers']) == 0) { throw new GeneratorException('No controller exist in the module ' . $moduleName . '.'); } // check for router if (!isset($moduleConfigNew['router'])) { $moduleConfigNew['router'] = array(); } // reset all routes $moduleConfigNew['router']['routes'] = array(); // set controller namespace $controllerNamespace = $moduleName . '\\Controller'; // set child routes $childRoutes = array(); // check for single route if ($flagSingleRoute) { // create child routes $childRoutes = array('controller-action' => array('type' => 'segment', 'options' => array('route' => '/:controller[/:action[/:id]]', 'constraints' => array('controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[0-9_-]*')))); } else { // set controller keys $controllerKeys = array(); // merge controller keys foreach ($moduleConfigNew['controllers'] as $group) { $controllerKeys = array_merge($controllerKeys, array_keys($group)); } // merge controller keys foreach ($controllerKeys as $controllerName) { // clear leading namespace if (stripos($controllerName, $controllerNamespace) === 0) { $controllerName = str_replace($controllerNamespace . '\\', '', $controllerName); } // set routing key $routingKey = strtolower($controllerName); $routingKey = str_replace('controller', '', $routingKey); // set controller route $controllerRoute = '/' . strtolower($controllerName); // create route $childRoutes[$routingKey] = array('type' => 'segment', 'options' => array('route' => $controllerRoute . '[/:action[/:id]]', 'defaults' => array('controller' => $controllerName), 'constraints' => array('action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[0-9_-]*'))); } } // set controller keys $controllerKeys = array(); // merge controller keys foreach ($moduleConfigNew['controllers'] as $group) { $controllerKeys = array_merge($controllerKeys, array_keys($group)); } // identify default controller if (count($controllerKeys) == 1) { $defaultController = reset($controllerKeys); } else { $indexController = $controllerNamespace . '\\Index'; $moduleController = $controllerNamespace . '\\' . $moduleName; if (in_array($indexController, $controllerKeys)) { $defaultController = $indexController; } elseif (in_array($moduleController, $controllerKeys)) { $defaultController = $moduleController; } else { $defaultController = reset($controllerKeys); } } // clear leading namespace if (stripos($defaultController, $controllerNamespace) === 0) { $defaultController = str_replace($controllerNamespace . '\\', '', $defaultController); } // set routing key $routingKey = strtolower($moduleName); // create route $moduleConfigNew['router']['routes'] = array($routingKey => array('type' => 'Literal', 'options' => array('route' => $moduleRoute, 'defaults' => array('__NAMESPACE__' => $controllerNamespace, 'controller' => $defaultController, 'action' => 'index')), 'may_terminate' => true, 'child_routes' => $childRoutes)); // set config dir $configDir = realpath($modulePath . '/config'); // reset constant compilation $moduleConfigNew = $this->resetConfigDirCompilation($moduleConfigNew, $configDir); // check for module config updates if ($moduleConfigNew !== $moduleConfigOld) { return $moduleConfigNew; } else { return false; } }
/** * Create a view helper * * @return ConsoleModel */ public function viewHelperAction() { // check for help mode if ($this->requestOptions->getFlagHelp()) { return $this->viewHelperHelp(); } // output header $this->consoleHeader('Creating new view helper'); // get needed options to shorten code $path = $this->requestOptions->getPath(); $flagWithFactory = $this->requestOptions->getFlagWithFactory(); $moduleName = $this->requestOptions->getModuleName(); $modulePath = $this->requestOptions->getModulePath(); $viewHelperName = $this->requestOptions->getViewHelperName(); $viewHelperPath = $this->requestOptions->getViewHelperPath(); $viewHelperClass = $this->requestOptions->getViewHelperClass(); $viewHelperFile = $this->requestOptions->getViewHelperFile(); // 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 helper name provided if (!$viewHelperName) { return $this->sendError(array(array(Color::NORMAL => 'Please provide the view helper name as parameter.'))); } // check if module name provided if (!$moduleName) { return $this->sendError(array(array(Color::NORMAL => 'Please provide the module name as parameter.'))); } // set mode if (file_exists($viewHelperPath . $viewHelperFile)) { $mode = 'update'; } else { $mode = 'insert'; } // check if view helper exists already in module if ($mode == 'update' && !$flagWithFactory) { return $this->sendError(array(array(Color::NORMAL => 'The view helper '), array(Color::RED => $viewHelperName), array(Color::NORMAL => ' already exists in module '), array(Color::RED => $moduleName), array(Color::NORMAL => '.'))); } // create view helper if ($mode == 'insert') { // write start message $this->console->write(' => Creating view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' in module '); $this->console->writeLine($moduleName, Color::GREEN); // create view helper class $viewHelperFlag = $this->moduleGenerator->createViewHelper(); // write start message $this->console->write(' => Adding view helper configuration for '); $this->console->writeLine($moduleName, Color::GREEN); // add view helper configuration to module $moduleConfig = $this->moduleConfigurator->addViewHelperConfig(); } // check for factory flag if ($flagWithFactory) { // create view helper factory class try { $factoryFlag = $this->moduleGenerator->createViewHelperFactory(); } catch (GeneratorException $e) { return $this->sendError(array(array(Color::NORMAL => 'The factory for the view helper '), array(Color::RED => $viewHelperName), array(Color::NORMAL => ' of module '), array(Color::RED => $moduleName), array(Color::NORMAL => ' exists already.'))); } // write start message $this->console->write(' => Creating factory for view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' in module '); $this->console->writeLine($moduleName, Color::GREEN); // add view helper factory configuration to module $moduleConfig = $this->moduleConfigurator->addViewHelperFactoryConfig(); } else { $factoryFlag = false; } // check for module config updates if ($moduleConfig) { // update module configuration $this->moduleGenerator->updateConfiguration($moduleConfig, $modulePath . '/config/module.config.php'); // write start message $this->console->write(' => Updating configuration for module '); $this->console->writeLine($moduleName, Color::GREEN); } $this->console->writeLine(); $this->console->write(' Done ', Color::NORMAL, Color::CYAN); $this->console->write(' '); // write message if ($factoryFlag) { $this->console->write('The view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' has been created with a factory in module '); $this->console->writeLine($moduleName, Color::GREEN); } else { $this->console->write('The view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' has been created in module '); $this->console->writeLine($moduleName, Color::GREEN); } $this->console->writeLine(); $this->console->writeLine(' => In order to use the view helper add the following code to any view script.'); $this->console->writeLine(' <?php echo $this->' . lcfirst($viewHelperName) . '(); ?>', Color::CYAN); // output footer $this->consoleFooter('view helper was successfully created'); }