@@ -67,20 +67,20 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
67
67
68
68
public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
69
69
{
70
- $ controllerClassNameDetails = $ generator ->createClassNameDetails (
71
- $ input ->getArgument ('controller-class ' ),
72
- 'Controller \\' ,
73
- 'Controller '
74
- );
75
-
76
70
$ withTemplate = $ this ->isTwigInstalled () && !$ input ->getOption ('no-template ' );
77
71
$ isInvokable = (bool ) $ input ->getOption ('invokable ' );
78
72
79
73
$ controllerClass = $ input ->getArgument ('controller-class ' );
80
- $ isAbsolute = '\\' === $ controllerClass [0 ];
74
+ $ controllerClassName = \sprintf ('Controller\%s ' , $ controllerClass );
75
+
76
+ // If the class name provided is absolute, we do not assume it will live in src/Controller
77
+ // e.g. src/Custom/Location/For/MyController instead of src/Controller/MyController
78
+ if ($ isAbsolute = '\\' === $ controllerClass [0 ]) {
79
+ $ controllerClassName = substr ($ controllerClass , 1 );
80
+ }
81
81
82
82
$ controllerClassData = ClassData::create (
83
- class: $ isAbsolute ? substr ( $ controllerClass , 1 ) : \sprintf ( ' Controller\%s ' , $ input -> getArgument ( ' controller-class ' )) ,
83
+ class: $ controllerClassName ,
84
84
suffix: 'Controller ' ,
85
85
extendsClass: AbstractController::class,
86
86
useStatements: [
@@ -89,37 +89,29 @@ class: $isAbsolute ? substr($controllerClass, 1) : \sprintf('Controller\%s', $in
89
89
]
90
90
);
91
91
92
- $ templateName = Str::asFilePath ($ isAbsolute ? $ controllerClassData ->getFullClassName (withoutRootNamespace: true , withoutSuffix: true ) : $ controllerClassData ->getClassName (relative: true , withoutSuffix: true ))
93
- .($ isInvokable ? '.html.twig ' : '/index.html.twig ' )
92
+ // Again if the class name is absolute, lets not make assumptions about where the twig template
93
+ // should live. E.g. templates/custom/location/for/my_controller.html.twig instead of
94
+ // templates/my/controller.html.twig. We do however remove the root_namespace prefix in either case
95
+ // so we don't end up with templates/app/my/controller.html.twig
96
+ $ templateName = $ isAbsolute ?
97
+ $ controllerClassData ->getFullClassName (withoutRootNamespace: true , withoutSuffix: true ) :
98
+ $ controllerClassData ->getClassName (relative: true , withoutSuffix: true )
94
99
;
95
100
101
+ // Convert the twig template name into a file path where it will be generated.
102
+ $ templatePath = \sprintf ('%s%s ' , Str::asFilePath ($ templateName ), $ isInvokable ? '.html.twig ' : '/index.html.twig ' );
103
+
96
104
$ controllerPath = $ generator ->generateClassFromClassData ($ controllerClassData , 'controller/Controller.tpl.php ' , [
97
105
'route_path ' => Str::asRoutePath ($ controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
98
106
'route_name ' => Str::AsRouteName ($ controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
99
107
'method_name ' => $ isInvokable ? '__invoke ' : 'index ' ,
100
108
'with_template ' => $ withTemplate ,
101
- 'template_name ' => $ templateName ,
109
+ 'template_name ' => $ templatePath ,
102
110
], true );
103
111
104
- // $controllerPath = $generator->generateController(
105
- // $controllerClassData->getFullClassName(),
106
- // 'controller/Controller.tpl.php',
107
- // [
108
- // 'class_data' => $controllerClassData,
109
- // // 'use_statements' => $useStatements,
110
- // // 'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
111
- // 'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
112
- // 'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
113
- // // 'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
114
- // 'method_name' => $isInvokable ? '__invoke' : 'index',
115
- // 'with_template' => $withTemplate,
116
- // 'template_name' => $templateName,
117
- // ]
118
- // );
119
-
120
112
if ($ withTemplate ) {
121
113
$ generator ->generateTemplate (
122
- $ templateName ,
114
+ $ templatePath ,
123
115
'controller/twig_template.tpl.php ' ,
124
116
[
125
117
'controller_path ' => $ controllerPath ,
0 commit comments