/** * 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; } }