ZF-11439: Zend_Tool produces incorrect controller class name when module name contains hyphen

Description

If you run:
zf create module test-mod zf create controller Index index-action-included[=1] test-mod

You'll get an error: Parse error: parse error, expecting '{' ... and you get a file with the invalid class name: Test-mod_IndexController

Expected name would be: TestMod_IndexController which would be accessed by http://project/test-mod

Comments

Reproduced issue against trunk:


[adam@zfdev zftest]$ zf create project ZF-11439
[adam@zfdev ZF-11439]$ zf create module test-mod
[adam@zfdev ZF-11439]$ zf create controller Index index-action-included=1 test-mod

Received this error message:


PHP Parse error:  syntax error, unexpected '-', expecting '{' in /tmp/zftest/ZF-11439/tests/application/modules/test-mod/controllers/IndexControllerTest.php on line 3
PHP Stack trace:
PHP   1. {main}() /tmp/zfdev/trunk/bin/zf.php:0
PHP   2. ZF::main() /tmp/zfdev/trunk/bin/zf.php:623
PHP   3. ZF->run() /tmp/zfdev/trunk/bin/zf.php:74
PHP   4. ZF->_runTool() /tmp/zfdev/trunk/bin/zf.php:117
PHP   5. Zend_Tool_Framework_Client_Abstract->dispatch() /tmp/zfdev/trunk/bin/zf.php:600
PHP   6. Zend_Tool_Framework_Client_Abstract->_handleDispatch() /tmp/zfdev/trunk/library/Zend/Tool/Framework/Client/Abstract.php:241
PHP   7. Zend_Tool_Framework_Client_Abstract->_handleDispatchExecution() /tmp/zfdev/trunk/library/Zend/Tool/Framework/Client/Abstract.php:318
PHP   8. call_user_func_array() /tmp/zfdev/trunk/library/Zend/Tool/Framework/Client/Abstract.php:324
PHP   9. Zend_Tool_Project_Provider_Controller->create() /tmp/zfdev/trunk/library/Zend/Tool/Framework/Client/Abstract.php:0
PHP  10. Zend_Tool_Project_Profile_Resource->create() /tmp/zfdev/trunk/library/Zend/Tool/Project/Provider/Controller.php:199
PHP  11. Zend_Tool_Project_Profile_Resource->__call() /tmp/zfdev/trunk/library/Zend/Tool/Project/Profile/Resource.php:0
PHP  12. call_user_func_array() /tmp/zfdev/trunk/library/Zend/Tool/Project/Profile/Resource.php:256
PHP  13. Zend_Tool_Project_Context_Zf_TestApplicationActionMethod->create() /tmp/zfdev/trunk/library/Zend/Tool/Project/Profile/Resource.php:0
PHP  14. Zend_CodeGenerator_Php_File::fromReflectedFileName() /tmp/zfdev/trunk/library/Zend/Tool/Project/Context/Zf/TestApplicationActionMethod.php:176

Issue can be fixed by applying filter Zend_Filter_Word_DashToCamelCase in ControllerFile and TestApplicationControllerFile contexts:


Index: library/Zend/Tool/Project/Context/Zf/ControllerFile.php
===================================================================
--- library/Zend/Tool/Project/Context/Zf/ControllerFile.php     (revision 24119)
+++ library/Zend/Tool/Project/Context/Zf/ControllerFile.php     (working copy)
@@ -100,7 +100,9 @@
      */
     public function getContents()
     {
-        $className = ($this->_moduleName) ? ucfirst($this->_moduleName) . '_' : '';
+        $filter = new Zend_Filter_Word_DashToCamelCase();
+
+        $className = ($this->_moduleName) ? $filter->filter(ucfirst($this->_moduleName)) . '_' : '';
         $className .= ucfirst($this->_controllerName) . 'Controller';

         $codeGenFile = new Zend_CodeGenerator_Php_File(array(
Index: library/Zend/Tool/Project/Context/Zf/TestApplicationControllerFile.php
===================================================================
--- library/Zend/Tool/Project/Context/Zf/TestApplicationControllerFile.php      (revision 24119)
+++ library/Zend/Tool/Project/Context/Zf/TestApplicationControllerFile.php      (working copy)
@@ -103,7 +103,7 @@
         /* @var $controllerDirectoryResource Zend_Tool_Project_Profile_Resource */
         $controllerDirectoryResource = $this->_resource->getParentResource();
         if ($controllerDirectoryResource->getParentResource()->getName() == 'TestApplicationModuleDirectory') {
-            $className = ucfirst($controllerDirectoryResource->getParentResource()->getForModuleName())
+            $className = $filter->filter(ucfirst($controllerDirectoryResource->getParentResource()->getForModuleName()))
                 . '_' . $className;
         }

Fixed in trunk r24120

Problem also affects the following contexts: * DbTableFile (zf create db-table -> class Test-mod_Model_DbTable_Test extends Zend_Db_Table_Abstract) * FormFile (zf create form -> class Test-mod_Form_Test extends Zend_Form) * ModelFile (zf create model -> class Test-mod_Model_Test)

All of which rely upon AbstractClassFile context to create their class names.

Fix is to apply Zend_Filter_Word_DashToCamelCase in AbstractClassFile:


Index: library/Zend/Tool/Project/Context/Zf/AbstractClassFile.php
===================================================================
--- library/Zend/Tool/Project/Context/Zf/AbstractClassFile.php  (revision 24119)
+++ library/Zend/Tool/Project/Context/Zf/AbstractClassFile.php  (working copy)
@@ -67,7 +67,8 @@
                 $prefix = $containingResource->getAttribute('classNamePrefix');
                 $fullClassName = $prefix;
             } elseif ($containingResource->getName() == 'ModuleDirectory') {
-                $prefix = ucfirst($containingResource->getAttribute('moduleName')) . '_';
+                $filter = new Zend_Filter_Word_DashToCamelCase();
+                $prefix = $filter->filter(ucfirst($containingResource->getAttribute('moduleName'))) . '_';
                 $fullClassName = $prefix;
             }
         }

Additional fix added to trunk r24121

Merged to release-1.11 in r24161