diff --git a/README.md b/README.md index b93d519..8d79eba 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,12 @@ You can also generate the zftool.phar using the `bin/create-phar` command as rep The path to the root folder of the ZF2 application (optional) ### Controller creation: - zf.php create controller [] + zf.php create controller [] [--no-config] The name of the controller to be created The module in which the controller should be created The root path of a ZF2 application where to create the controller + --no-config Prevent that module configuration is updated ### Action creation: zf.php create action [] diff --git a/config/module.config.php b/config/module.config.php index e9abdca..d4a686c 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -101,7 +101,7 @@ ), 'zftool-create-controller' => array( 'options' => array( - 'route' => 'create controller []', + 'route' => 'create controller [] [--no-config]', 'defaults' => array( 'controller' => 'ZFTool\Controller\Create', 'action' => 'controller', diff --git a/src/ZFTool/Controller/CreateController.php b/src/ZFTool/Controller/CreateController.php index e4b1f04..2212179 100644 --- a/src/ZFTool/Controller/CreateController.php +++ b/src/ZFTool/Controller/CreateController.php @@ -98,7 +98,13 @@ public function controllerAction() $request = $this->getRequest(); $name = $request->getParam('name'); $module = $request->getParam('module'); - $path = $request->getParam('path', '.'); + $path = rtrim($request->getParam('path'), '/'); + + $noConfig = $this->params()->fromRoute('no-config', false); + + if (empty($path)) { + $path = '.'; + } if (!file_exists("$path/module") || !file_exists("$path/config/application.config.php")) { return $this->sendError( @@ -148,9 +154,80 @@ public function controllerAction() $phtml = false; $phtmlPath = $dir . "/index.phtml"; if (file_put_contents($phtmlPath, 'Action "index", controller "'.$ucName.'", module "'.$module.'".')) { + $console->writeLine(sprintf("Created view script at %s", $phtmlPath), Color::GREEN); $phtml = true; } + // check for no-config flag + if (!$noConfig) { + // Read module configuration + $moduleConfigNew = $moduleConfigOld = require "$path/module/$module/config/module.config.php"; + + // check for controllers configuration + if (!isset($moduleConfigNew['controllers'])) { + $moduleConfigNew['controllers'] = array(); + } + + // check for controllers invokables configuration + if (!isset($moduleConfigNew['controllers']['invokables'])) { + $moduleConfigNew['controllers']['invokables'] = array(); + } + + // define invokable key + $invokableKey = $module . '\Controller\\' . $name; + + // check if invokable key is already there + if (!in_array($invokableKey, $moduleConfigNew['controllers']['invokables'])) { + $moduleConfigNew['controllers']['invokables'][$invokableKey] = ucfirst($module) . '\Controller\\' . $controller; + } + + // check for view_manager + if (!isset($moduleConfigNew['view_manager'])) { + $moduleConfigNew['view_manager'] = array(); + } + + // check for template_path_stack + if (!isset($moduleConfigNew['view_manager']['template_path_stack'])) { + $moduleConfigNew['view_manager']['template_path_stack'] = array(); + } + + // set config dir + $configDir = realpath("$path/module/$module/config"); + + // check for any path + if (count($moduleConfigNew['view_manager']['template_path_stack']) > 0) { + // loop through path stack and add path again due to constant resolution problems + foreach ($moduleConfigNew['view_manager']['template_path_stack'] as $pathKey => $pathKey) { + if ($configDir . '/../view' == $pathKey) { + $moduleConfigNew['view_manager']['template_path_stack'][$pathKey] = '__DIR__ . \'/../view\''; + } + } + } else { + $moduleConfigNew['view_manager']['template_path_stack'][] = '__DIR__ . \'/../view\''; + } + + // check for module config updates + if ($moduleConfigNew !== $moduleConfigOld) { + copy("$path/module/$module/config/module.config.php", "$path/module/$module/config/module.config.old"); + + $content = <<writeLine("Module configuration was updated for module $module.", Color::YELLOW); + } + } + if (file_put_contents($ctrlPath, $file->generate()) && $phtml == true) { $console->writeLine("The controller $name has been created in module $module.", Color::GREEN); } else { diff --git a/src/ZFTool/Model/Skeleton.php b/src/ZFTool/Model/Skeleton.php index e35ce10..3a43885 100644 --- a/src/ZFTool/Model/Skeleton.php +++ b/src/ZFTool/Model/Skeleton.php @@ -87,6 +87,7 @@ public static function exportConfig($config, $indent = 0) { if (empty(static::$valueGenerator)) { static::$valueGenerator = new ValueGenerator(); + static::$valueGenerator->initEnvironmentConstants(); } static::$valueGenerator->setValue($config); static::$valueGenerator->setArrayDepth($indent); diff --git a/src/ZFTool/Module.php b/src/ZFTool/Module.php index 4f4beca..8764ae2 100644 --- a/src/ZFTool/Module.php +++ b/src/ZFTool/Module.php @@ -82,10 +82,11 @@ public function getConsoleUsage(ConsoleAdapterInterface $console) array('', 'The root path of a ZF2 application where to create the module'), 'Controller creation:', - 'create controller []' => 'create a controller in module', + 'create controller [] [--no-config]' => 'create a controller in module', array('', 'The name of the controller to be created'), array('', 'The module in which the controller should be created'), array('', 'The root path of a ZF2 application where to create the controller'), + array('--no-config', 'Prevent that module configuration is updated'), 'Action creation:', 'create action []' => 'create an action in a controller',