diff --git a/README.md b/README.md index b93d519..79b0588 100644 --- a/README.md +++ b/README.md @@ -56,25 +56,28 @@ You can also generate the zftool.phar using the `bin/create-phar` command as rep ### Module creation - zf.php create module [] + zf.php create module [] [--ignore-conventions|-i] - The name of the module to be created - The path to the root folder of the ZF2 application (optional) + The name of the module to be created + The path to the root folder of the ZF2 application (optional) + --ignore-conventions | -i Ignore coding conventions ### Controller creation: - zf.php create controller [] + zf.php create controller [] [--ignore-conventions|-i] - 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 + 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 + --ignore-conventions | -i Ignore coding conventions ### Action creation: - zf.php create action [] + zf.php create action [] [--ignore-conventions|-i] - The name of the action to be created - The name of the controller in which the action should be created - The module containing the controller - The root path of a ZF2 application where to create the action + The name of the action to be created + The name of the controller in which the action should be created + The module containing the controller + The root path of a ZF2 application where to create the action + --ignore-conventions | -i Ignore coding conventions ### Application configuration diff --git a/config/module.config.php b/config/module.config.php index e9abdca..4f8a609 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -92,7 +92,7 @@ ), 'zftool-create-module' => array( 'options' => array( - 'route' => 'create module []', + 'route' => 'create module [] [--ignore-conventions|-i]', 'defaults' => array( 'controller' => 'ZFTool\Controller\Create', 'action' => 'module', @@ -101,7 +101,7 @@ ), 'zftool-create-controller' => array( 'options' => array( - 'route' => 'create controller []', + 'route' => 'create controller [] [--ignore-conventions|-i]', 'defaults' => array( 'controller' => 'ZFTool\Controller\Create', 'action' => 'controller', @@ -110,7 +110,7 @@ ), 'zftool-create-action' => array( 'options' => array( - 'route' => 'create action ', + 'route' => 'create action [] [--ignore-conventions|-i]', 'defaults' => array( 'controller' => 'ZFTool\Controller\Create', 'action' => 'method', diff --git a/src/ZFTool/Controller/CreateController.php b/src/ZFTool/Controller/CreateController.php index e4b1f04..7eb8e0a 100644 --- a/src/ZFTool/Controller/CreateController.php +++ b/src/ZFTool/Controller/CreateController.php @@ -2,7 +2,10 @@ namespace ZFTool\Controller; +use Zend\Console\Adapter\AdapterInterface; +use Zend\Filter\StaticFilter; use Zend\Mvc\Controller\AbstractActionController; +use Zend\Stdlib\Parameters; use Zend\View\Model\ViewModel; use Zend\View\Model\ConsoleModel; use ZFTool\Model\Skeleton; @@ -10,56 +13,244 @@ use Zend\Console\ColorInterface as Color; use Zend\Code\Generator; use Zend\Code\Reflection; -use Zend\Filter\Word\CamelCaseToDash as CamelCaseToDashFilter; +/** + * Class CreateController + * + * @package ZFTool\Controller + */ class CreateController extends AbstractActionController { + /** + * @var AdapterInterface + */ + protected $console; + + /** + * @var Parameters + */ + protected $requestParams; + + /** + * Setup request params + */ + protected function setupParams() + { + // get console adapter + $this->console = $this->getServiceLocator()->get('console'); + + // get request + $request = $this->getRequest(); + + // set path + $path = rtrim($request->getParam('path'), '/'); + + if (empty($path)) { + $path = '.'; + } + + // initialize requestParams + $this->requestParams = new Parameters( + array( + 'tmpDir' => sys_get_temp_dir(), + 'path' => $path, + ) + ); + + // ignore conventions + $ignoreConventions = $request->getParam('ignore-conventions', false) + || $request->getParam('i', false); + + // check for moduleName param + if ($request->getParam('moduleName')) { + $moduleName = $request->getParam('moduleName'); + + if (!$ignoreConventions) { + $moduleName = StaticFilter::execute( + $moduleName, 'Word\UnderscoreToCamelCase' + ); + $moduleName = StaticFilter::execute( + $moduleName, 'Word\DashToCamelCase' + ); + } else { + $moduleName = StaticFilter::execute( + $moduleName, 'Word\DashToUnderscore' + ); + } + + // set path for new module + $modulePath = $path . '/module/' . $moduleName; + + // setup module view dir + $moduleViewDir = StaticFilter::execute( + $moduleName, 'Word\CamelCaseToDash' + ); + $moduleViewDir = StaticFilter::execute($moduleViewDir, 'StringToLower'); + + // set params + $this->requestParams->set('moduleName', $moduleName); + $this->requestParams->set('modulePath', $modulePath); + $this->requestParams->set('moduleViewDir', $moduleViewDir); + } + + // check for controllerName param + if ($request->getParam('controllerName')) { + $controllerName = $request->getParam('controllerName'); + + if (!$ignoreConventions) { + $controllerName = StaticFilter::execute( + $controllerName, 'Word\UnderscoreToCamelCase' + ); + $controllerName = StaticFilter::execute( + $controllerName, 'Word\DashToCamelCase' + ); + } else { + $controllerName = StaticFilter::execute( + $controllerName, 'Word\DashToUnderscore' + ); + } + + // set controller path + $controllerPath = $modulePath . '/src/' + . $moduleName . '/Controller/'; + // set controller class + $controllerClass = $controllerName . 'Controller'; + + // set controller file + $controllerFile = $controllerClass . '.php'; + + // setup controller view dir + $controllerViewDir = StaticFilter::execute( + $controllerName, 'Word\CamelCaseToDash' + ); + $controllerViewDir = StaticFilter::execute( + $controllerViewDir, 'StringToLower' + ); + + // set controller view path + $controllerViewPath = $modulePath . '/view/' . $moduleViewDir . '/' + . $controllerViewDir; + + // set params + $this->requestParams->set('controllerName', $controllerName); + $this->requestParams->set('controllerPath', $controllerPath); + $this->requestParams->set('controllerClass', $controllerClass); + $this->requestParams->set('controllerFile', $controllerFile); + $this->requestParams->set('controllerViewPath', $controllerViewPath); + } + + // check for actionName param + if ($request->getParam('actionName')) { + $actionName = $request->getParam('actionName'); + + if (!$ignoreConventions) { + $actionName = StaticFilter::execute( + $actionName, 'Word\UnderScoreToDash' + ); + $actionName = StaticFilter::execute( + $actionName, 'Word\DashToCamelCase' + ); + } else { + $actionName = StaticFilter::execute( + $actionName, 'Word\DashToUnderscore' + ); + } + + // set action method + $actionMethod = lcfirst($actionName) . 'Action'; + + // setup action view file + $actionViewFile = StaticFilter::execute( + $actionName . '.phtml', 'Word\CamelCaseToDash' + ); + $actionViewFile = StaticFilter::execute( + $actionViewFile, 'StringToLower' + ); + + // set action view path + $actionViewPath = $controllerViewPath . '/' . $actionViewFile; + + // set params + $this->requestParams->set('actionName', $actionName); + $this->requestParams->set('actionMethod', $actionMethod); + $this->requestParams->set('actionViewFile', $actionViewFile); + $this->requestParams->set('actionViewPath', $actionViewPath); + } + } + + /** + * Create a project + * + * @return ConsoleModel + */ public function projectAction() { + // check for zip extension if (!extension_loaded('zip')) { - return $this->sendError('You need to install the ZIP extension of PHP'); + return $this->sendError( + 'You need to install the ZIP extension of PHP' + ); } + + // check for openssl extension if (!extension_loaded('openssl')) { - return $this->sendError('You need to install the OpenSSL extension of PHP'); + return $this->sendError( + 'You need to install the OpenSSL extension of PHP' + ); } - $console = $this->getServiceLocator()->get('console'); - $tmpDir = sys_get_temp_dir(); - $request = $this->getRequest(); - $path = rtrim($request->getParam('path'), '/'); + // setup params + $this->setupParams(); + + // get needed params + $path = $this->requestParams->get('path'); + $tmpDir = $this->requestParams->get('tmpDir'); + + // check if path exists if (file_exists($path)) { - return $this->sendError ( - "The directory $path already exists. You cannot create a ZF2 project here." + return $this->sendError( + 'The directory ' . $path . ' already exists. ' + . 'You cannot create a ZF2 project here.' ); } + // check last commit $commit = Skeleton::getLastCommit(); if (false === $commit) { // error on github connection $tmpFile = Skeleton::getLastZip($tmpDir); if (empty($tmpFile)) { - return $this->sendError('I cannot access the API of github.'); + return $this->sendError( + 'I cannot access the API of GitHub.' + ); } - $console->writeLine( - "Warning: I cannot connect to github, I will use the last download of ZF2 Skeleton.", - Color::GRAY + $this->console->writeLine( + 'Warning: I cannot connect to GitHub, I will use the last ' + . 'download of ZF2 Skeleton.', + Color::GRAY ); } else { $tmpFile = Skeleton::getTmpFileName($tmpDir, $commit); } + // check for Skeleton App if (!file_exists($tmpFile)) { if (!Skeleton::getSkeletonApp($tmpFile)) { - return $this->sendError('I cannot access the ZF2 skeleton application in github.'); + return $this->sendError( + 'I cannot access the ZF2 skeleton application in GitHub.' + ); } } + // set Zip Archive $zip = new \ZipArchive; if ($zip->open($tmpFile)) { $stateIndex0 = $zip->statIndex(0); $tmpSkeleton = $tmpDir . '/' . rtrim($stateIndex0['name'], "/"); if (!$zip->extractTo($tmpDir)) { - return $this->sendError("Error during the unzip of $tmpFile."); + return $this->sendError( + 'Error during the unzip of ' . $tmpFile + ); } $result = Utility::copyFiles($tmpSkeleton, $path); if (file_exists($tmpSkeleton)) { @@ -67,219 +258,405 @@ public function projectAction() } $zip->close(); if (false === $result) { - return $this->sendError("Error during the copy of the files in $path."); + return $this->sendError( + 'Error during the copy of the files in ' . $path + ); } } - if (file_exists("$path/composer.phar")) { - exec("php $path/composer.phar self-update"); + + // check for composer + if (file_exists($path . '/composer.phar')) { + exec('php ' . $path . '/composer.phar self-update'); } else { - if (!file_exists("$tmpDir/composer.phar")) { - if (!file_exists("$tmpDir/composer_installer.php")) { + if (!file_exists($tmpDir . '/composer.phar')) { + if (!file_exists($tmpDir . '/composer_installer.php')) { file_put_contents( - "$tmpDir/composer_installer.php", - '?>' . file_get_contents('https://getcomposer.org/installer') + $tmpDir . '/composer_installer.php', + '?>' . file_get_contents( + 'https://getcomposer.org/installer' + ) ); } - exec("php $tmpDir/composer_installer.php --install-dir $tmpDir"); + exec( + 'php ' . $tmpDir . '/composer_installer.php --install-dir ' + . $tmpDir + ); } - copy("$tmpDir/composer.phar", "$path/composer.phar"); + copy($tmpDir . '/composer.phar', $path . '/composer.phar'); } - chmod("$path/composer.phar", 0755); - $console->writeLine("ZF2 skeleton application installed in $path.", Color::GREEN); - $console->writeLine("In order to execute the skeleton application you need to install the ZF2 library."); - $console->writeLine("Execute: \"composer.phar install\" in $path"); - $console->writeLine("For more info in $path/README.md"); + chmod($path . '/composer.phar', 0755); + $this->console->writeLine( + 'ZF2 skeleton application installed in ' . $path, Color::GREEN + ); + $this->console->writeLine( + 'In order to execute the skeleton application you need to ' + . 'install the ZF2 library.' + ); + $this->console->writeLine( + 'Execute: "composer.phar install" in ' . $path + ); + $this->console->writeLine( + 'For more info in ' . $path . '/README.md' + ); } + /** + * Create a controller + * + * @return ConsoleModel + */ public function controllerAction() { - $console = $this->getServiceLocator()->get('console'); - $tmpDir = sys_get_temp_dir(); - $request = $this->getRequest(); - $name = $request->getParam('name'); - $module = $request->getParam('module'); - $path = $request->getParam('path', '.'); - - if (!file_exists("$path/module") || !file_exists("$path/config/application.config.php")) { + // setup params + $this->setupParams(); + + // get needed params + $path = $this->requestParams->get('path'); + $moduleName = $this->requestParams->get('moduleName'); + $modulePath = $this->requestParams->get('modulePath'); + $controllerName = $this->requestParams->get('controllerName'); + $controllerPath = $this->requestParams->get('controllerPath'); + $controllerClass = $this->requestParams->get('controllerClass'); + $controllerFile = $this->requestParams->get('controllerFile'); + $controllerViewPath = $this->requestParams->get('controllerViewPath'); + + // check for module path and application config + if (!file_exists($path . '/module') + || !file_exists($path . '/config/application.config.php') + ) { return $this->sendError( - "The path $path doesn't contain a ZF2 application. I cannot create a module here." + 'The path ' . $path . ' doesn\'t contain a ZF2 application. ' + . 'I cannot create a controller here.' ); } - if (file_exists("$path/module/$module/src/$module/Controller/$name")) { + + // check if controller exists already in module + if (file_exists($controllerPath . $controllerFile)) { return $this->sendError( - "The controller $name already exists in module $module." + 'The controller "' . $controllerClass + . '" already exists in module "' . $moduleName . '".' ); } - $ucName = ucfirst($name); - $ctrlPath = $path . '/module/' . $module . '/src/' . $module . '/Controller/' . $ucName.'Controller.php'; - $controller = $ucName . 'Controller'; + // write start message + $this->console->writeLine( + 'Creating controller "' . $controllerName + . '" in module "' . $moduleName . '".', + Color::YELLOW + ); + // create controller class with class generator $code = new Generator\ClassGenerator(); - $code->setNamespaceName(ucfirst($module) . '\Controller') - ->addUse('Zend\Mvc\Controller\AbstractActionController') - ->addUse('Zend\View\Model\ViewModel'); - - $code->setName($controller) - ->addMethods(array( - new Generator\MethodGenerator( - 'indexAction', - array(), - Generator\MethodGenerator::FLAG_PUBLIC, - 'return new ViewModel();' - ), - )) - ->setExtendedClass('AbstractActionController'); + $code->setDocBlock( + new Generator\DocBlockGenerator( + 'Class ' . $controllerClass, + 'Please add a proper description for the ' . $controllerClass, + array( + new Generator\DocBlock\Tag( + array( + 'name' => 'package', + 'description' => $moduleName, + ) + ), + ) + ) + ); + $code->setNamespaceName($moduleName . '\Controller'); + $code->addUse('Zend\Mvc\Controller\AbstractActionController'); + $code->addUse('Zend\View\Model\ViewModel'); + $code->setName($controllerClass); + $code->setExtendedClass('AbstractActionController'); + $code->addMethod( + 'indexAction', + array(), + Generator\MethodGenerator::FLAG_PUBLIC, + 'return new ViewModel();', + new Generator\DocBlockGenerator( + 'Method indexAction', + 'Please add a proper description for this action', + array( + new Generator\DocBlock\Tag( + array( + 'name' => 'return', + 'description' => 'ViewModel', + ) + ), + ) + ) + ); + // create file with file generator $file = new Generator\FileGenerator( array( - 'classes' => array($code), + 'classes' => array($code), + ) + ); + $file->setDocBlock( + new Generator\DocBlockGenerator( + null, + 'This file was generated by ZFTool.', + array( + new Generator\DocBlock\Tag( + array( + 'name' => 'package', + 'description' => $moduleName, + ) + ), + ) ) ); - $filter = new CamelCaseToDashFilter(); - $viewfolder = strtolower($filter->filter($module)); + // write controller class + $controllerFlag = file_put_contents( + $controllerPath . $controllerFile, + $file->generate() + ); - $dir = $path . "/module/$module/view/$viewfolder/" . strtolower($filter->filter($name)); - if (!file_exists($dir)) { - mkdir($dir, 0777, true); + // create dir if not exists + if (!file_exists($controllerViewPath)) { + mkdir($controllerViewPath, 0777, true); } - $phtml = false; - $phtmlPath = $dir . "/index.phtml"; - if (file_put_contents($phtmlPath, 'Action "index", controller "'.$ucName.'", module "'.$module.'".')) { - $phtml = true; - } + // initialite view script + $actionViewPath = $controllerViewPath . '/index.phtml'; + $actionViewContent = 'Action "index", controller "' + . $controllerName . '", module "'. $moduleName .'".'; - if (file_put_contents($ctrlPath, $file->generate()) && $phtml == true) { - $console->writeLine("The controller $name has been created in module $module.", Color::GREEN); + // write view script + $viewScriptFlag = file_put_contents( + $actionViewPath, $actionViewContent + ); + + // write message + if ($controllerFlag && $viewScriptFlag) { + $this->console->writeLine( + 'The controller "' . $controllerName + . '" has been created in module "' . $moduleName . '".', + Color::GREEN + ); } else { - $console->writeLine("There was an error during controller creation.", Color::RED); + $this->console->writeLine( + 'There was an error during controller creation.', + Color::RED + ); } } + /** + * Create an action method + * + * @return ConsoleModel + */ public function methodAction() { - $console = $this->getServiceLocator()->get('console'); - $request = $this->getRequest(); - $action = $request->getParam('name'); - $controller = $request->getParam('controllerName'); - $module = $request->getParam('module'); - $path = $request->getParam('path', '.'); - $ucController = ucfirst($controller); - $controllerPath = sprintf('%s/module/%s/src/%s/Controller/%sController.php', $path, $module, $module, $ucController); - $class = sprintf('%s\\Controller\\%sController', $module, $ucController); - - - $console->writeLine("Creating action '$action' in controller '$module\\Controller\\$controller'.", Color::YELLOW); - - if (!file_exists("$path/module") || !file_exists("$path/config/application.config.php")) { + // setup params + $this->setupParams(); + + // get needed params + $path = $this->requestParams->get('path'); + $moduleName = $this->requestParams->get('moduleName'); + $controllerName = $this->requestParams->get('controllerName'); + $controllerPath = $this->requestParams->get('controllerPath'); + $controllerClass = $this->requestParams->get('controllerClass'); + $controllerFile = $this->requestParams->get('controllerFile'); + $controllerViewPath = $this->requestParams->get('controllerViewPath'); + $actionName = $this->requestParams->get('actionName'); + $actionMethod = $this->requestParams->get('actionMethod'); + $actionViewPath = $this->requestParams->get('actionViewPath'); + + // check for module path and application config + if (!file_exists($path . '/module') + || !file_exists($path . '/config/application.config.php') + ) { return $this->sendError( - "The path $path doesn't contain a ZF2 application. I cannot create a controller action." + 'The path ' . $path . ' doesn\'t contain a ZF2 application. ' + . 'I cannot create a controller action here.' ); } - if (!file_exists($controllerPath)) { + + // check if controller exists already in module + if (!file_exists($controllerPath . $controllerFile)) { return $this->sendError( - "The controller $controller does not exists in module $module. I cannot create a controller action." + 'The controller "' . $controllerClass + . '" does not exists in module "' . $moduleName . '". ' + . 'I cannot create a controller action here.' ); } - $fileReflection = new Reflection\FileReflection($controllerPath, true); - $classReflection = $fileReflection->getClass($class); + // write start message + $this->console->writeLine( + 'Creating action "' . $actionName + . '" in controller "' . $controllerName + . '" in module "' . $moduleName . '".', + Color::YELLOW + ); + + + // get file and class reflection + $fileReflection = new Reflection\FileReflection( + $controllerPath . $controllerFile, + true + ); + $classReflection = $fileReflection->getClass( + $moduleName . '\Controller\\' . $controllerClass + ); - $classGenerator = Generator\ClassGenerator::fromReflection($classReflection); - $classGenerator->addUse('Zend\Mvc\Controller\AbstractActionController') - ->addUse('Zend\View\Model\ViewModel') - ->setExtendedClass('AbstractActionController'); + // setup class generator with reflected class + $code = Generator\ClassGenerator::fromReflection($classReflection); - if ($classGenerator->hasMethod($action . 'Action')) { + // fix namespace usage + $code->addUse('Zend\Mvc\Controller\AbstractActionController') + ->addUse('Zend\View\Model\ViewModel') + ->setExtendedClass('AbstractActionController'); + + // check for action method + if ($code->hasMethod($actionMethod)) { return $this->sendError( - "The action $action already exists in controller $controller of module $module." + 'The action "' . $actionName + . '" already exists in controller "' . $controllerName + . '" of module "' . $moduleName . '".' ); } - $classGenerator->addMethods(array( - new Generator\MethodGenerator( - $action . 'Action', - array(), - Generator\MethodGenerator::FLAG_PUBLIC, - 'return new ViewModel();' - ), - )); + // add new action method + $code->addMethod( + $actionMethod, + array(), + Generator\MethodGenerator::FLAG_PUBLIC, + 'return new ViewModel();', + new Generator\DocBlockGenerator( + 'Method ' . $actionMethod, + 'Please add a proper description for this action', + array( + new Generator\DocBlock\Tag( + array( + 'name' => 'return', + 'description' => 'ViewModel', + ) + ), + ) + ) + ); - $fileGenerator = new Generator\FileGenerator( + // create file with file generator + $file = new Generator\FileGenerator( array( - 'classes' => array($classGenerator), + 'classes' => array($code), + ) + ); + $file->setDocBlock( + new Generator\DocBlockGenerator( + null, + 'This file was generated by ZFTool.', + array( + new Generator\DocBlock\Tag( + array( + 'name' => 'package', + 'description' => $moduleName, + ) + ), + ) ) ); - $filter = new CamelCaseToDashFilter(); - $phtmlPath = sprintf( - '%s/module/%s/view/%s/%s/%s.phtml', - $path, - $module, - strtolower($filter->filter($module)), - strtolower($filter->filter($controller)), - strtolower($filter->filter($action)) + // write controller class + $controllerFlag = file_put_contents( + $controllerPath . $controllerFile, + $file->generate() ); - if (!file_exists($phtmlPath)) { - $contents = sprintf("Module: %s\nController: %s\nAction: %s", $module, $controller, $action); - if (file_put_contents($phtmlPath, $contents)) { - $console->writeLine(sprintf("Created view script at %s", $phtmlPath), Color::GREEN); - } else { - $console->writeLine(sprintf("An error occurred when attempting to create view script at location %s", $phtmlPath), Color::RED); - } - } - if (file_put_contents($controllerPath, $fileGenerator->generate())) { - $console->writeLine(sprintf('The action %s has been created in controller %s\\Controller\\%s.', $action, $module, $controller), Color::GREEN); + // initialite view script + $actionViewContent = 'Action "' . $actionName . '", controller "' + . $controllerName . '", module "'. $moduleName .'".'; + + // write view script + $viewScriptFlag = file_put_contents( + $actionViewPath, $actionViewContent + ); + + // write message + if ($controllerFlag && $viewScriptFlag) { + $this->console->writeLine( + 'The action "' . $actionName + . '" has been created in controller "' . $controllerName + . '" of module "' . $moduleName . '".', + Color::GREEN + ); } else { - $console->writeLine("There was an error during action creation.", Color::RED); + $this->console->writeLine( + 'There was an error during action creation.', + Color::RED + ); } } + /** + * Create a module + * + * @return ConsoleModel + */ public function moduleAction() { - $console = $this->getServiceLocator()->get('console'); - $tmpDir = sys_get_temp_dir(); - $request = $this->getRequest(); - $name = $request->getParam('name'); - $path = rtrim($request->getParam('path'), '/'); - - if (empty($path)) { - $path = '.'; - } - - if (!file_exists("$path/module") || !file_exists("$path/config/application.config.php")) { + // setup params + $this->setupParams(); + + // get needed params + $path = $this->requestParams->get('path'); + $moduleName = $this->requestParams->get('moduleName'); + $modulePath = $this->requestParams->get('modulePath'); + $moduleViewDir = $this->requestParams->get('moduleViewDir'); + + // check for module path and application config + if (!file_exists($path . '/module') + || !file_exists($path . '/config/application.config.php') + ) { return $this->sendError( - "The path $path doesn't contain a ZF2 application. I cannot create a module here." + 'The path ' . $path . ' doesn\'t contain a ZF2 application. ' + . 'I cannot create a module here.' ); } - if (file_exists("$path/module/$name")) { + + // check if module exists + if (file_exists($modulePath)) { return $this->sendError( - "The module $name already exists." + 'The module ' . $moduleName . ' already exists.' ); } - $filter = new CamelCaseToDashFilter(); - $viewfolder = strtolower($filter->filter($name)); + // write start message + $this->console->writeLine( + 'Creating module "' . $moduleName . '".', + Color::YELLOW + ); - $name = ucfirst($name); - mkdir("$path/module/$name/config", 0777, true); - mkdir("$path/module/$name/src/$name/Controller", 0777, true); - mkdir("$path/module/$name/view/$viewfolder", 0777, true); + // create dirs + mkdir($modulePath . '/config', 0777, true); + mkdir($modulePath . '/src/' . $moduleName . '/Controller', 0777, true); + mkdir($modulePath . '/view/' . $moduleViewDir, 0777, true); // Create the Module.php - file_put_contents("$path/module/$name/Module.php", Skeleton::getModule($name)); + file_put_contents( + $modulePath . '/Module.php', + Skeleton::getModule($moduleName) + ); // Create the module.config.php - file_put_contents("$path/module/$name/config/module.config.php", Skeleton::getModuleConfig($name)); + file_put_contents( + $modulePath . '/config/module.config.php', + Skeleton::getModuleConfig($moduleName) + ); + + // set file name + $applicationConfigFile = $path . '/config/application.config.php'; // Add the module in application.config.php - $application = require "$path/config/application.config.php"; - if (!in_array($name, $application['modules'])) { - $application['modules'][] = $name; - copy ("$path/config/application.config.php", "$path/config/application.config.old"); + $application = require $applicationConfigFile; + if (!in_array($moduleName, $application['modules'])) { + $application['modules'][] = $moduleName; + copy( + $applicationConfigFile, + $path . '/config/application.config.old' + ); $content = <<writeLine("The module $name has been created", Color::GREEN); + $this->console->writeLine( + 'The module "' . $moduleName . '" has been created', + Color::GREEN + ); } else { - $console->writeLine("The module $name has been created in $path", Color::GREEN); + $this->console->writeLine( + 'The module "' . $moduleName . '" has been created in ' . $path, + Color::GREEN + ); } } @@ -309,9 +694,11 @@ public function moduleAction() */ protected function sendError($msg) { + $this->console->writeLine($msg, Color::RED); + $m = new ConsoleModel(); $m->setErrorLevel(2); - $m->setResult($msg . PHP_EOL); + $m->setResult('---> aborted' . PHP_EOL); return $m; } } diff --git a/src/ZFTool/Module.php b/src/ZFTool/Module.php index 4f4beca..36cd4ea 100644 --- a/src/ZFTool/Module.php +++ b/src/ZFTool/Module.php @@ -77,22 +77,25 @@ public function getConsoleUsage(ConsoleAdapterInterface $console) array('', 'The path of the project to be created'), 'Module creation:', - 'create module []' => 'create a module', - array('', 'The name of the module to be created'), + 'create module [] [--ignore-conventions|-i]' => 'create a module', + array('', 'The name of the module to be created'), array('', 'The root path of a ZF2 application where to create the module'), + array('--ignore-conventions | -i', 'Ignore coding conventions'), 'Controller creation:', - 'create controller []' => 'create a controller in module', - array('', 'The name of the controller to be created'), - array('', 'The module in which the controller should be created'), + 'create controller [] [--ignore-conventions|-i]' => '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('--ignore-conventions | -i', 'Ignore coding conventions'), 'Action creation:', - 'create action []' => 'create an action in a controller', - array('', 'The name of the action to be created'), + 'create action [] [--ignore-conventions|-i]' => 'create an action in a controller', + array('', 'The name of the action to be created'), array('', 'The name of the controller in which the action should be created'), - array('', 'The module containing the controller'), + array('', 'The module containing the controller'), array('', 'The root path of a ZF2 application where to create the action'), + array('--ignore-conventions | -i', 'Ignore coding conventions'), 'Classmap generator:', 'classmap generate [--append|-a] [--overwrite|-w]' => '',