@@ -38,6 +38,11 @@ final class MakeController extends AbstractMaker
3838{
3939 use CanGenerateTestsTrait;
4040
41+ private bool $ isInvokable ;
42+ private ClassData $ controllerClassData ;
43+ private bool $ usesTwigTemplate ;
44+ private string $ twigTemplatePath ;
45+
4146 public function __construct (private ?PhpCompatUtil $ phpCompatUtil = null )
4247 {
4348 if (null !== $ phpCompatUtil ) {
@@ -73,29 +78,24 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
7378
7479 public function interact (InputInterface $ input , ConsoleStyle $ io , Command $ command ): void
7580 {
76- $ this ->interactSetGenerateTests ($ input , $ io );
77- }
78-
79- public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
80- {
81- $ withTemplate = $ this ->isTwigInstalled () && !$ input ->getOption ('no-template ' );
82- $ isInvokable = (bool ) $ input ->getOption ('invokable ' );
81+ $ this ->usesTwigTemplate = $ this ->isTwigInstalled () && !$ input ->getOption ('no-template ' );
82+ $ this ->isInvokable = (bool ) $ input ->getOption ('invokable ' );
8383
8484 $ controllerClass = $ input ->getArgument ('controller-class ' );
8585 $ controllerClassName = \sprintf ('Controller\%s ' , $ controllerClass );
8686
8787 // If the class name provided is absolute, we do not assume it will live in src/Controller
8888 // e.g. src/Custom/Location/For/MyController instead of src/Controller/MyController
89- if ($ isAbsolute = '\\' === $ controllerClass [0 ]) {
89+ if ($ isAbsoluteNamespace = '\\' === $ controllerClass [0 ]) {
9090 $ controllerClassName = substr ($ controllerClass , 1 );
9191 }
9292
93- $ controllerClassData = ClassData::create (
93+ $ this -> controllerClassData = ClassData::create (
9494 class: $ controllerClassName ,
9595 suffix: 'Controller ' ,
9696 extendsClass: AbstractController::class,
9797 useStatements: [
98- $ withTemplate ? Response::class : JsonResponse::class,
98+ $ this -> usesTwigTemplate ? Response::class : JsonResponse::class,
9999 Route::class,
100100 ]
101101 );
@@ -104,45 +104,48 @@ class: $controllerClassName,
104104 // should live. E.g. templates/custom/location/for/my_controller.html.twig instead of
105105 // templates/my/controller.html.twig. We do however remove the root_namespace prefix in either case
106106 // so we don't end up with templates/app/my/controller.html.twig
107- $ templateName = $ isAbsolute ?
108- $ controllerClassData ->getFullClassName (withoutRootNamespace: true , withoutSuffix: true ) :
109- $ controllerClassData ->getClassName (relative: true , withoutSuffix: true )
107+ $ templateName = $ isAbsoluteNamespace ?
108+ $ this -> controllerClassData ->getFullClassName (withoutRootNamespace: true , withoutSuffix: true ) :
109+ $ this -> controllerClassData ->getClassName (relative: true , withoutSuffix: true )
110110 ;
111111
112112 // Convert the twig template name into a file path where it will be generated.
113- $ templatePath = \sprintf ('%s%s ' , Str::asFilePath ($ templateName ), $ isInvokable ? '.html.twig ' : '/index.html.twig ' );
114-
115- $ controllerPath = $ generator ->generateClassFromClassData ($ controllerClassData , 'controller/Controller.tpl.php ' , [
116- 'route_path ' => Str::asRoutePath ($ controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
117- 'route_name ' => Str::AsRouteName ($ controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
118- 'method_name ' => $ isInvokable ? '__invoke ' : 'index ' ,
119- 'with_template ' => $ withTemplate ,
120- 'template_name ' => $ templatePath ,
113+ $ this ->twigTemplatePath = \sprintf ('%s%s ' , Str::asFilePath ($ templateName ), $ this ->isInvokable ? '.html.twig ' : '/index.html.twig ' );
114+
115+ $ this ->interactSetGenerateTests ($ input , $ io );
116+ }
117+
118+ public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
119+ {
120+ $ controllerPath = $ generator ->generateClassFromClassData ($ this ->controllerClassData , 'controller/Controller.tpl.php ' , [
121+ 'route_path ' => Str::asRoutePath ($ this ->controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
122+ 'route_name ' => Str::AsRouteName ($ this ->controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
123+ 'method_name ' => $ this ->isInvokable ? '__invoke ' : 'index ' ,
124+ 'with_template ' => $ this ->usesTwigTemplate ,
125+ 'template_name ' => $ this ->twigTemplatePath ,
121126 ], true );
122127
123- if ($ withTemplate ) {
128+ if ($ this -> usesTwigTemplate ) {
124129 $ generator ->generateTemplate (
125- $ templatePath ,
130+ $ this -> twigTemplatePath ,
126131 'controller/twig_template.tpl.php ' ,
127132 [
128133 'controller_path ' => $ controllerPath ,
129134 'root_directory ' => $ generator ->getRootDirectory (),
130- 'class_name ' => $ controllerClassData ->getClassName (),
135+ 'class_name ' => $ this -> controllerClassData ->getClassName (),
131136 ]
132137 );
133138 }
134139
135140 if ($ this ->shouldGenerateTests ()) {
136141 $ testClassData = ClassData::create (
137- class: \sprintf ('Tests\Controller\%s ' , $ controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
142+ class: \sprintf ('Tests\Controller\%s ' , $ this -> controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
138143 suffix: 'ControllerTest ' ,
139144 extendsClass: WebTestCase::class,
140- useStatements: [
141- ]
142145 );
143146
144147 $ generator ->generateClassFromClassData ($ testClassData , 'controller/test/Test.tpl.php ' , [
145- 'route_path ' => Str::asRoutePath ($ controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
148+ 'route_path ' => Str::asRoutePath ($ this -> controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
146149 ]);
147150
148151 if (!class_exists (WebTestCase::class)) {
0 commit comments