Skip to content

Commit 0428618

Browse files
committed
cleanup makeController and generator
1 parent d372c76 commit 0428618

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

src/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function generateClass(string $className, string $templateName, array $va
8585
* @param array $variables Array of variables to pass to the template
8686
* @param bool $isController Set to true if generating a Controller that needs
8787
* access to the TemplateComponentGenerator ("generator") in
88-
* the twig template. e.g. to create route attributes on the index method
88+
* the twig template. e.g. to create route attributes for a route method
8989
*
9090
* @return string The path where the file will be created
9191
*

src/Maker/MakeController.php

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
6767

6868
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
6969
{
70-
$controllerClassNameDetails = $generator->createClassNameDetails(
71-
$input->getArgument('controller-class'),
72-
'Controller\\',
73-
'Controller'
74-
);
75-
7670
$withTemplate = $this->isTwigInstalled() && !$input->getOption('no-template');
7771
$isInvokable = (bool) $input->getOption('invokable');
7872

7973
$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+
}
8181

8282
$controllerClassData = ClassData::create(
83-
class: $isAbsolute ? substr($controllerClass, 1) : \sprintf('Controller\%s', $input->getArgument('controller-class')),
83+
class: $controllerClassName,
8484
suffix: 'Controller',
8585
extendsClass: AbstractController::class,
8686
useStatements: [
@@ -89,37 +89,29 @@ class: $isAbsolute ? substr($controllerClass, 1) : \sprintf('Controller\%s', $in
8989
]
9090
);
9191

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)
9499
;
95100

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+
96104
$controllerPath = $generator->generateClassFromClassData($controllerClassData, 'controller/Controller.tpl.php', [
97105
'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
98106
'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
99107
'method_name' => $isInvokable ? '__invoke' : 'index',
100108
'with_template' => $withTemplate,
101-
'template_name' => $templateName,
109+
'template_name' => $templatePath,
102110
], true);
103111

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-
120112
if ($withTemplate) {
121113
$generator->generateTemplate(
122-
$templateName,
114+
$templatePath,
123115
'controller/twig_template.tpl.php',
124116
[
125117
'controller_path' => $controllerPath,

0 commit comments

Comments
 (0)