@@ -38,6 +38,11 @@ final class MakeController extends AbstractMaker
38
38
{
39
39
use CanGenerateTestsTrait;
40
40
41
+ private bool $ isInvokable ;
42
+ private ClassData $ controllerClassData ;
43
+ private bool $ usesTwigTemplate ;
44
+ private string $ twigTemplatePath ;
45
+
41
46
public function __construct (private ?PhpCompatUtil $ phpCompatUtil = null )
42
47
{
43
48
if (null !== $ phpCompatUtil ) {
@@ -73,29 +78,24 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
73
78
74
79
public function interact (InputInterface $ input , ConsoleStyle $ io , Command $ command ): void
75
80
{
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 ' );
83
83
84
84
$ controllerClass = $ input ->getArgument ('controller-class ' );
85
85
$ controllerClassName = \sprintf ('Controller\%s ' , $ controllerClass );
86
86
87
87
// If the class name provided is absolute, we do not assume it will live in src/Controller
88
88
// e.g. src/Custom/Location/For/MyController instead of src/Controller/MyController
89
- if ($ isAbsolute = '\\' === $ controllerClass [0 ]) {
89
+ if ($ isAbsoluteNamespace = '\\' === $ controllerClass [0 ]) {
90
90
$ controllerClassName = substr ($ controllerClass , 1 );
91
91
}
92
92
93
- $ controllerClassData = ClassData::create (
93
+ $ this -> controllerClassData = ClassData::create (
94
94
class: $ controllerClassName ,
95
95
suffix: 'Controller ' ,
96
96
extendsClass: AbstractController::class,
97
97
useStatements: [
98
- $ withTemplate ? Response::class : JsonResponse::class,
98
+ $ this -> usesTwigTemplate ? Response::class : JsonResponse::class,
99
99
Route::class,
100
100
]
101
101
);
@@ -104,45 +104,48 @@ class: $controllerClassName,
104
104
// should live. E.g. templates/custom/location/for/my_controller.html.twig instead of
105
105
// templates/my/controller.html.twig. We do however remove the root_namespace prefix in either case
106
106
// 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 )
110
110
;
111
111
112
112
// 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 ,
121
126
], true );
122
127
123
- if ($ withTemplate ) {
128
+ if ($ this -> usesTwigTemplate ) {
124
129
$ generator ->generateTemplate (
125
- $ templatePath ,
130
+ $ this -> twigTemplatePath ,
126
131
'controller/twig_template.tpl.php ' ,
127
132
[
128
133
'controller_path ' => $ controllerPath ,
129
134
'root_directory ' => $ generator ->getRootDirectory (),
130
- 'class_name ' => $ controllerClassData ->getClassName (),
135
+ 'class_name ' => $ this -> controllerClassData ->getClassName (),
131
136
]
132
137
);
133
138
}
134
139
135
140
if ($ this ->shouldGenerateTests ()) {
136
141
$ 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 )),
138
143
suffix: 'ControllerTest ' ,
139
144
extendsClass: WebTestCase::class,
140
- useStatements: [
141
- ]
142
145
);
143
146
144
147
$ 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 )),
146
149
]);
147
150
148
151
if (!class_exists (WebTestCase::class)) {
0 commit comments