1010use Zend \Console \ColorInterface as Color ;
1111use Zend \Code \Generator ;
1212use Zend \Code \Reflection ;
13+ use Zend \Filter \Word \CamelCaseToDash as CamelCaseToDashFilter ;
1314
1415class CreateController extends AbstractActionController
1516{
@@ -136,7 +137,10 @@ public function controllerAction()
136137 )
137138 );
138139
139- $ dir = $ path . "/module/ $ module/view/ " . strtolower ($ module ) . "/ " . strtolower ($ name );
140+ $ filter = new CamelCaseToDashFilter ();
141+ $ viewfolder = strtolower ($ filter ->filter ($ module ));
142+
143+ $ dir = $ path . "/module/ $ module/view/ $ viewfolder/ " . strtolower ($ filter ->filter ($ name ));
140144 if (!file_exists ($ dir )) {
141145 mkdir ($ dir , 0777 , true );
142146 }
@@ -156,46 +160,42 @@ public function controllerAction()
156160
157161 public function methodAction ()
158162 {
159- $ console ->writeLine ("Creating action ' $ action' in controller ' $ module \\$ controller'. " , Color::YELLOW );
163+ $ console = $ this ->getServiceLocator ()->get ('console ' );
164+ $ request = $ this ->getRequest ();
165+ $ action = $ request ->getParam ('name ' );
166+ $ controller = $ request ->getParam ('controllerName ' );
167+ $ module = $ request ->getParam ('module ' );
168+ $ path = $ request ->getParam ('path ' , '. ' );
169+ $ ucController = ucfirst ($ controller );
170+ $ controllerPath = sprintf ('%s/module/%s/src/%s/Controller/%sController.php ' , $ path , $ module , $ module , $ ucController );
171+ $ class = sprintf ('%s \\Controller \\%sController ' , $ module , $ ucController );
160172
161- $ console = $ this ->getServiceLocator ()->get ('console ' );
162- $ tmpDir = sys_get_temp_dir ();
163173
164- $ request = $ this ->getRequest ();
165- $ name = $ request ->getParam ('name ' );
166- $ ctrl = $ request ->getParam ('controller ' );
167- $ module = $ request ->getParam ('module ' );
168- $ path = $ request ->getParam ('path ' , '. ' );
174+ $ console ->writeLine ("Creating action ' $ action' in controller ' $ module \\Controller \\$ controller'. " , Color::YELLOW );
169175
170176 if (!file_exists ("$ path/module " ) || !file_exists ("$ path/config/application.config.php " )) {
171177 return $ this ->sendError (
172- "The path $ path doesn't contain a ZF2 application. I cannot create a module here . "
178+ "The path $ path doesn't contain a ZF2 application. I cannot create a controller action . "
173179 );
174180 }
175- if (!file_exists (" $ path /module/ $ module /src/ $ module /Controller/ $ ctrl " )) {
181+ if (!file_exists ($ controllerPath )) {
176182 return $ this ->sendError (
177- "The controller $ name does not exists in module $ module. "
183+ "The controller $ controller does not exists in module $ module. I cannot create a controller action . "
178184 );
179185 }
180186
181- $ ucName = ucfirst ($ ctrl );
182- $ ctrlPath = $ path . '/module/ ' . $ module . '/src/ ' . $ module . '/Controller/ ' . $ ucName .'Controller.php ' ;
183- $ controller = $ ucName . 'Controller ' ;
184- $ action = strtolower ($ name );
185-
186- $ code = new Generator \FileGenerate::fromRelection (
187- new Reflection \FileReflection ($ ctrlPath )
188- );
187+ $ fileReflection = new Reflection \FileReflection ($ controllerPath , true );
188+ $ classReflection = $ fileReflection ->getClass ($ class );
189189
190- $ new_code = Generator \ClassGenerator::fromReflection ($ class );
190+ $ classGenerator = Generator \ClassGenerator::fromReflection ($ classReflection );
191191
192- if ($ new_code ->hasMethod ($ action . 'Action ' )) {
192+ if ($ classGenerator ->hasMethod ($ action . 'Action ' )) {
193193 return $ this ->sendError (
194- "The action $ action already exists in controller $ ctrl of module $ module. "
194+ "The action $ action already exists in controller $ controller of module $ module. "
195195 );
196196 }
197197
198- $ new_code ->addMethods (array (
198+ $ classGenerator ->addMethods (array (
199199 new Generator \MethodGenerator (
200200 $ action . 'Action ' ,
201201 array (),
@@ -204,26 +204,32 @@ public function methodAction()
204204 ),
205205 ));
206206
207- $ file = new Generator \FileGenerator (
207+ $ fileGenerator = new Generator \FileGenerator (
208208 array (
209- 'classes ' => array ($ new_code ),
209+ 'classes ' => array ($ classGenerator ),
210210 )
211211 );
212212
213- $ make_phtml = true ;
214- $ phtmlPath = $ path . "/module/ $ module/view/ " . strtolower ($ module ) . "/ " . strtolower ($ ctrl ) . '/ ' . $ action . '.phtml ' ;
215- if (file_exists ($ phtmlPath )) {
216- $ make_phtml = false ;
217- }
218-
219- if ($ make_phtml ) {
220- if (file_put_contents ($ phtmlPath , 'Action " ' .$ action .'", controller " ' .$ ucName .'", module " ' .$ module .'". ' )) {
221- $ phtml = true ;
213+ $ filter = new CamelCaseToDashFilter ();
214+ $ phtmlPath = sprintf (
215+ '%s/module/%s/view/%s/%s/%s.phtml ' ,
216+ $ path ,
217+ $ module ,
218+ strtolower ($ filter ->filter ($ module )),
219+ strtolower ($ filter ->filter ($ controller )),
220+ strtolower ($ filter ->filter ($ action ))
221+ );
222+ if (!file_exists ($ phtmlPath )) {
223+ $ contents = sprintf ("Module: %s \nController: %s \nAction: %s " , $ module , $ controller , $ action );
224+ if (file_put_contents ($ phtmlPath , $ contents )) {
225+ $ console ->writeLine (sprintf ("Created view script at %s " , $ phtmlPath ), Color::GREEN );
226+ } else {
227+ $ console ->writeLine (sprintf ("An error occurred when attempting to create view script at location %s " , $ phtmlPath ), Color::RED );
222228 }
223229 }
224230
225- if (file_put_contents ($ ctrlPath , $ file ->generate ())) {
226- $ console ->writeLine (" The action $ action has been created in controller $ module \\ $ controller. " , Color::GREEN );
231+ if (file_put_contents ($ controllerPath , $ fileGenerator ->generate ())) {
232+ $ console ->writeLine (sprintf ( ' The action %s has been created in controller %s \\ Controller \\ %s. ' , $ action , $ module , $ controller) , Color::GREEN );
227233 } else {
228234 $ console ->writeLine ("There was an error during action creation. " , Color::RED );
229235 }
@@ -252,7 +258,9 @@ public function moduleAction()
252258 );
253259 }
254260
255- $ viewfolder = strtolower ($ name );
261+ $ filter = new CamelCaseToDashFilter ();
262+ $ viewfolder = strtolower ($ filter ->filter ($ name ));
263+
256264 $ name = ucfirst ($ name );
257265 mkdir ("$ path/module/ $ name/config " , 0777 , true );
258266 mkdir ("$ path/module/ $ name/src/ $ name/Controller " , 0777 , true );
0 commit comments